summaryrefslogtreecommitdiff
path: root/math/millerRabin.cpp
diff options
context:
space:
mode:
authorPaul Jungeblut <paul.jungeblut@gmail.com>2016-02-14 00:36:49 +0100
committerPaul Jungeblut <paul.jungeblut@gmail.com>2016-02-14 00:36:49 +0100
commit0545e74aeab679ff67e95b62f788a79d6a31f222 (patch)
tree86b1edb1ae6c9aa603b36e5c84a4ae7c2b3324f0 /math/millerRabin.cpp
parent2f428f1415fcbd3700def0f513b30a4818b6e39d (diff)
Adding Jojo's Miller-Rabin code to the document and improving prime sieve.
Diffstat (limited to 'math/millerRabin.cpp')
-rw-r--r--math/millerRabin.cpp19
1 files changed, 19 insertions, 0 deletions
diff --git a/math/millerRabin.cpp b/math/millerRabin.cpp
new file mode 100644
index 0000000..d41f33a
--- /dev/null
+++ b/math/millerRabin.cpp
@@ -0,0 +1,19 @@
+// Theoretisch: n < 318,665,857,834,031,151,167,461 (> 10^23)
+// Praktisch: n <= 10^18 (long long)
+// Laufzeit: O(log n)
+bool isPrime(ll n) {
+ if(n == 2) return true;
+ if(n < 2 || n % 2 == 0) return false;
+ ll d=n-1,j=0;
+ while(d % 2 == 0) d >>= 1, j++;
+ for(int a = 2; a <= min((ll)37, n-1); a++) {
+ ll v = pow_mod(a, d, n);
+ if(v == 1 || v == n-1) continue;
+ for(int i = 1; i <= j; i++) {
+ v = mult_mod(v, v, n);
+ if(v == n-1 || v <= 1) break;
+ }
+ if(v != n-1) return false;
+ }
+ return true;
+}