【发布时间】:2012-10-11 13:41:48
【问题描述】:
13195 的质因数是 5、7、13 和 29。什么是最大的 数字 600851475143 的素数?
好的,所以我正在处理 python 中的项目 euler 问题 3。我有点困惑。我不知道我从这个程序中得到的答案是否正确。如果有人能告诉我我做错了什么,那就太好了!
#import pdb
odd_list=[]
prime_list=[2] #Begin with zero so that we can pop later without errors.
#Define a function that finds all the odd numbers in the range of a number
def oddNumbers(x):
x+=1 #add one to the number because range does not include it
for i in range(x):
if i%2!=0: #If it cannot be evenly divided by two it is eliminated
odd_list.append(i) #Add it too the list
return odd_list
def findPrimes(number_to_test, list_of_odd_numbers_in_tested_number): # Pass in the prime number to test
for i in list_of_odd_numbers_in_tested_number:
if number_to_test % i==0:
prime_list.append(i)
number_to_test=number_to_test / i
#prime_list.append(i)
#prime_list.pop(-2) #remove the old number so that we only have the biggest
if prime_list==[1]:
print "This has no prime factors other than 1"
else:
print prime_list
return prime_list
#pdb.set_trace()
number_to_test=raw_input("What number would you like to find the greatest prime of?\n:")
#Convert the input to an integer
number_to_test=int(number_to_test)
#Pass the number to the oddnumbers function
odds=oddNumbers(number_to_test)
#Pass the return of the oddnumbers function to the findPrimes function
findPrimes(number_to_test , odds)
【问题讨论】:
-
不一定每个人都解决了
Project Euler # 3。至少在这里发布问题陈述。你需要告诉我们你的代码有什么问题,而不是问任何人。 -
“我无法判断我通过这个程序得到的答案是否正确。” 测试分解程序很容易:只需将因子相乘,然后查看如果结果与原始数字匹配。
-
对素数尝试筛算法,否则
600851475143的蛮力将花费很长时间。 -
@AshwiniChaudhary 蛮力解决这个问题应该不会超过几秒钟,如果你(正确地)停在 775146。