Skip to content

Commit

Permalink
Clang Attemp
Browse files Browse the repository at this point in the history
  • Loading branch information
joelkurien committed Oct 31, 2024
1 parent 0b23d6b commit 07700c5
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/main/java/com/thealgorithms/others/ShorAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import java.math.BigInteger;
import java.util.Random;

//The algorithm is referred from
//https://www.geeksforgeeks.org/shors-factorization-algorithm/
// 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
// 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;
Expand All @@ -17,9 +17,9 @@ private int exponent(BigInteger base, BigInteger number) {
return increment;
}

//implementing the shor algorithm
// implementing the shor algorithm
public BigInteger[] shorAlgorithm(BigInteger number) {
if(number.mod(new BigInteger("2")).equals(BigInteger.ZERO)) {
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};
Expand All @@ -32,20 +32,20 @@ public BigInteger[] shorAlgorithm(BigInteger number) {
} while (base.compareTo(BigInteger.ZERO) <= 0 || base.compareTo(number) >= 0);

BigInteger hcf = base.gcd(number);
if(hcf.compareTo(BigInteger.ONE) > 0) {
if (hcf.compareTo(BigInteger.ONE) > 0) {
return new BigInteger[]{hcf, number.divide(hcf)};
}

int result = exponent(base, number);
if(result % 2 != 0) return null;
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 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))
if (!p.equals(BigInteger.ONE) && !q.equals(BigInteger.ONE))
return new BigInteger[]{p, q};
return null;
}
Expand Down

0 comments on commit 07700c5

Please sign in to comment.