From 7fabeb3b2661ebba4527c459a47bbe0ac2b732dd Mon Sep 17 00:00:00 2001 From: HetuKariya <131229641+HetuKariya@users.noreply.github.com> Date: Sat, 27 Sep 2025 08:09:34 +0530 Subject: [PATCH 1/6] Added BitwiseGCD.java and BitwiseGCDTest.java --- .../bitmanipulation/BitwiseGCD.java | 156 ++++++++++++++++++ .../bitmanipulation/BitwiseGCDTest.java | 111 +++++++++++++ 2 files changed, 267 insertions(+) create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/BitwiseGCDTest.java diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java new file mode 100644 index 000000000000..ec740f52c696 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java @@ -0,0 +1,156 @@ +package com.thealgorithms.bitmanipulation; + +import java.math.BigInteger; + +/** + * Bitwise GCD implementation with full-range support utilities. + * + *
This class provides a fast binary (Stein's) GCD implementation for {@code long} + * inputs and a BigInteger-backed API for full 2's-complement range support (including + * {@code Long.MIN_VALUE}). The {@code long} implementation is efficient and avoids + * division/modulo operations. For edge-cases that overflow signed-64-bit ranges + * (e.g., gcd(Long.MIN_VALUE, 0) = 2^63), use the BigInteger API {@code gcdBig}. + * + *
Behaviour: + *
Handles negative inputs. If either input is {@code Long.MIN_VALUE} the + * method delegates to the BigInteger implementation and will throw {@link ArithmeticException} + * if the result cannot be represented as a signed {@code long}. + * + * @param a first value (may be negative) + * @param b second value (may be negative) + * @return non-negative gcd as a {@code long} + * @throws ArithmeticException when the exact gcd does not fit into a signed {@code long} + */ + public static long gcd(long a, long b) { + // Trivial cases + if (a == 0L) { + return absOrThrowIfOverflow(b); + } + if (b == 0L) { + return absOrThrowIfOverflow(a); + } + + // If either is Long.MIN_VALUE, absolute value doesn't fit into signed long. + if (a == Long.MIN_VALUE || b == Long.MIN_VALUE) { + // Delegate to BigInteger and try to return a long if it fits + BigInteger g = gcdBig(BigInteger.valueOf(a), BigInteger.valueOf(b)); + try { + return g.longValueExact(); + } catch (ArithmeticException ex) { + throw new ArithmeticException("GCD doesn't fit into signed long. Use gcdBig() for full-range result."); + } + } + + // Work with non-negative long values now (safe because we excluded Long.MIN_VALUE) + a = (a < 0) ? -a : a; + b = (b < 0) ? -b : b; + + // Count common factors of 2 + int commonTwos = Long.numberOfTrailingZeros(a | b); + + // Remove all factors of 2 from a + a >>= Long.numberOfTrailingZeros(a); + + while (b != 0L) { + // Remove all factors of 2 from b + b >>= Long.numberOfTrailingZeros(b); + + // Now both a and b are odd. Ensure a <= b + if (a > b) { + long tmp = a; + a = b; + b = tmp; + } + + // b >= a; subtract a from b (result is even) + b = b - a; + } + + // Restore common powers of two + return a << commonTwos; + } + + /** + * Helper to return absolute value of x unless x == Long.MIN_VALUE, in which + * case we delegate to BigInteger and throw to indicate overflow. + */ + private static long absOrThrowIfOverflow(long x) { + if (x == Long.MIN_VALUE) { + // |Long.MIN_VALUE| = 2^63 which does not fit into signed long + throw new ArithmeticException("Absolute value of Long.MIN_VALUE does not fit into signed long. Use gcdBig() for full-range support."); + } + return (x < 0) ? -x : x; + } + + /** + * Computes GCD for an array of {@code long} values. Returns 0 for empty/null arrays. + * If any intermediate gcd cannot be represented in signed long (rare), an ArithmeticException + * will be thrown. + */ + public static long gcd(long... values) { + if (values == null || values.length == 0) return 0L; + long result = values[0]; + for (int i = 1; i < values.length; i++) { + result = gcd(result, values[i]); + if (result == 1L) return 1L; // early exit + } + return result; + } + + /** + * BigInteger-backed gcd that works for the full integer range (and beyond). + * This is the recommended method when inputs may be Long.MIN_VALUE or when you + * need an exact result even if it is greater than Long.MAX_VALUE. + * + * @param a first value (may be negative) + * @param b second value (may be negative) + * @return non-negative gcd as a {@link BigInteger} + */ + public static BigInteger gcdBig(BigInteger a, BigInteger b) { + if (a == null || b == null) throw new NullPointerException("Arguments must not be null"); + return a.abs().gcd(b.abs()); + } + + /** + * Convenience overload that accepts signed-64 inputs and returns BigInteger gcd. + */ + public static BigInteger gcdBig(long a, long b) { + return gcdBig(BigInteger.valueOf(a), BigInteger.valueOf(b)); + } + + /** + * int overload for convenience. + */ + public static int gcd(int a, int b) { + return (int) gcd((long) a, (long) b); + } + + /** + * Demo main method. Remove in production; kept for ad-hoc running. + */ + public static void main(String[] args) { + System.out.println("gcd(48, 18) = " + gcd(48L, 18L)); + System.out.println("gcd(0, 5) = " + gcd(0L, 5L)); + System.out.println("gcd(270, 192) = " + gcd(270L, 192L)); + + // BigInteger example with Long.MIN_VALUE + BigInteger g = gcdBig(Long.MIN_VALUE, 0L); + System.out.println("gcdBig(Long.MIN_VALUE, 0) = " + g); // prints 2^63 + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BitwiseGCDTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BitwiseGCDTest.java new file mode 100644 index 000000000000..f3416dba9ba3 --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/BitwiseGCDTest.java @@ -0,0 +1,111 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.math.BigInteger; +import org.junit.jupiter.api.Test; + +public class BitwiseGCDTest { + + @Test + public void testGcdBasic() { + assertEquals(6L, BitwiseGCD.gcd(48L, 18L)); + } + + @Test + public void testGcdZeroAndNonZero() { + assertEquals(5L, BitwiseGCD.gcd(0L, 5L)); + assertEquals(5L, BitwiseGCD.gcd(5L, 0L)); + } + + @Test + public void testGcdBothZero() { + assertEquals(0L, BitwiseGCD.gcd(0L, 0L)); + } + + @Test + public void testGcdNegativeInputs() { + assertEquals(6L, BitwiseGCD.gcd(-48L, 18L)); + assertEquals(6L, BitwiseGCD.gcd(48L, -18L)); + assertEquals(6L, BitwiseGCD.gcd(-48L, -18L)); + } + + @Test + public void testGcdIntOverload() { + assertEquals(6, BitwiseGCD.gcd(48, 18)); + } + + @Test + public void testGcdArray() { + long[] values = {48L, 18L, 6L}; + assertEquals(6L, BitwiseGCD.gcd(values)); + } + + @Test + public void testGcdEmptyArray() { + long[] empty = {}; + assertEquals(0L, BitwiseGCD.gcd(empty)); + } + + @Test + public void testGcdCoprime() { + assertEquals(1L, BitwiseGCD.gcd(17L, 13L)); + } + + @Test + public void testGcdPowersOfTwo() { + assertEquals(1024L, BitwiseGCD.gcd(1L << 20, 1L << 10)); + } + + @Test + public void testGcdLargeNumbers() { + assertEquals(6L, BitwiseGCD.gcd(270L, 192L)); + } + + @Test + public void testGcdEarlyExitArray() { + long[] manyCoprimes = {7L, 11L, 13L, 17L, 19L, 23L, 29L}; + assertEquals(1L, BitwiseGCD.gcd(manyCoprimes)); + } + + @Test + public void testGcdSameNumbers() { + assertEquals(42L, BitwiseGCD.gcd(42L, 42L)); + } + + @Test + public void testGcdLongMinValueBigInteger() { + // gcd(Long.MIN_VALUE, 0) = |Long.MIN_VALUE| = 2^63; must use BigInteger to represent it + BigInteger expected = BigInteger.ONE.shiftLeft(63); // 2^63 + assertEquals(expected, BitwiseGCD.gcdBig(Long.MIN_VALUE, 0L)); + } + + @Test + public void testGcdLongMinValueLongOverloadThrows() { + // The long overload cannot return 2^63 as a positive signed long, so it must throw + assertThrows(ArithmeticException.class, () -> BitwiseGCD.gcd(Long.MIN_VALUE, 0L)); + } + + @Test + public void testGcdWithLongMinAndOther() { + // gcd(Long.MIN_VALUE, 2^10) should be 2^10 + long p = 1L << 10; + BigInteger expected = BigInteger.valueOf(p); + assertEquals(expected, BitwiseGCD.gcdBig(Long.MIN_VALUE, p)); + } + + @Test + public void testGcdWithBothLongMin() { + // gcd(Long.MIN_VALUE, Long.MIN_VALUE) = 2^63 + BigInteger expected = BigInteger.ONE.shiftLeft(63); + assertEquals(expected, BitwiseGCD.gcdBig(Long.MIN_VALUE, Long.MIN_VALUE)); + } + + @Test + public void testGcdEdgeCasesMixed() { + assertEquals(1L, BitwiseGCD.gcd(1L, Long.MAX_VALUE)); + assertEquals(1L, BitwiseGCD.gcd(Long.MAX_VALUE, 1L)); + } + +} \ No newline at end of file From 785d80d59af8009daf11a0b2f926a5dfccc4c94b Mon Sep 17 00:00:00 2001 From: HetuKariya <131229641+HetuKariya@users.noreply.github.com> Date: Sat, 27 Sep 2025 08:34:37 +0530 Subject: [PATCH 2/6] Fixed a bug --- .../bitmanipulation/BitwiseGCD.java | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java index ec740f52c696..1f5c02bf9957 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java @@ -24,7 +24,7 @@ */ public final class BitwiseGCD { - private BitwiseGCD() {} + private BitwiseGCD() { } /** * Computes GCD of two long values using Stein's algorithm (binary GCD). @@ -140,17 +140,5 @@ public static BigInteger gcdBig(long a, long b) { public static int gcd(int a, int b) { return (int) gcd((long) a, (long) b); } - - /** - * Demo main method. Remove in production; kept for ad-hoc running. - */ - public static void main(String[] args) { - System.out.println("gcd(48, 18) = " + gcd(48L, 18L)); - System.out.println("gcd(0, 5) = " + gcd(0L, 5L)); - System.out.println("gcd(270, 192) = " + gcd(270L, 192L)); - - // BigInteger example with Long.MIN_VALUE - BigInteger g = gcdBig(Long.MIN_VALUE, 0L); - System.out.println("gcdBig(Long.MIN_VALUE, 0) = " + g); // prints 2^63 - } + } \ No newline at end of file From 2917774fd1125e2589badc72e58f3f3cc93ca472 Mon Sep 17 00:00:00 2001 From: HetuKariya <131229641+HetuKariya@users.noreply.github.com> Date: Sat, 27 Sep 2025 08:46:25 +0530 Subject: [PATCH 3/6] Fixed some more bug --- .../bitmanipulation/BitwiseGCD.java | 20 ++++++++++++------- .../bitmanipulation/BitwiseGCDTest.java | 3 +-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java index 1f5c02bf9957..2434cb555990 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java @@ -24,7 +24,9 @@ */ public final class BitwiseGCD { - private BitwiseGCD() { } + private BitwiseGCD() { + + } /** * Computes GCD of two long values using Stein's algorithm (binary GCD). @@ -104,11 +106,15 @@ private static long absOrThrowIfOverflow(long x) { * will be thrown. */ public static long gcd(long... values) { - if (values == null || values.length == 0) return 0L; + if (values == null || values.length == 0) { + return 0L; + } long result = values[0]; for (int i = 1; i < values.length; i++) { result = gcd(result, values[i]); - if (result == 1L) return 1L; // early exit + if (result == 1L) { + return 1L; // early exit + } } return result; } @@ -117,13 +123,14 @@ public static long gcd(long... values) { * BigInteger-backed gcd that works for the full integer range (and beyond). * This is the recommended method when inputs may be Long.MIN_VALUE or when you * need an exact result even if it is greater than Long.MAX_VALUE. - * * @param a first value (may be negative) * @param b second value (may be negative) * @return non-negative gcd as a {@link BigInteger} */ public static BigInteger gcdBig(BigInteger a, BigInteger b) { - if (a == null || b == null) throw new NullPointerException("Arguments must not be null"); + if (a == null || b == null) { + throw new NullPointerException("Arguments must not be null"); + } return a.abs().gcd(b.abs()); } @@ -140,5 +147,4 @@ public static BigInteger gcdBig(long a, long b) { public static int gcd(int a, int b) { return (int) gcd((long) a, (long) b); } - -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BitwiseGCDTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BitwiseGCDTest.java index f3416dba9ba3..207fdc1e57a0 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/BitwiseGCDTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/BitwiseGCDTest.java @@ -107,5 +107,4 @@ public void testGcdEdgeCasesMixed() { assertEquals(1L, BitwiseGCD.gcd(1L, Long.MAX_VALUE)); assertEquals(1L, BitwiseGCD.gcd(Long.MAX_VALUE, 1L)); } - -} \ No newline at end of file +} From e34cde8a3c5189c0619899835ea8d12b1f5c8db4 Mon Sep 17 00:00:00 2001 From: HetuKariya <131229641+HetuKariya@users.noreply.github.com> Date: Sat, 27 Sep 2025 08:54:57 +0530 Subject: [PATCH 4/6] Fixed styling bugs --- .../com/thealgorithms/bitmanipulation/BitwiseGCD.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java index 2434cb555990..2da96f790a9f 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java @@ -25,7 +25,6 @@ public final class BitwiseGCD { private BitwiseGCD() { - } /** @@ -106,15 +105,16 @@ private static long absOrThrowIfOverflow(long x) { * will be thrown. */ public static long gcd(long... values) { - if (values == null || values.length == 0) { + + if (values == null || values.length == 0) { return 0L; } long result = values[0]; for (int i = 1; i < values.length; i++) { result = gcd(result, values[i]); - if (result == 1L) { + if (result == 1L) { return 1L; // early exit - } + } } return result; } @@ -128,7 +128,8 @@ public static long gcd(long... values) { * @return non-negative gcd as a {@link BigInteger} */ public static BigInteger gcdBig(BigInteger a, BigInteger b) { - if (a == null || b == null) { + + if (a == null || b == null) { throw new NullPointerException("Arguments must not be null"); } return a.abs().gcd(b.abs()); From 97a5a8da3ae9a8db141ccdc6ca32f9b2b880315e Mon Sep 17 00:00:00 2001 From: HetuKariya <131229641+HetuKariya@users.noreply.github.com> Date: Sat, 27 Sep 2025 08:58:51 +0530 Subject: [PATCH 5/6] Final fixing of bugs --- .../java/com/thealgorithms/bitmanipulation/BitwiseGCD.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java index 2da96f790a9f..317c3246a32b 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java @@ -24,7 +24,7 @@ */ public final class BitwiseGCD { - private BitwiseGCD() { + private BitwiseGCD() { } /** @@ -112,7 +112,7 @@ public static long gcd(long... values) { long result = values[0]; for (int i = 1; i < values.length; i++) { result = gcd(result, values[i]); - if (result == 1L) { + if (result == 1L) { return 1L; // early exit } } From 32bd37c80f7305cdb57ce4fae07f6693578fa627 Mon Sep 17 00:00:00 2001 From: HetuKariya <131229641+HetuKariya@users.noreply.github.com> Date: Sat, 27 Sep 2025 09:09:46 +0530 Subject: [PATCH 6/6] fixing exception --- .../java/com/thealgorithms/bitmanipulation/BitwiseGCD.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java index 317c3246a32b..516563459256 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java @@ -51,11 +51,7 @@ public static long gcd(long a, long b) { if (a == Long.MIN_VALUE || b == Long.MIN_VALUE) { // Delegate to BigInteger and try to return a long if it fits BigInteger g = gcdBig(BigInteger.valueOf(a), BigInteger.valueOf(b)); - try { - return g.longValueExact(); - } catch (ArithmeticException ex) { - throw new ArithmeticException("GCD doesn't fit into signed long. Use gcdBig() for full-range result."); - } + return g.longValueExact(); } // Work with non-negative long values now (safe because we excluded Long.MIN_VALUE)