gpt4 book ai didi

python - 中给出的大多数数字的意外输出。(Python)

转载 作者:行者123 更新时间:2023-12-01 06:41:59 26 4
gpt4 key购买 nike

我正在尝试用 Python 编写质因数分解代码,这是我到目前为止所做的:

# Prime Factorisation
while True:
try:
n, primes, factorisation, dividers, factors = abs(int(input('Enter an integer to find it\'s Prime Factorisation: '))), [], [], [], [] # Asks for input and assigns multiple variables and lists
break
except:
print('Please enter an integer.')
def isprime(num): # Checks if given number is prime
for i in range(1,num+1):
if num % i == 0:
factors.append(i)
return len(factors)== 2
for i in range(2,n+1):
if isprime(i):
primes.append(i)
for i in primes: # This code does the actual Prime Factorisation
while n % i == 0: # If n (The input the user gave) is divisible by i of list primes:
factorisation.append(n) # n is added to factorisation
dividers.append(i) # i is added to divisors
n /= i # n = n / i
output = str(dividers).replace(', ',' x ').replace('[','').replace(']','') # Pretties up the list dividers
print(str(factorisation[0]) + ' = ' + output) # Prints given value and divisors

该代码适用于像 256 这样的数字,但对于其他数字会给出奇怪的输出,请帮我找出错误,谢谢!

最佳答案

工作示例(问题在于共享“因素”列表变量)。

# Prime Factorisation
while True:
try:
n, primes, factorisation, dividers, factors = abs(int(input(
'Enter an integer to find it\'s Prime Factorisation: '))), [], [], [], [] # Asks for input and assigns multiple variables and lists
break
except:
print('Please enter an integer.')


def isprime(num): # Checks if given number is prime
factors = []

for i in range(1, num + 1):
if num % i == 0:
factors.append(i)

return len(factors) == 2


for i in range(2, n + 1):
if isprime(i):
primes.append(i)

for i in primes: # This code does the actual Prime Factorisation
while n % i == 0: # If n (The input the user gave) is divisible by i of list primes:
factorisation.append(n) # n is added to factorisation
dividers.append(i) # i is added to divisors
n /= i # n = n / i

output = str(dividers).replace(', ', ' x ').replace('[', '').replace(']', '') # Pretties up the list dividers

print(str(factorisation[0]) + ' = ' + output) # Prints given value and divisors

输出

# > python test.py
Enter an integer to find it's Prime Factorisation: 247
247 = 13 x 19

更干净的版本

只是源代码的更简洁版本

# Prime Factorisation
while True:
try:
n = abs(int(input(
'Enter an integer to find it\'s Prime Factorisation: '))) # Asks for input and assigns multiple variables and lists
break
except:
print('Please enter an integer.')


def isprime(num): # Checks if given number is prime
return len([n for n in range(1, num + 1) if num % n == 0]) == 2


primes = [n for n in range(2, n + 1) if isprime(n)]

factorisation, dividers = [], []
for i in primes: # This code does the actual Prime Factorisation
while n % i == 0: # If n (The input the user gave) is divisible by i of list primes:
factorisation.append(n) # n is added to factorisation
dividers.append(i) # i is added to divisors
n /= i # n = n / i

output = str(dividers).replace(', ', ' x ').replace('[', '').replace(']', '') # Pretties up the list dividers

print(str(factorisation[0]) + ' = ' + output) # Prints given value and divisors

关于python - 中给出的大多数数字的意外输出。(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59411221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com