Project Euler Problem 3

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:
            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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s