Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to calculate one million prime numbers

I have got one question to print one million prime numbers . I have written a java program for that .. It's currently taking 1.5 mins approx to calculate it .. I think my solution is not that efficient. I have used the below algo:

  • Adding 1 2 3 to the prime list initially
  • Calculating the last digit of the number to be checked
  • Checking if the digit is 0 , 2 or 4 or 6 or 8 then skipping the number
  • else calculating the square root of the number ..
  • Trying to Divide the number starting from 2 till the square root of the number
  • if number is divisible then skipping the number else adding it to the prime list

I have read several other solutions as well , but I didn't find a good answer. Please suggest ideally what should be approx minimum time to calculate this and what changes are required to make the algorithm more efficient.

like image 699
priyas Avatar asked Nov 15 '12 19:11

priyas


People also ask

What is the 1 millionth prime?

First, 1 is not a prime number. Second, the millionth prime is 15,485,863, so you need to be prepared for some large data-handling.

What is the formula to calculate prime numbers?

Methods to Find Prime Numbers Method 1: Two consecutive numbers which are natural numbers and prime numbers are 2 and 3. Apart from 2 and 3, every prime number can be written in the form of 6n + 1 or 6n – 1, where n is a natural number. Note: These both are the general formula to find the prime numbers.

Is there trick to finding prime numbers?

Take a number, say, 26577. The unit digit of this number is not 0, 2, 4, 6 or 8. Now, take the sum of digits which will be: 2 + 6 + 5 + 7 + 7 = 27. Since 27 is divisible by 3, 26577 is not a prime number.


3 Answers

If you added 1 to your list, your answer is wrong already :)

Anyway, Sieve of Erathosthenes is where you should begin, it's incredibly simple and quite efficient.

Once you're familiar with the idea of sieves and how they work, you can move on to Sieve of Atkin, which is a bit more complicated but obviously more efficient.

like image 149
biziclop Avatar answered Sep 24 '22 10:09

biziclop


Key things:

  1. Skip all even numbers. Start with 5, and just add two at a time.
  2. 1 isn't a prime number...
  3. Test a number by finding the mod of all prime numbers till the square root of the number. No need to test anything but primes.
like image 21
PearsonArtPhoto Avatar answered Sep 23 '22 10:09

PearsonArtPhoto


A simple sieve of Eratosthenes runs like the clappers. This calculates the 1,000,000th prime in less than a second on my box:

class PrimeSieve
{
    public List<int> Primes;

    private BitArray Sieve;

    public PrimeSieve(int max)
    {
        Primes = new List<int> { 2, 3 }; // Must include at least 2, 3.
        Sieve = new BitArray(max + 1);
        foreach (var p in Primes)
            for (var i = p * p; i < Sieve.Length; i += p) Sieve[i] = true;
    }

    public int Extend()
    {
        var p = Primes.Last() + 2; // Skip the even numbers.
        while (Sieve[p]) p += 2;
        for (var i = p * p; i < Sieve.Length; i += p) Sieve[i] = true;
        Primes.Add(p);
        return p;
    }
}

EDIT: sieving optimally starts from p^2, not 2p, as Will Ness correctly points out (all compound numbers below p^2 will have been marked in earlier iterations).

like image 31
Rafe Avatar answered Sep 23 '22 10:09

Rafe