File tree 2 files changed +13
-5
lines changed
main/java/com/thealgorithms/others
test/java/com/thealgorithms/others
2 files changed +13
-5
lines changed Original file line number Diff line number Diff line change 3
3
import java .math .BigInteger ;
4
4
import java .util .Random ;
5
5
6
- // The algorithm is referred from
6
+ // The algorithm is referred from
7
7
// https://www.geeksforgeeks.org/shors-factorization-algorithm/
8
8
public class ShorAlgorithm {
9
9
// trying to find the order of exponent given the base and the number
@@ -37,15 +37,21 @@ public BigInteger[] shorAlgorithm(BigInteger number) {
37
37
}
38
38
39
39
int result = exponent (base , number );
40
- if (result % 2 != 0 ) return null ;
40
+ if (result % 2 != 0 ) {
41
+ return null ;
42
+ }
41
43
42
44
BigInteger congruentResult = base .modPow (BigInteger .valueOf (result / 2 ), number );
43
- if (congruentResult .equals (number .subtract (BigInteger .ONE ))) return null ;
45
+ if (congruentResult .equals (number .subtract (BigInteger .ONE ))) {
46
+ return null ;
47
+ }
44
48
45
49
BigInteger p = congruentResult .add (BigInteger .ONE ).gcd (number );
46
50
BigInteger q = congruentResult .subtract (BigInteger .ONE ).gcd (number );
47
51
48
- if (!p .equals (BigInteger .ONE ) && !q .equals (BigInteger .ONE )) return new BigInteger [] {p , q };
52
+ if (!p .equals (BigInteger .ONE ) && !q .equals (BigInteger .ONE )) {
53
+ return new BigInteger [] {p , q };
54
+ }
49
55
return null ;
50
56
}
51
57
}
Original file line number Diff line number Diff line change 2
2
3
3
import java .math .BigInteger ;
4
4
import org .junit .jupiter .api .Test ;
5
- import static org .junit .jupiter .api .Assertions .*;
5
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
6
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
7
+ import static org .junit .jupiter .api .Assertions .assertNull ;
6
8
7
9
public class ShorAlgorithmTest {
8
10
You can’t perform that action at this time.
0 commit comments