in reply to Find prime number between 1 to 1000000

Back when I was doing project Euler problems I created a library for myself that took care of a number of common needs. They aren't particularly fast, but they were good enough for that.

In it I had 2 prime testing algorithms. The first, is_prime, generates all of the primes up to a given one then does a binary search. On my laptop it generates all of the primes up to a million in under half a second, and after that I can do about 70,0000 primality tests/second on numbers 1..1_000_000. The other one, is_prime2, generates the primes up to the square root of a given one and does trial division. It is avoids the up front load but can only do about 25,000 primality tests per second on numbers 1..1,000,000. My laptop may be faster or slower than your computer, but either should be more than fast enough for your purposes.

I use integer so neither primality test will work past 2 billion. If you want to test primality for larger numbers, I highly recommend Math::Pari which pushes it down to a highly optimized C library. I sometimes find it amusing to watch it factor random 100 digit numbers in the blink of an eye.

Anyways here is my Perl library. Be warned it has a lot of irrelevant stuff in it and was only meant for personal use. (Hence no documentation.) However it has a lot of goodies for anyone trying to do project Euler problems.