I wrote the following function, which finds all the divisors of a given natural number and returns them as a list:
def FindAllDivisors(x):
divList = []
y = 1
while y <= math.sqrt(x):
if x % y == 0:
divList.append(y)
divList.append(int(x / y))
y += 1
return divList
It works great with the exception that it is really slow when input contains an 18-digit number. Do you have any suggestions on how I can speed it up?
Update
I have the following primitiveness test method based on Fermat’s small theorem:
def CheckIfProbablyPrime(x):
return (2 << x - 2) % x == 1
This method is really effective at checking a single number, however, I'm not sure if I should use it to compile all primes to a certain limit.
source
share