Skip to content

Commit d856047

Browse files
committed
Added test cases, removed console prints
1 parent 42859b7 commit d856047

File tree

2 files changed

+77
-13
lines changed

2 files changed

+77
-13
lines changed

src/main/java/com/thealgorithms/others/ShorAlgorithm.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,50 @@
33
import java.math.BigInteger;
44
import java.util.Random;
55

6+
//The algorithm is referred from
7+
//https://www.geeksforgeeks.org/shors-factorization-algorithm/
68
public class ShorAlgorithm {
79
//trying to find the order of exponent given the base and the number
8-
private int exponent(BigInteger base, BigInteger number){
10+
private int exponent(BigInteger base, BigInteger number) {
911
BigInteger result = BigInteger.ONE;
1012
int increment = 0;
11-
while (!result.equals(BigInteger.ONE) || increment == 0){
13+
while (!result.equals(BigInteger.ONE) || increment == 0) {
1214
result = result.multiply(base).mod(number);
1315
increment++;
1416
}
1517
return increment;
1618
}
1719

1820
//implementing the shor algorithm
19-
public void shorAlgorithm(BigInteger number){
20-
if(number.mod(new BigInteger("2")).equals(BigInteger.ZERO)){
21+
public BigInteger[] shorAlgorithm(BigInteger number) {
22+
if(number.mod(new BigInteger("2")).equals(BigInteger.ZERO)) {
2123
BigInteger p = number.divide(new BigInteger("2"));
2224
BigInteger q = new BigInteger("2");
23-
System.out.println("p: "+ p + " q: " + q);
24-
return;
25+
return new BigInteger[]{p, q};
2526
}
2627

2728
Random random = new Random();
2829
BigInteger base = BigInteger.ZERO;
29-
do{
30+
do {
3031
base = new BigInteger(number.bitLength(), random);
3132
} while (base.compareTo(BigInteger.ZERO) <= 0 || base.compareTo(number) >= 0);
3233

3334
BigInteger hcf = base.gcd(number);
34-
if(hcf.compareTo(BigInteger.ONE) > 0){
35-
System.out.println("p: "+ hcf + " q: " + number.divide(hcf));
36-
return;
35+
if(hcf.compareTo(BigInteger.ONE) > 0) {
36+
return new BigInteger[]{hcf, number.divide(hcf)};
3737
}
3838

3939
int result = exponent(base, number);
40-
if(result % 2 != 0) return;
40+
if(result % 2 != 0) return null;
4141

4242
BigInteger congruentResult = base.modPow(BigInteger.valueOf(result/2), number);
43-
if(congruentResult.equals(number.subtract(BigInteger.ONE))) return;
43+
if(congruentResult.equals(number.subtract(BigInteger.ONE))) return null;
4444

4545
BigInteger p = congruentResult.add(BigInteger.ONE).gcd(number);
4646
BigInteger q = congruentResult.subtract(BigInteger.ONE).gcd(number);
4747

4848
if(!p.equals(BigInteger.ONE) && !q.equals(BigInteger.ONE))
49-
System.out.println("p: "+ p + " q: " + q);
49+
return new BigInteger[]{p, q};
50+
return null;
5051
}
5152
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.thealgorithms.others;
2+
3+
import org.junit.jupiter.api.Test;
4+
import java.math.BigInteger;
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class ShorAlgorithmTest {
8+
9+
@Test
10+
public void testFactorizationOfEvenNumber() {
11+
ShorAlgorithm shor = new ShorAlgorithm();
12+
BigInteger number = new BigInteger("15");
13+
BigInteger[] factors = shor.shorAlgorithm(number);
14+
15+
assertNotNull(factors, "Factors should not be null for composite numbers.");
16+
assertEquals(2, factors.length, "There should be two factors.");
17+
18+
BigInteger p = factors[0];
19+
BigInteger q = factors[1];
20+
21+
assertEquals(number, p.multiply(q), "Factors should multiply to the original number.");
22+
}
23+
24+
@Test
25+
public void testFactorizationOfPrimeNumber() {
26+
ShorAlgorithm shor = new ShorAlgorithm();
27+
BigInteger number = new BigInteger("13");
28+
BigInteger[] factors = shor.shorAlgorithm(number);
29+
30+
assertNull(factors, "Factors should be null for prime numbers.");
31+
}
32+
33+
@Test
34+
public void testFactorizationOfEvenCompositeNumber() {
35+
ShorAlgorithm shor = new ShorAlgorithm();
36+
BigInteger number = new BigInteger("20");
37+
BigInteger[] factors = shor.shorAlgorithm(number);
38+
39+
assertNotNull(factors, "Factors should not be null for composite numbers.");
40+
assertEquals(2, factors.length, "There should be two factors.");
41+
42+
BigInteger p = factors[0];
43+
BigInteger q = factors[1];
44+
45+
assertEquals(number, p.multiply(q), "Factors should multiply to the original number.");
46+
}
47+
48+
@Test
49+
public void testLargeCompositeNumber() {
50+
ShorAlgorithm shor = new ShorAlgorithm();
51+
BigInteger number = new BigInteger("21");
52+
BigInteger[] factors = shor.shorAlgorithm(number);
53+
54+
assertNotNull(factors, "Factors should not be null for composite numbers.");
55+
assertEquals(2, factors.length, "There should be two factors.");
56+
57+
BigInteger p = factors[0];
58+
BigInteger q = factors[1];
59+
60+
assertEquals(number, p.multiply(q), "Factors should multiply to the original number.");
61+
}
62+
}
63+

0 commit comments

Comments
 (0)