Does C++ have any built in function to check if the number is prime or not. If yes, then in which library?
Below is my implementation. But was just looking if there is any built in function. Searching on Google just gives user based implementations.
int isprime(int N){
if(N<2 || (!(N&1) && N!=2))
return 0;
for(int i=3; i*i<=N; i+=2){
if(!(N%i))
return 0;
}
return 1;
}
Short answer: no, there’s no such function.
The only time the word “prime” is used in the standard is a footnote in 26.5.3.2, which is where the
mersenne_twister_engineclass template is described. The footnote says:If such function existed, the standard would contain more occurrences of that word, as it would use it to describe the behavior of that function.