3
3
import java .math .BigInteger ;
4
4
import java .util .Random ;
5
5
6
- //The algorithm is referred from
7
- //https://www.geeksforgeeks.org/shors-factorization-algorithm/
6
+ // The algorithm is referred from
7
+ // https://www.geeksforgeeks.org/shors-factorization-algorithm/
8
8
public class ShorAlgorithm {
9
- //trying to find the order of exponent given the base and the number
9
+ // trying to find the order of exponent given the base and the number
10
10
private int exponent (BigInteger base , BigInteger number ) {
11
11
BigInteger result = BigInteger .ONE ;
12
12
int increment = 0 ;
@@ -17,9 +17,9 @@ private int exponent(BigInteger base, BigInteger number) {
17
17
return increment ;
18
18
}
19
19
20
- //implementing the shor algorithm
20
+ // implementing the shor algorithm
21
21
public BigInteger [] shorAlgorithm (BigInteger number ) {
22
- if (number .mod (new BigInteger ("2" )).equals (BigInteger .ZERO )) {
22
+ if (number .mod (new BigInteger ("2" )).equals (BigInteger .ZERO )) {
23
23
BigInteger p = number .divide (new BigInteger ("2" ));
24
24
BigInteger q = new BigInteger ("2" );
25
25
return new BigInteger []{p , q };
@@ -32,20 +32,20 @@ public BigInteger[] shorAlgorithm(BigInteger number) {
32
32
} while (base .compareTo (BigInteger .ZERO ) <= 0 || base .compareTo (number ) >= 0 );
33
33
34
34
BigInteger hcf = base .gcd (number );
35
- if (hcf .compareTo (BigInteger .ONE ) > 0 ) {
35
+ if (hcf .compareTo (BigInteger .ONE ) > 0 ) {
36
36
return new BigInteger []{hcf , number .divide (hcf )};
37
37
}
38
38
39
39
int result = exponent (base , number );
40
- if (result % 2 != 0 ) return null ;
40
+ if (result % 2 != 0 ) return null ;
41
41
42
- BigInteger congruentResult = base .modPow (BigInteger .valueOf (result / 2 ), number );
43
- if (congruentResult .equals (number .subtract (BigInteger .ONE ))) return null ;
42
+ BigInteger congruentResult = base .modPow (BigInteger .valueOf (result / 2 ), number );
43
+ if (congruentResult .equals (number .subtract (BigInteger .ONE ))) return null ;
44
44
45
45
BigInteger p = congruentResult .add (BigInteger .ONE ).gcd (number );
46
46
BigInteger q = congruentResult .subtract (BigInteger .ONE ).gcd (number );
47
47
48
- if (!p .equals (BigInteger .ONE ) && !q .equals (BigInteger .ONE ))
48
+ if (!p .equals (BigInteger .ONE ) && !q .equals (BigInteger .ONE ))
49
49
return new BigInteger []{p , q };
50
50
return null ;
51
51
}
0 commit comments