I tried to solve some Euler's problems, this is the solution I found for problem #3 with Python. The problem asks:
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
How can I improve my code?
def get_largest_prime2(value):
start = time.clock() #used to measure the execution time
divisors = [] #this list will be filled with all number's divisors
i = 1
while value != 1:
while value%i == 0: #check if the value can be divided by i and, if so, iterate the process.
value = (value // i)
divisors.append(i)
if i == 1 : break #it's needed to avoid an infinity loop
if i >= 3: i += 2 #when i is bigger than 3, search divisors among odd numbers( this reduces the search field)
else: i += 1
print( time.clock()-start)
return max(divisors) #returns the max prime divisors