13195的主要因素是5,7,13和29.什麼是最大的 數字600851475143的主要因素?卡住在項目歐拉#3 python
好的,所以我正在處理python中的項目歐羅問題3。我有點困惑。我無法判斷我得到的答案是否正確。如果somone可以告訴我什麼即時做錯了,那會很棒!
#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。至少在這裏發佈問題陳述。你需要告訴我們你的代碼有什麼問題,而不是問任何人。 –
_「我不知道我用這個程序得到的答案是否正確。」_測試分解程序很簡單:只需乘以因子,看看結果是否與原始數字相匹配。 – hammar
嘗試質數的篩選算法,否則蠻力需要花費很長時間來處理'600851475143'。 –