Skip to content
Prev Previous commit
Next Next commit
modified with proper indentaion and code quality
  • Loading branch information
sundaram123krishnan committed May 4, 2023
commit 8d922c7563935f8fc7803856d02e74f12e2ef9e8
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,56 @@
* @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
if (exponent & 1) {
result = (result * base) % modulus
}
base = (base * base) % modulus
exponent >>= 1
}
return result
}
const checkComposite = (n, a, d, s) => {
}

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

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


for (let r = 1; r < s; r++) {
x = (x * x) % n
if (x == n - 1) {
return false
}
x = (x * x) % n
if (x == n - 1) {
return false
}
}
return true
}
const MillerRabin = (n, iter = 5) => {
}

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

let s = 0

let d = n - 1

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

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

}