-2

I have written the following to code to find largest prime factor of the number 600851475143 but the computation time is too large. Is there anything I can do to reduce the computation time. Thanks!

def factor(n):
    answer = [] 
    for i in range(1,(n//2) + 1):
        if n%i == 0:
            print(i)
            answer.append(i)
    return answer        

def prime_factor(n):
    l = n[:]     
    for i in n:
        for j in range(2,i):
            if i%j == 0:
                l.remove(i)
                break
    return l  
print(prime_factor(factor(600851475143)))

1 Answers1

0

This code is pretty quick

num = 600851475143
factor = 2 
while num > 1:
    while num % factor == 0:
        num = num//factor
    factor+=1

print(factor - 1)

Take the number and start dividing it by integers(start from 2) repeatedly until you can't. Now try for the next integer and then the next and so on... The last factor with which you can divide num will be the greatest prime.

Pretty obvious once it clicks. Have fun problem solving :)