Skip to content

Create Miller-Rabin_primalityTest.cpp #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Miller-Rabin_primalityTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

bool miller_rabin(long long n, int k=5) {
// Check some basic primality conditions
if (n < 2) {
return false;
}
if (n == 2 || n == 3) {
return true;
}
if (n % 2 == 0) {
return false;
}

// Write n-1 as d * 2^r, where d is odd
long long d = n - 1;
int r = 0;
while (d % 2 == 0) {
d /= 2;
r++;
}

// Perform the Miller-Rabin test k times
for (int i = 0; i < k; i++) {
// Choose a random witness a in the range [2, n-2]
long long a = rand() % (n - 3) + 2;
long long x = pow(a, d) % n;

// Check whether a is a witness for the compositeness of n
if (x == 1 || x == n - 1) {
continue;
}
for (int j = 0; j < r - 1; j++) {
x = (x * x) % n;
if (x == n - 1) {
break;
}
}
if (x != n - 1) {
return false;
}
}
return true;
}