summaryrefslogtreecommitdiff
path: root/math/millerRabin.cpp
diff options
context:
space:
mode:
authormzuenni <michi.zuendorf@gmail.com>2022-06-27 17:19:28 +0200
committermzuenni <michi.zuendorf@gmail.com>2022-06-27 17:19:28 +0200
commit5ab8a5088b729a9953b8dff1b2a985dc8fb2098b (patch)
treeed40d6936c0e9eee40ba62751cbf99ecddbaddc2 /math/millerRabin.cpp
parentadabbad9c51cf7cd3874bfde8eac1fbcf84fec10 (diff)
updated tcr
Diffstat (limited to 'math/millerRabin.cpp')
-rw-r--r--math/millerRabin.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/math/millerRabin.cpp b/math/millerRabin.cpp
new file mode 100644
index 0000000..fc9385a
--- /dev/null
+++ b/math/millerRabin.cpp
@@ -0,0 +1,20 @@
+constexpr ll bases32[] = {2, 7, 61};
+constexpr ll bases64[] = {2, 325, 9375, 28178, 450775,
+ 9780504, 1795265022};
+
+bool isPrime(ll n) {
+ if(n < 2 || n % 2 == 0) return n == 2;
+ ll d = n - 1, j = 0;
+ while(d % 2 == 0) d /= 2, j++;
+ for(ll a : bases64) {
+ if (a % n == 0) continue;
+ ll v = powMod(a, d, n);
+ if(v == 1 || v == n - 1) continue;
+ for(ll i = 1; i <= j; i++) {
+ v = (v * v) % n; //mulmod or int128
+ if(v == n - 1 || v <= 1) break;
+ }
+ if(v != n - 1) return false;
+ }
+ return true;
+}