Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb3916f1

Browse files
committed
Formatted code
1 parent7997eec commitb3916f1

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

‎Maths/MillerRabin.js

Lines changed: 42 additions & 42 deletions
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
constbinaryExponentiation=(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-
letresult=BigInt(1)
17-
base%=mod
18-
while(exp){
19-
if(exp&1n){
20-
result=(result*base)%mod
16+
letresult=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-
returnresult
26-
}
25+
returnresult;
26+
};
2727

2828
// Check if number is composite
2929
constcheckComposite=(n,a,d,s)=>{
30-
letx=binaryExponentiation(a,d,n)
31-
if(x==1n||x==(n-1n)){
32-
returnfalse
30+
letx=binaryExponentiation(a,d,n);
31+
if(x==1n||x==n-1n){
32+
returnfalse;
3333
}
3434

35-
for(letr=1;r<s;r++){
36-
x=(x*x)%n
37-
if(x==n-1n){
38-
returnfalse
35+
for(letr=1;r<s;r++){
36+
x=(x*x)%n;
37+
if(x==n-1n){
38+
returnfalse;
3939
}
4040
}
41-
returntrue
42-
}
41+
returntrue;
42+
};
4343

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

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

52-
lets=0n
53-
letd=n-1n
54-
while((d&1n)==0){
55-
d=d>>1n
52+
lets=0n;
53+
letd=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-
letprime=[2n,3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n]
61-
for(letaofprime){
62-
if(n==a){
63-
returntrue
60+
letprime=[2n,3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n];
61+
for(letaofprime){
62+
if(n==a){
63+
returntrue;
6464
}
65-
if(checkComposite(n,a,d,s)){
66-
returnfalse
65+
if(checkComposite(n,a,d,s)){
66+
returnfalse;
6767
}
6868
}
6969
returntrue;
70-
}
70+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp