0

so I'm trying to create a function, which will return either 1 or 0, depending whether the given number is or isn't a prime number.

NOTE: I know for certain that the given number is Natural. Greater than 1

My original function:

int Prime(int a) {
    int i;
    for (i = 2; i*i <= a; i++)
    {
       if ((a % i) == 0)
          return 0;
    };
    return 1;
}

works just fine, but is somehow... slow. I'm looking for a more efficient algorithm without using an array . My second try:

int Prime(int a) {
    int i;
    if (a == 2)
       return 1;
    if ((a % 2) == 0)
       return 0;

    for (i = 3; i*i <= a; i = i + 3)
    {
       if ((a % i) == 0) 
          return 0;
    };

    return 1;
}

ended badly. There is some number (which I can't really imagine), less than MAX_INT, that causes this particular algorithm to work extremely slowly. So, I have 2 questions:

  1. Is something wrong with my upgraded algorithm?

  2. Is there any way to do this task more efficiently?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Michal Kučera
  • 223
  • 1
  • 11

3 Answers3

1

1) Is something wrong with my upgraded algorithm?

Yes.

i=i+3 needs to be i=i+2, otherwise you're checking multiples of 3 (3,6,9,12,...) instead of odd numbers (3,5,7,9,...)

2) Is there any way to do this task more efficiently?

You can compute sqrt(a) and assign it to a variable (say sqrtOfA) right at the beginning and just check i <= sqrtOfA in your loop condition.

Or you can try a prime number sieve like the sieve of Eratosthenes.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • Thanks for your response. That i=i+3 was just my mistake, algorithm has i=i+2 in it. But the problem remains the same. As far as sieves are concerned, I was unable to find a way to make them happen without an array. My problem with arrays lies with it's size. I need this algorithm to work even with very large numbers – Michal Kučera Oct 26 '13 at 15:37
  • @MichalKučera Remember that, with a sieve, each number can be a single bit, thus up to 2147483647 can be stored in 256 MB. If not that, I don't think you'll be able to get a much more efficient ([guaranteed](http://en.wikipedia.org/wiki/Probable_prime)) prime number checker. – Bernhard Barker Oct 26 '13 at 15:42
  • So, I'm trying now to implement a sieve in this function. I have the basic idea, but there is one question remaining. I need to create array, as largest as possible, and I am very limited on RAM. Is there any data type, that can contain numbers 1 and 0, and is as small as possible? – Michal Kučera Oct 26 '13 at 16:15
  • For C? You'll probably have to create such a data structure yourself, although it's not too difficult, see [**this question**](http://stackoverflow.com/questions/4372515/how-to-implement-a-bitset-in-c) for more information (if you're actually using C++, there is [std::bitset](http://en.cppreference.com/w/cpp/utility/bitset)). – Bernhard Barker Oct 26 '13 at 16:18
  • @MichalKučera - I see you just started using SO. An up-click or accept is not a bad thing if this answer has helped you :) – ryyker Oct 26 '13 at 16:25
  • @ryyker - Indeed I have just started :). I did accept the solution, but due to my low reputation, I cannot up-click it, since 15 reputation is requied for that – Michal Kučera Oct 26 '13 at 17:17
  • we have to write a condition for checking number ''1'', as the result of the above code returns 1 instead of 0. – Abhiped Mar 15 '18 at 14:55
0

Here it depends if you want to know if number is prime or just with high probability prime. If you are satisfied that program could give wrong answer or meteor crashes it while meteor crash is more probable then you can use O(1) checks like Miller-Rabin test.

http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test

Also there is a deterministic O(n^12) algorithm for primality testing but it is not used much in practice.

neleai
  • 1
0

@Dukeling is absolutely correct. Also, I want to add a small condition for checking "is 1 prime?"

Because in standard procedure, we get 1 as not prime with the help of the loop itself. But here, we have to manually add a condition for that.

So just add the following 'if' condition and your code runs perfectly!

if(a==1)
return 0;
Abhiped
  • 431
  • 3
  • 4