The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143?

The trick in this problem is to divide out the value of d (in this case a number) if the remainder is 0. By doing this, you deal the other values that could be divisible by 2 such as 4 and 8. And this value of d must be appended to an array of factors. An array which is basically tracking the values that divide into n.

def largest_factor(n):
factor = []
d = 2
while n > 1:
while n % d == 0:
factor.append(d)
n /= d
d +=1
return factor

This second snippet of code runs the function and gets the result, but you still have to call the max function on the array to obtain the largest value which is the answer to this problem.

answer = largest_factor(600851475143)
max_value = max(answer)
print max_value

### Like this:

Like Loading...