-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPrimility Test.cpp
More file actions
34 lines (30 loc) · 827 Bytes
/
Primility Test.cpp
File metadata and controls
34 lines (30 loc) · 827 Bytes
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
typedef unsigned long long ull;
typedef long long ll;
ull modmul(ull a, ull b, ull m)
{
ll ret = a * b - m * ull(1.L / m * a * b);
return ret + m * (ret < 0) - m * (ret >= (ll)m);
}
ull modpow(ull b, ull e, ull mod)
{
ull ans = 1;
for (; e; b = modmul(b, b, mod), e /= 2)
if (e & 1)
ans = modmul(ans, b, mod);
return ans;
}
bool is_Prime(ull n)
{
if (n < 2 || n % 6 % 4 != 1)
return (n | 1) == 3;
ull A[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}, s = __builtin_ctzll(n - 1), d = n >> s;
for (ull a : A)
{
ull p = modpow(a % n, d, n), i = s;
while (p != 1 && p != n - 1 && a % n && i--)
p = modmul(p, p, n);
if (p != n - 1 && i != s)
return 0;
}
return 1;
}