Skip to content

Added Miller-Rabin primality test #225

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

Merged
merged 1 commit into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"number_theory": {
"list": {
"euclidean_algorithm": "Euclidean Algorithm",
"sieve_of_eratosthenes": "Sieve of Eratosthenes"
"sieve_of_eratosthenes": "Sieve of Eratosthenes",
"miller_rabin_primality_test": "Miller-Rabin primality test"
},
"name": "Number Theory"
},
Expand Down
91 changes: 91 additions & 0 deletions algorithm/number_theory/miller_rabin_primality_test/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Utility function to do modular exponentiation.
// It returns (x^y) % p
function power(x, y, p)
{
var res = 1;
x = x % p;
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1) res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}


/**
* Determine if N is prime using Miller-Rabin probabilistic algorithm
* @param {Number} n The number
* @param {Number} k An integer that determine the accuracy of the solution
* @return {Boolean}
*/
function testProbablyPrime(n, k) {
logger._print("==> Testing number " + n);

if (n === 1 || n === 3) {
logger._print("==> Simple case, N is 1 or 3");
return true;
}
if (n % 2 === 0) {
logger._print("==> Simple case, " + n + " mod 2 = 0");
return false;
}

// Write (n - 1) as 2^s * d
var d = n-1;
while (d % 2 === 0) {
d /= 2;
}
logger._print("d = " + d);

// Do 5 iterations if none supplied
k = k || 5;
var P = 100 * (1 - (1/Math.pow(4, k)));

WitnessLoop: do {
logger._print("Remaining iterations: #" + k);

var a = 2 + Math.floor(Math.random() * (n - 4));
logger._print("--> first test with random = " + a);

// Compute a^d % n
var x = power(a, d, n);

if (x === 1 || x === n - 1) {
logger._print("--> continue WitnessLoop, x = 1 or x = n-1");
continue;
}

logger._print("--> second test");

// Keep squaring x while one of the following doesn't
// happen
// (i) d does not reach n-1
// (ii) (x^2) % n is not 1
// (iii) (x^2) % n is not n-1
var i = d;
while (i != n-1) {
x = (x * x) % n;
i *= 2;

if (x == 1) {
logger._print("--> exiting, " + n + " is composite");
return false;
}

if (x == n-1) {
logger._print("--> continue WitnessLoop");
continue WitnessLoop;
}
}

logger._print("--> exiting, " + n + " is composite 'cause (n-1) is reached");
return false;

} while (--k);

logger._print("End of tests, " + n + " is probably prime with probabilty of " + P + "%");
return true;
}
18 changes: 18 additions & 0 deletions algorithm/number_theory/miller_rabin_primality_test/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var logger = new LogTracer();

var a = Math.floor(Math.random()*300); if (a % 2 === 0) a += 1;
testProbablyPrime(a);
logger._print("----------");

var a = Math.floor(Math.random()*300); if (a % 2 === 0) a += 1;
testProbablyPrime(a);
logger._print("----------");

var a = Math.floor(Math.random()*300); if (a % 2 === 0) a += 1;
testProbablyPrime(a);
logger._print("----------");

testProbablyPrime(151);
logger._print("----------");

testProbablyPrime(199, 10);
13 changes: 13 additions & 0 deletions algorithm/number_theory/miller_rabin_primality_test/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Miller-Rabin primality test": "Determines whether a given number is prime",
"Complexity": {
"time": "$O(klog^{3}(n)))$",
"probability": "$1 - (1/(4^{k}))$"
},
"References": [
"<a href='https://www.wikiwand.com/en/Miller%E2%80%93Rabin_primality_test'>Wikipedia</a>"
],
"files": {
"basic": "Miller Rabin primality test"
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy