I have been trying to learn Python and just solved Euler Problem 3:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
I am looking for any feedback on improving efficiency and the code itself.
from itertools import count, islice def is_prime(number): for n in islice(count(2), int(number**0.5 -1)): if number % n == 0: return False return True def find_factors(number): lst =  for n in islice(count(2), int(number/2-1)): if number % n == 0: if is_prime(n): lst.append(n) return lst print(find_factors(13195).pop())
islice instead of
xrange because of
int limits, which I learned about from another question at StackOverflow.com
In code above, I have created two separate functions, one to find a prime and the other to find factors.
The program works this way,
- It takes an input number
- Find all the factors of that number from
- If it finds a factor, it checks if its a prime, and append it to a list.
- Eventually I print the last element of list since list in python are ordered.