Skip to content

Commit dcb2a73

Browse files
committed
Formatted code
1 parent 2181dfc commit dcb2a73

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

Diff for: Maths/MillerRabin.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
2-
* @function millerRabin
3-
* @description Check if number is prime or not (accurate for 64-bit integers)
4-
* @param {Integer} n
5-
* @returns {Boolean} true if prime, false otherwise
6-
* @url https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
7-
* note: Here we are using BigInt to handle large numbers
8-
**/
2+
* @function millerRabin
3+
* @description Check if number is prime or not (accurate for 64-bit integers)
4+
* @param {Integer} n
5+
* @returns {Boolean} true if prime, false otherwise
6+
* @url https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
7+
* note: Here we are using BigInt to handle large numbers
8+
**/
99

1010
// Modular Binary Exponentiation (Iterative)
1111
const binaryExponentiation = (base, exp, mod) => {
@@ -15,8 +15,8 @@ const binaryExponentiation = (base, exp, mod) => {
1515

1616
let result = BigInt(1)
1717
base %= mod
18-
while(exp){
19-
if (exp & 1n){
18+
while (exp) {
19+
if (exp & 1n) {
2020
result = (result * base) % mod
2121
}
2222
base = (base * base) % mod
@@ -28,13 +28,13 @@ const binaryExponentiation = (base, exp, mod) => {
2828
// Check if number is composite
2929
const checkComposite = (n, a, d, s) => {
3030
let x = binaryExponentiation(a, d, n)
31-
if (x == 1n || x == (n - 1n)){
31+
if (x == 1n || x == n - 1n) {
3232
return false
3333
}
3434

35-
for (let r = 1; r < s; r++){
35+
for (let r = 1; r < s; r++) {
3636
x = (x * x) % n
37-
if (x == n - 1n){
37+
if (x == n - 1n) {
3838
return false
3939
}
4040
}
@@ -45,26 +45,26 @@ const checkComposite = (n, a, d, s) => {
4545
export const millerRabin = (n) => {
4646
n = BigInt(n)
4747

48-
if (n < 2){
48+
if (n < 2) {
4949
return false
5050
}
5151

5252
let s = 0n
5353
let d = n - 1n
54-
while((d & 1n) == 0){
54+
while ((d & 1n) == 0) {
5555
d = d >> 1n
56-
s++;
56+
s++
5757
}
5858

5959
// Only first 12 primes are needed to be check to find primality of any 64-bit integer
6060
let prime = [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n, 37n]
61-
for(let a of prime){
62-
if (n == a){
61+
for (let a of prime) {
62+
if (n == a) {
6363
return true
6464
}
65-
if (checkComposite(n, a, d, s)){
65+
if (checkComposite(n, a, d, s)) {
6666
return false
6767
}
6868
}
69-
return true;
69+
return true
7070
}

0 commit comments

Comments
 (0)