I complicated this problem by trying to list all the primes below the square root of 600851475143 using Sieve of Eratosthenes algorithm. However this problem can actually be solved by a very simple way:
- Find the smallest prime factor 'spf ' of n
- Let n be the quotient of the division n / spf
- Repeat step 1 and 2 until n is equal to 1
In other words, this trick exploits the fact that smallest factor of a number must be a prime. Think about it for a moment!
My program solved this problem gracefully and gave the answer: 6857
1 comment:
I believe this problem can be solved more efficiently with Sieve of Eratosthenes algorithm:
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Post a Comment