mirror of
https://github.com/freebsd/freebsd-src.git
synced 2024-11-28 04:43:32 +00:00
27 lines
385 B
C++
27 lines
385 B
C++
|
#include <math.h>
|
||
|
|
||
|
int is_prime(unsigned n)
|
||
|
{
|
||
|
if (n <= 3)
|
||
|
return 1;
|
||
|
if (!(n & 1))
|
||
|
return 0;
|
||
|
if (n % 3 == 0)
|
||
|
return 0;
|
||
|
unsigned lim = unsigned(sqrt((double)n));
|
||
|
unsigned d = 5;
|
||
|
for (;;) {
|
||
|
if (d > lim)
|
||
|
break;
|
||
|
if (n % d == 0)
|
||
|
return 0;
|
||
|
d += 2;
|
||
|
if (d > lim)
|
||
|
break;
|
||
|
if (n % d == 0)
|
||
|
return 0;
|
||
|
d += 4;
|
||
|
}
|
||
|
return 1;
|
||
|
}
|