From 42859b7019466f0ca8c883e1a16ada707db39605 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Wed, 30 Oct 2024 20:26:33 +1100 Subject: [PATCH 01/12] Added Shor Algorithm --- .../thealgorithms/others/ShorAlgorithm.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/main/java/com/thealgorithms/others/ShorAlgorithm.java diff --git a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java new file mode 100644 index 000000000000..0883882e75c0 --- /dev/null +++ b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java @@ -0,0 +1,51 @@ +package com.thealgorithms.others; + +import java.math.BigInteger; +import java.util.Random; + +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 void shorAlgorithm(BigInteger number){ + if(number.mod(new BigInteger("2")).equals(BigInteger.ZERO)){ + BigInteger p = number.divide(new BigInteger("2")); + BigInteger q = new BigInteger("2"); + System.out.println("p: "+ p + " q: " + q); + return; + } + + 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){ + System.out.println("p: "+ hcf + " q: " + number.divide(hcf)); + return; + } + + int result = exponent(base, number); + if(result % 2 != 0) return; + + BigInteger congruentResult = base.modPow(BigInteger.valueOf(result/2), number); + if(congruentResult.equals(number.subtract(BigInteger.ONE))) return; + + 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)) + System.out.println("p: "+ p + " q: " + q); + } +} From d85604790711108ab6504906ae4d6389acc71bcb Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Thu, 31 Oct 2024 15:07:29 +1100 Subject: [PATCH 02/12] Added test cases, removed console prints --- .../thealgorithms/others/ShorAlgorithm.java | 27 ++++---- .../others/ShorAlgorithmTest.java | 63 +++++++++++++++++++ 2 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java diff --git a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java index 0883882e75c0..027a8bb14457 100644 --- a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java +++ b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java @@ -3,12 +3,14 @@ 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){ + private int exponent(BigInteger base, BigInteger number) { BigInteger result = BigInteger.ONE; int increment = 0; - while (!result.equals(BigInteger.ONE) || increment == 0){ + while (!result.equals(BigInteger.ONE) || increment == 0) { result = result.multiply(base).mod(number); increment++; } @@ -16,36 +18,35 @@ private int exponent(BigInteger base, BigInteger number){ } //implementing the shor algorithm - public void shorAlgorithm(BigInteger number){ - if(number.mod(new BigInteger("2")).equals(BigInteger.ZERO)){ + 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"); - System.out.println("p: "+ p + " q: " + q); - return; + return new BigInteger[]{p, q}; } Random random = new Random(); BigInteger base = BigInteger.ZERO; - do{ + 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){ - System.out.println("p: "+ hcf + " q: " + number.divide(hcf)); - return; + if(hcf.compareTo(BigInteger.ONE) > 0) { + return new BigInteger[]{hcf, number.divide(hcf)}; } int result = exponent(base, number); - if(result % 2 != 0) return; + if(result % 2 != 0) return null; BigInteger congruentResult = base.modPow(BigInteger.valueOf(result/2), number); - if(congruentResult.equals(number.subtract(BigInteger.ONE))) return; + 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)) - System.out.println("p: "+ p + " q: " + q); + return new BigInteger[]{p, q}; + return null; } } diff --git a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java new file mode 100644 index 000000000000..a7a94909051c --- /dev/null +++ b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java @@ -0,0 +1,63 @@ +package com.thealgorithms.others; + +import org.junit.jupiter.api.Test; +import java.math.BigInteger; +import static org.junit.jupiter.api.Assertions.*; + +public class ShorAlgorithmTest { + + @Test + public void testFactorizationOfEvenNumber() { + ShorAlgorithm shor = new ShorAlgorithm(); + BigInteger number = new BigInteger("15"); + BigInteger[] factors = shor.shorAlgorithm(number); + + assertNotNull(factors, "Factors should not be null for composite numbers."); + assertEquals(2, factors.length, "There should be two factors."); + + BigInteger p = factors[0]; + BigInteger q = factors[1]; + + assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); + } + + @Test + public void testFactorizationOfPrimeNumber() { + ShorAlgorithm shor = new ShorAlgorithm(); + BigInteger number = new BigInteger("13"); + BigInteger[] factors = shor.shorAlgorithm(number); + + assertNull(factors, "Factors should be null for prime numbers."); + } + + @Test + public void testFactorizationOfEvenCompositeNumber() { + ShorAlgorithm shor = new ShorAlgorithm(); + BigInteger number = new BigInteger("20"); + BigInteger[] factors = shor.shorAlgorithm(number); + + assertNotNull(factors, "Factors should not be null for composite numbers."); + assertEquals(2, factors.length, "There should be two factors."); + + BigInteger p = factors[0]; + BigInteger q = factors[1]; + + assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); + } + + @Test + public void testLargeCompositeNumber() { + ShorAlgorithm shor = new ShorAlgorithm(); + BigInteger number = new BigInteger("21"); + BigInteger[] factors = shor.shorAlgorithm(number); + + assertNotNull(factors, "Factors should not be null for composite numbers."); + assertEquals(2, factors.length, "There should be two factors."); + + BigInteger p = factors[0]; + BigInteger q = factors[1]; + + assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); + } +} + From 95b9724559c84536ef5f1cefb358a88edd6bb483 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Thu, 31 Oct 2024 15:12:04 +1100 Subject: [PATCH 03/12] Handled clang formatting --- .../thealgorithms/others/ShorAlgorithm.java | 88 ++++++++++--------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java index 027a8bb14457..05235cf0f1f9 100644 --- a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java +++ b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java @@ -3,50 +3,54 @@ 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 - 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; + // 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); - //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; + 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; + } } From 0b23d6bdb2b6c8599811a729ca7105c6c3d47a72 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Thu, 31 Oct 2024 15:16:56 +1100 Subject: [PATCH 04/12] Clang Attempt --- .../thealgorithms/others/ShorAlgorithm.java | 88 +++++++++---------- 1 file changed, 42 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java index 05235cf0f1f9..027a8bb14457 100644 --- a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java +++ b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java @@ -3,54 +3,50 @@ 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 - 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++; + //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; } - 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)}; + //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; } - - 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; - } } From 07700c57fd141a103b5ea6485ba5888957099688 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Thu, 31 Oct 2024 15:21:01 +1100 Subject: [PATCH 05/12] Clang Attemp --- .../thealgorithms/others/ShorAlgorithm.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java index 027a8bb14457..4782e586a673 100644 --- a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java +++ b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java @@ -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; @@ -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}; @@ -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; } From 7d4200e602fe87d563afa60f3d946201a728a6d6 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Thu, 31 Oct 2024 15:22:47 +1100 Subject: [PATCH 06/12] Clang Attemp --- src/main/java/com/thealgorithms/others/ShorAlgorithm.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java index 4782e586a673..a117cfa5173e 100644 --- a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java +++ b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java @@ -22,7 +22,7 @@ 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}; + return new BigInteger[] {p, q}; } Random random = new Random(); @@ -33,7 +33,7 @@ public BigInteger[] shorAlgorithm(BigInteger number) { BigInteger hcf = base.gcd(number); if (hcf.compareTo(BigInteger.ONE) > 0) { - return new BigInteger[]{hcf, number.divide(hcf)}; + return new BigInteger[] {hcf, number.divide(hcf)}; } int result = exponent(base, number); @@ -45,8 +45,7 @@ public BigInteger[] shorAlgorithm(BigInteger number) { 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}; + if (!p.equals(BigInteger.ONE) && !q.equals(BigInteger.ONE)) return new BigInteger[] {p, q}; return null; } } From 9f527a96a5cd21f3eb11369fa7464e7aa82f6a16 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Thu, 31 Oct 2024 15:30:46 +1100 Subject: [PATCH 07/12] Build Fix --- .../thealgorithms/others/ShorAlgorithmTest.java | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java index a7a94909051c..757b769dcef6 100644 --- a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java +++ b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java @@ -1,7 +1,7 @@ package com.thealgorithms.others; -import org.junit.jupiter.api.Test; import java.math.BigInteger; +import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ShorAlgorithmTest { @@ -44,20 +44,5 @@ public void testFactorizationOfEvenCompositeNumber() { assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); } - - @Test - public void testLargeCompositeNumber() { - ShorAlgorithm shor = new ShorAlgorithm(); - BigInteger number = new BigInteger("21"); - BigInteger[] factors = shor.shorAlgorithm(number); - - assertNotNull(factors, "Factors should not be null for composite numbers."); - assertEquals(2, factors.length, "There should be two factors."); - - BigInteger p = factors[0]; - BigInteger q = factors[1]; - - assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); - } } From 4dd6b30215c41c86fae2cd44ba2867ae8f840097 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Thu, 31 Oct 2024 15:37:19 +1100 Subject: [PATCH 08/12] Handled Clang and Build --- .../com/thealgorithms/others/ShorAlgorithm.java | 14 ++++++++++---- .../thealgorithms/others/ShorAlgorithmTest.java | 4 +++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java index a117cfa5173e..fdc6ae7a62e4 100644 --- a/src/main/java/com/thealgorithms/others/ShorAlgorithm.java +++ b/src/main/java/com/thealgorithms/others/ShorAlgorithm.java @@ -3,7 +3,7 @@ import java.math.BigInteger; import java.util.Random; -// The algorithm is referred from +// 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 @@ -37,15 +37,21 @@ public BigInteger[] shorAlgorithm(BigInteger number) { } 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; + 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}; + if (!p.equals(BigInteger.ONE) && !q.equals(BigInteger.ONE)) { + return new BigInteger[] {p, q}; + } return null; } } diff --git a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java index 757b769dcef6..0d43c9fae211 100644 --- a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java +++ b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java @@ -2,7 +2,9 @@ import java.math.BigInteger; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; public class ShorAlgorithmTest { From dcc5502e7c54bb075153fbd3ec1619d2aa698a7e Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Thu, 31 Oct 2024 15:43:39 +1100 Subject: [PATCH 09/12] Handled more testcase clang --- .../java/com/thealgorithms/others/ShorAlgorithmTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java index 0d43c9fae211..5f547aa4f662 100644 --- a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java +++ b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java @@ -1,13 +1,13 @@ package com.thealgorithms.others; -import java.math.BigInteger; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import java.math.BigInteger; +import org.junit.jupiter.api.Test; + public class ShorAlgorithmTest { - @Test public void testFactorizationOfEvenNumber() { ShorAlgorithm shor = new ShorAlgorithm(); @@ -16,7 +16,7 @@ public void testFactorizationOfEvenNumber() { assertNotNull(factors, "Factors should not be null for composite numbers."); assertEquals(2, factors.length, "There should be two factors."); - + BigInteger p = factors[0]; BigInteger q = factors[1]; From a531db07b2644ca68c676291cd6f398ea1cf1166 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Thu, 31 Oct 2024 16:42:28 +1100 Subject: [PATCH 10/12] Clang fixes for Test file --- .../thealgorithms/others/ShorAlgorithmTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java index 5f547aa4f662..ab388feb4c96 100644 --- a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java +++ b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java @@ -13,13 +13,13 @@ public void testFactorizationOfEvenNumber() { ShorAlgorithm shor = new ShorAlgorithm(); BigInteger number = new BigInteger("15"); BigInteger[] factors = shor.shorAlgorithm(number); - + assertNotNull(factors, "Factors should not be null for composite numbers."); assertEquals(2, factors.length, "There should be two factors."); - + BigInteger p = factors[0]; BigInteger q = factors[1]; - + assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); } @@ -28,7 +28,7 @@ public void testFactorizationOfPrimeNumber() { ShorAlgorithm shor = new ShorAlgorithm(); BigInteger number = new BigInteger("13"); BigInteger[] factors = shor.shorAlgorithm(number); - + assertNull(factors, "Factors should be null for prime numbers."); } @@ -37,13 +37,13 @@ public void testFactorizationOfEvenCompositeNumber() { ShorAlgorithm shor = new ShorAlgorithm(); BigInteger number = new BigInteger("20"); BigInteger[] factors = shor.shorAlgorithm(number); - + assertNotNull(factors, "Factors should not be null for composite numbers."); assertEquals(2, factors.length, "There should be two factors."); - + BigInteger p = factors[0]; BigInteger q = factors[1]; - + assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); } } From 108c9b37b4172d2d920ec2af43c72efb6a2f6610 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Thu, 31 Oct 2024 16:45:08 +1100 Subject: [PATCH 11/12] Clang fixes for test file --- .../com/thealgorithms/others/ShorAlgorithmTest.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java index ab388feb4c96..ae7fc3d35f27 100644 --- a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java +++ b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java @@ -16,13 +16,13 @@ public void testFactorizationOfEvenNumber() { assertNotNull(factors, "Factors should not be null for composite numbers."); assertEquals(2, factors.length, "There should be two factors."); - + BigInteger p = factors[0]; BigInteger q = factors[1]; assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); } - + @Test public void testFactorizationOfPrimeNumber() { ShorAlgorithm shor = new ShorAlgorithm(); @@ -31,13 +31,13 @@ public void testFactorizationOfPrimeNumber() { assertNull(factors, "Factors should be null for prime numbers."); } - + @Test public void testFactorizationOfEvenCompositeNumber() { ShorAlgorithm shor = new ShorAlgorithm(); BigInteger number = new BigInteger("20"); BigInteger[] factors = shor.shorAlgorithm(number); - + assertNotNull(factors, "Factors should not be null for composite numbers."); assertEquals(2, factors.length, "There should be two factors."); @@ -46,5 +46,4 @@ public void testFactorizationOfEvenCompositeNumber() { assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); } -} - +} \ No newline at end of file From 85f35826d7fb350bf722879b121a0c9e12f45ccf Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Thu, 31 Oct 2024 21:29:15 +1100 Subject: [PATCH 12/12] Build handling --- src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java index ae7fc3d35f27..47c3cabd5496 100644 --- a/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java +++ b/src/test/java/com/thealgorithms/others/ShorAlgorithmTest.java @@ -19,7 +19,7 @@ public void testFactorizationOfEvenNumber() { BigInteger p = factors[0]; BigInteger q = factors[1]; - + assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); }