Content-Length: 391895 | pFad | http://github.com/TheAlgorithms/JavaScript/pull/1321/files/cdc62830f90e47c121877b2d464b59642089980a

AA Added Miller Rabin Primality Test by sundaram123krishnan · Pull Request #1321 · TheAlgorithms/JavaScript · GitHub
Skip to content

Added Miller Rabin Primality Test #1321

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 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
16 changes: 16 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "test",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"label": "npm: test",
"detail": "jest"
}
]
}
69 changes: 69 additions & 0 deletions Maths/MilerRabinPrimalityTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Author: Sundaram krishnan (https://github.com/sundaram123krishnan)
* Miller–Rabin primality test : https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#:~:text=The%20Miller%E2%80%93Rabin%20primality%20test,the%20Solovay%E2%80%93Strassen%20primality%20test.
* Used to check whether given number is probably prime or not
* return true if the number is probably a prime
* else false
*/

/**
* @function MillerRabin
* @description -> Checking if a number is probably prime or not
* @param {number} number
* @returns {boolean} true if the number is prime, otherwise false
*/

const power = (base, exponent, modulus) => {
let result = 1
base %= modulus

while (exponent) {
if (exponent & 1) {
result = (result * base) % modulus
}
base = (base * base) % modulus
exponent >>= 1
}
return result
}

const checkComposite = (n, a, d, s) => {
let x = power(a, d, n)

if (x === 1 || x === n - 1) {
return false
}

for (let r = 1; r < s; r++) {
x = (x * x) % n
if (x === n - 1) {
return false
}
}
return true
}

const MillerRabin = (n, iter = 5) => {
if (n < 4) {
return n === 2 || n === 3
}

let s = 0

let d = n - 1

while ((d & 1) === 0) {
d >>= 1
s++
}

for (let i = 0; i < iter; i++) {
const a = 2 + (Math.floor(Math.random() * (n - 2)) % (n - 4))
if (checkComposite(n, a, d, s)) {
return false
}
}
return true
}

export { MillerRabin }
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"dependencies": {
"@babel/core": "^7.19.3",
"@babel/plugin-transform-runtime": "^7.19.1",
"@babel/preset-env": "^7.19.4"
"@babel/preset-env": "^7.19.4",
"format": "^0.2.2"
},
"devDependencies": {
"@babel/eslint-parser": "^7.19.1",
Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/TheAlgorithms/JavaScript/pull/1321/files/cdc62830f90e47c121877b2d464b59642089980a

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy