[英]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 在isprime()函数中
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. 因此,基本上,您正要在循环开始之前检查2和3的可除性。
Then for checking with other numbers, you start from 5 and go till that i*i = n
. 然后使用其他数字进行检查,从5开始直到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
. 那是因为一个可以被任意数i
整除的数n
总是意味着该数i
小于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. 说37。i i*i>n
最小数字为6
,因此,您只需要检查直到6
,就可以继续检查,因为您已经检查了所有其他倍数。 So, if you don't find any number beyond 6
here, you need not to go further for check. 因此,如果您在此处找不到6
以外的任何数字,则无需进一步检查。
Second part is the other condition where you are incrementing by 2 for checking if
condition. 第二部分是,你是通过2检查递增的其他条件if
条件。 This is because you are starting the divisibility by 5
and incrementing it by 6
everytime. 这是因为您每次都将除数从5
开始,然后将其递增6
。 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. 您会注意到,最后一列中的所有数字都是3的倍数。如果任何数字可被这些数字整除,它们也可被3整除。 Since the function already checks whether the number is divisible by 3 at the beginning, it's not necessary to check that. 由于该函数已经开始检查数字是否可以被3整除,因此没有必要进行检查。 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: 行之间的增量为6。您可以将其转换为:
i = 5
从i = 5
开始 i
or i+2
. 检查数字是否可以被i
或i+2
整除。 If so, return false
. 如果是这样,则返回false
。 i
by 6
and repeat. 将i
递增6
然后重复。 That's what the for
loop does. 这就是for
循环的作用。
Why is the conditional of the for
statement i*i <= n
? 为什么for
语句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
. 如果到达i*i
> n
的点,则可以确保n
不被i
整除。 Continuing with the loop for any i
greater than that will not change the value of the conditional. 继续循环大于等于i
不会更改条件值。 The number is a prime number when we reach that point. 当达到这一点时,该数字就是质数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.