първия въпрос тук. Опитвам се да науча Python, като преминавам през проекта euler, и се натъкнах на пречка. Следният метод (връща списък с прости множители) работи добре за едно извикване:
def findPrimeFactors(num, primeFactors = []):
'''Find the prime factors of an arbitrary positive integer
input: num to factorize
returns: a list containing the prime factors of the number
'''
pIndex = 2
while (num >= pIndex):
if num % pIndex == 0:
num /= pIndex
primeFactors.append(pIndex)
return FindPrimes.findPrimeFactors(num, primeFactors)
else:
pIndex += 1
return primeFactors
Въпреки това, когато го използвам в цикъл, така (този метод може все още да не е завършен, в момента води до безкраен цикъл, тъй като не могат да бъдат намерени повече прости числа):
def countPrimes(n = 1001):
'''find n amount of unique primes ascending
input: number of primes to find
returns: list of n primes starting from 2 '''
primes = []
i = 2
while len(primes) < n:
primeFactors = FindPrimes.findPrimeFactors(i)
print(primeFactors) #verify method behavior
if len(primeFactors) is 1:
primes.append(primeFactors[0])
i += 1
return primes
Резултатът е, че първият цикъл връща [2], следващият връща [2, 3] и т.н., добавяйки новите резултати към списъка, който исках да е празен при първото рекурсивно повикване. Изглежда, че списъкът ми продължава, но не съм сигурен точно защо? Прочетох и Python Class scope & lists, което ми дава някои улики, но рекурсията го усложнява още повече .
Рекурсивно също означава, че не мога просто да му присвоя празен набор. Идвайки от фона на C++, очакванията ми бяха, че променливата primeFactors трябва да се инициализира отново всеки път, когато функцията се извиква от моята програма. Тук все още е бебе змия.
РЕДАКТИРАНЕ: Това е итеративната версия на findPrimeFactors, която написах. Знам, че не е оптимално - но бих искал поне да го направя достатъчно ефективен, за да отговаря на правилото за 1 минута на Project Euler. Всички предложения за подобрение или яснота се оценяват.
PRIMES = [2,3,5,7,11,13,17,19]
import math
class FindPrimes():
'''V2 iterative'''
def findPrimeFactors(n, primeFactors = None):
'''Find the prime factors of an arbitrary positive integer
input: num to factorize
returns: a list containing the prime factors of the number
'''
if primeFactors is None:
primeFactors = []
num = n
ceil = math.sqrt(n) #currently unused
global PRIMES
knownPrimes = PRIMES
#check known primes for divisors first, then continue searching for primes by brute force
while True:
factorFound = False
for prime in knownPrimes:
if num % prime == 0:
primeFactors.append(prime)
num /= prime
factorFound = True
break #ensure that the list returned has ascending primes
if not factorFound:
break
#once attempts have been made to reduce using known primes
#search for new primes if the number is not fully reduced
i = knownPrimes[-1] + 2
while num != 1:
if num % i == 0:
knownPrimes.append(i)
primeFactors.append(i)
num /= i
i += 2
return primeFactors
def countPrimes(n = 10001):
'''find n amount of unique primes ascending
input: number of primes to find
returns: list of n primes starting from 2 '''
primes = []
i = 2
while len(primes) < n:
primeFactors = FindPrimes.findPrimeFactors(i)
if len(primeFactors) == 1:
primes.append(primeFactors[0])
#print(primeFactors[-1])
i += 1
print(len(primes))
return primes
nth = 10001
print(FindPrimes.countPrimes(nth)[nth-1]) #print the largest prime found