forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShorAlgorithm.java
57 lines (48 loc) · 1.93 KB
/
ShorAlgorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.thealgorithms.others;
import java.math.BigInteger;
import java.util.Random;
// The algorithm is referred from
// https://www.geeksforgeeks.org/shors-factorization-algorithm/
public class ShorAlgorithm {
// trying to find the order of exponent given the base and the number
private int exponent(BigInteger base, BigInteger number) {
BigInteger result = BigInteger.ONE;
int increment = 0;
while (!result.equals(BigInteger.ONE) || increment == 0) {
result = result.multiply(base).mod(number);
increment++;
}
return increment;
}
// implementing the shor algorithm
public BigInteger[] shorAlgorithm(BigInteger number) {
if (number.mod(new BigInteger("2")).equals(BigInteger.ZERO)) {
BigInteger p = number.divide(new BigInteger("2"));
BigInteger q = new BigInteger("2");
return new BigInteger[] {p, q};
}
Random random = new Random();
BigInteger base = BigInteger.ZERO;
do {
base = new BigInteger(number.bitLength(), random);
} while (base.compareTo(BigInteger.ZERO) <= 0 || base.compareTo(number) >= 0);
BigInteger hcf = base.gcd(number);
if (hcf.compareTo(BigInteger.ONE) > 0) {
return new BigInteger[] {hcf, number.divide(hcf)};
}
int result = exponent(base, number);
if (result % 2 != 0) {
return null;
}
BigInteger congruentResult = base.modPow(BigInteger.valueOf(result / 2), number);
if (congruentResult.equals(number.subtract(BigInteger.ONE))) {
return null;
}
BigInteger p = congruentResult.add(BigInteger.ONE).gcd(number);
BigInteger q = congruentResult.subtract(BigInteger.ONE).gcd(number);
if (!p.equals(BigInteger.ONE) && !q.equals(BigInteger.ONE)) {
return new BigInteger[] {p, q};
}
return null;
}
}