|
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 |
10 |
| - private int exponent(BigInteger base, BigInteger number) { |
11 |
| - BigInteger result = BigInteger.ONE; |
12 |
| - int increment = 0; |
13 |
| - while (!result.equals(BigInteger.ONE) || increment == 0) { |
14 |
| - result = result.multiply(base).mod(number); |
15 |
| - increment++; |
16 |
| - } |
17 |
| - return increment; |
| 9 | + // trying to find the order of exponent given the base and the number |
| 10 | + private int exponent(BigInteger base, BigInteger number) { |
| 11 | + BigInteger result = BigInteger.ONE; |
| 12 | + int increment = 0; |
| 13 | + while (!result.equals(BigInteger.ONE) || increment == 0) { |
| 14 | + result = result.multiply(base).mod(number); |
| 15 | + increment++; |
18 | 16 | }
|
| 17 | + return increment; |
| 18 | + } |
| 19 | + |
| 20 | + // implementing the shor algorithm |
| 21 | + public BigInteger[] shorAlgorithm(BigInteger number) { |
| 22 | + if (number.mod(new BigInteger("2")).equals(BigInteger.ZERO)) { |
| 23 | + BigInteger p = number.divide(new BigInteger("2")); |
| 24 | + BigInteger q = new BigInteger("2"); |
| 25 | + return new BigInteger[] {p, q}; |
| 26 | + } |
| 27 | + |
| 28 | + Random random = new Random(); |
| 29 | + BigInteger base = BigInteger.ZERO; |
| 30 | + do { |
| 31 | + base = new BigInteger(number.bitLength(), random); |
| 32 | + } while (base.compareTo(BigInteger.ZERO) <= 0 || |
| 33 | + base.compareTo(number) >= 0); |
19 | 34 |
|
20 |
| - //implementing the shor algorithm |
21 |
| - public BigInteger[] shorAlgorithm(BigInteger number) { |
22 |
| - if(number.mod(new BigInteger("2")).equals(BigInteger.ZERO)) { |
23 |
| - BigInteger p = number.divide(new BigInteger("2")); |
24 |
| - BigInteger q = new BigInteger("2"); |
25 |
| - return new BigInteger[]{p, q}; |
26 |
| - } |
27 |
| - |
28 |
| - Random random = new Random(); |
29 |
| - BigInteger base = BigInteger.ZERO; |
30 |
| - do { |
31 |
| - base = new BigInteger(number.bitLength(), random); |
32 |
| - } while (base.compareTo(BigInteger.ZERO) <= 0 || base.compareTo(number) >= 0); |
33 |
| - |
34 |
| - BigInteger hcf = base.gcd(number); |
35 |
| - if(hcf.compareTo(BigInteger.ONE) > 0) { |
36 |
| - return new BigInteger[]{hcf, number.divide(hcf)}; |
37 |
| - } |
38 |
| - |
39 |
| - int result = exponent(base, number); |
40 |
| - if(result % 2 != 0) return null; |
41 |
| - |
42 |
| - BigInteger congruentResult = base.modPow(BigInteger.valueOf(result/2), number); |
43 |
| - if(congruentResult.equals(number.subtract(BigInteger.ONE))) return null; |
44 |
| - |
45 |
| - BigInteger p = congruentResult.add(BigInteger.ONE).gcd(number); |
46 |
| - BigInteger q = congruentResult.subtract(BigInteger.ONE).gcd(number); |
47 |
| - |
48 |
| - if(!p.equals(BigInteger.ONE) && !q.equals(BigInteger.ONE)) |
49 |
| - return new BigInteger[]{p, q}; |
50 |
| - return null; |
| 35 | + BigInteger hcf = base.gcd(number); |
| 36 | + if (hcf.compareTo(BigInteger.ONE) > 0) { |
| 37 | + return new BigInteger[] {hcf, number.divide(hcf)}; |
51 | 38 | }
|
| 39 | + |
| 40 | + int result = exponent(base, number); |
| 41 | + if (result % 2 != 0) |
| 42 | + return null; |
| 43 | + |
| 44 | + BigInteger congruentResult = |
| 45 | + base.modPow(BigInteger.valueOf(result / 2), number); |
| 46 | + if (congruentResult.equals(number.subtract(BigInteger.ONE))) |
| 47 | + return null; |
| 48 | + |
| 49 | + BigInteger p = congruentResult.add(BigInteger.ONE).gcd(number); |
| 50 | + BigInteger q = congruentResult.subtract(BigInteger.ONE).gcd(number); |
| 51 | + |
| 52 | + if (!p.equals(BigInteger.ONE) && !q.equals(BigInteger.ONE)) |
| 53 | + return new BigInteger[] {p, q}; |
| 54 | + return null; |
| 55 | + } |
52 | 56 | }
|
0 commit comments