Solved using sieve of eratosthenes

precalcualte primes up to 10 million