Skip to content

Commit 2181dfc

Browse files
committed
Revert "Formatted code"
This reverts commit b3916f1.
1 parent b3916f1 commit 2181dfc

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

Diff for: Maths/MillerRabin.js

+42-42
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
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) => {
12-
base = BigInt(base);
13-
exp = BigInt(exp);
14-
mod = BigInt(mod);
12+
base = BigInt(base)
13+
exp = BigInt(exp)
14+
mod = BigInt(mod)
1515

16-
let result = BigInt(1);
17-
base %= mod;
18-
while (exp) {
19-
if (exp & 1n) {
20-
result = (result * base) % mod;
16+
let result = BigInt(1)
17+
base %= mod
18+
while(exp){
19+
if (exp & 1n){
20+
result = (result * base) % mod
2121
}
22-
base = (base * base) % mod;
23-
exp = exp >> 1n;
22+
base = (base * base) % mod
23+
exp = exp >> 1n
2424
}
25-
return result;
26-
};
25+
return result
26+
}
2727

2828
// Check if number is composite
2929
const checkComposite = (n, a, d, s) => {
30-
let x = binaryExponentiation(a, d, n);
31-
if (x == 1n || x == n - 1n) {
32-
return false;
30+
let x = binaryExponentiation(a, d, n)
31+
if (x == 1n || x == (n - 1n)){
32+
return false
3333
}
3434

35-
for (let r = 1; r < s; r++) {
36-
x = (x * x) % n;
37-
if (x == n - 1n) {
38-
return false;
35+
for (let r = 1; r < s; r++){
36+
x = (x * x) % n
37+
if (x == n - 1n){
38+
return false
3939
}
4040
}
41-
return true;
42-
};
41+
return true
42+
}
4343

4444
// Miller Rabin Primality Test
4545
export const millerRabin = (n) => {
46-
n = BigInt(n);
46+
n = BigInt(n)
4747

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

52-
let s = 0n;
53-
let d = n - 1n;
54-
while ((d & 1n) == 0) {
55-
d = d >> 1n;
52+
let s = 0n
53+
let d = n - 1n
54+
while((d & 1n) == 0){
55+
d = d >> 1n
5656
s++;
5757
}
5858

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

0 commit comments

Comments
 (0)