-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathisprime.c
45 lines (34 loc) · 994 Bytes
/
isprime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "isprime.h"
#include <stdlib.h>
static unsigned long long power(unsigned long long x, unsigned long long y, unsigned long long p) {
unsigned long long result = 1;
x %= p;
while (y) {
if (y & 1) result = (result * x) % p;
y >>=1;
x = (x * x) % p;
}
return result;
}
static bool miller_rabin_test(unsigned long long d, unsigned long long n) {
unsigned long long a = 2 + rand() % (n - 4);
unsigned long long x = power(a, d, n);
if (x == 1 || x == n - 1) return true;
while (d != n - 1) {
x = (x * x) % n;
d *= 2;
if (x == 1) return false;
if (x == n - 1) return true;
}
return false;
}
bool is_prime(unsigned long long n, unsigned int k) {
if (n <= 1 || n == 4) return false;
if (n <= 3) return true;
unsigned long long d = n - 1;
while (d % 2 == 0) d /= 2;
for (; k != 0; --k) {
if (!miller_rabin_test(d, n)) return false;
}
return true;
}