中给出的大多数数字的意外输出。(Python)
我正在尝试用Python编写一个素因子分解代码,以下是我到目前为止所做的工作:中给出的大多数数字的意外输出。(Python),python,loops,primes,Python,Loops,Primes,我正在尝试用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 assi
# 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这样的数字,但与其他数字的输出很奇怪,请帮我找出错误,谢谢 函数isprime中的局部变量因子缺失:
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
工作正常,经过此校正后工作示例(问题在于共享的“因子”列表变量)
输出
# > 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
此功能已中断:
...
factors = []
...
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
因此,isprime
将继续一次又一次地向全局变量factors
追加i
,即使它已经包含了这个数字。因此,因子
的长度将继续增长,超过2。事实上,对于n==255
,因子将包含1456个数字!对于任何大于2的数字,函数将返回False
,找到的唯一素数是2
您需要将因子设置为本地:
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
或使用生成器表达式以避免浪费内存:
def isprime(num):
factors = (f for f in range(1, num + 1) if num % f == 0)
assert next(factors) == 1 # this is always true
try:
# this will be true if there are no more factors other than `num` itself
return next(factors) == num
except StopIteration:
# There are no more factors, so `num` must equal 1, which is not prime
return False
“用其他数字给出奇怪的输出”-它们有什么奇怪之处?因子应该是isprime函数定义中的局部变量
def isprime(num):
factors = (f for f in range(1, num + 1) if num % f == 0)
assert next(factors) == 1 # this is always true
try:
# this will be true if there are no more factors other than `num` itself
return next(factors) == num
except StopIteration:
# There are no more factors, so `num` must equal 1, which is not prime
return False