简体   繁体   中英

Finding whether a number is prime or not c++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}

int main() {
    int T,n;
    cin>>T;
    while(T--){
    cin>>n;
    isPrime(n)?  cout << "Prime\n": cout << "Not prime\n";
    }
    return 0;
}

Hey, so I am working on this code to find whether a number is a prime or not, and I did lots of research but I am unable to find the working of this step.

in isprime() function

for (int i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return false;

Please help me figure this out any help is appreciated

Well, this is one of the classic algorithms for checking a prime nature of number. So basically, you are checking for divisibility by 2 and 3 right before the loop starts.

Then for checking with other numbers, you start from 5 and go till that i*i = n . That is because a number n which is divisible by any number i would always mean that number i is less than the square root of n . You can verify it through various examples. Say 37. Smallest number for which i*i>n is 6 and hence, you need to check it only till number 6 and not go beyond for checking ahead because all the other multiples you have already checked. So, if you don't find any number beyond 6 here, you need not to go further for check.

Second part is the other condition where you are incrementing by 2 for checking if condition. This is because you are starting the divisibility by 5 and incrementing it by 6 everytime. By doing this, you are ensuring that you only check possible prime numbers for the divisibility test and not any others.

I hope the logic is clear now. Feel free to ask any doubts you have in comments.

The loop

for (int i=5; i*i<=n; i=i+6)
  if (n%i == 0 || n%(i+2) == 0)
    return false;

could have been written as:

for (int i=5; i*i<=n; i=i+2)
  if (n%i == 0 )
    return false;

for easier understanding. You check whether the number is divisible by:

5 7 9 11 13, etc.

If you rearrange those odd numbers as:

5 7 9
11 13 15
17 19 21
23 25 27

etc.,

you'll notice that the all the numbers in the last column are multiples of 3. If any number is divisible by those, they are divisible by 3 also. Since the function already checks whether the number is divisible by 3 at the beginning, it's not necessary to check that. Hence, we need to check whether the number is divisible only by:

5 7 
11 13
17 19
23 25

etc.

The patter for those number is:

i i+2

with the increment between the rows being 6. You can translate that to:

  1. Start with i = 5
  2. Check whether the number is divisible by i or i+2 . If so, return false .
  3. Increment i by 6 and repeat.

That's what the for loop does.

Why is the conditional of the for statement i*i <= n ?

That's because a number cannot be divisible by any number greater than its square root. If you reach the point where i*i > n , you are assured that n is not divisible by i . Continuing with the loop for any i greater than that will not change the value of the conditional. The number is a prime number when we reach that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM