MCPcopy Create free account
hub / github.com/SuprDewd/CompetitiveProgramming / is_probable_prime

Function is_probable_prime

code/mathematics/miller_rabin.cpp:2–18  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1#include "mod_pow.cpp"
2bool is_probable_prime(ll n, int k) {
3 if (~n & 1) return n == 2;
4 if (n <= 3) return n == 3;
5 int s = 0; ll d = n - 1;
6 while (~d & 1) d >>= 1, s++;
7 while (k--) {
8 ll a = uniform_int_distribution(2LL, n-2)(rng);
9 ll x = mod_pow(a, d, n);
10 if (x == 1 || x == n - 1) continue;
11 bool ok = false;
12 rep(i,0,s-1) {
13 x = (x * x) % n;
14 if (x == 1) return false;
15 if (x == n - 1) { ok = true; break; }
16 }
17 if (!ok) return false;
18 } return true; }
19// vim: cc=60 ts=2 sts=2 sw=2:

Callers 1

testFunction · 0.85

Calls 1

mod_powFunction · 0.85

Tested by 1

testFunction · 0.68