From db1f33ea5f799f5737bac1498d9c0cef124e05b8 Mon Sep 17 00:00:00 2001 From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com> Date: Wed, 29 Oct 2025 18:11:32 +0530 Subject: [PATCH 1/6] Added PowerOfFour algorithm in bitmanipulation --- .../bitmanipulation/PowerOfFour.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java new file mode 100644 index 000000000000..dfde4aa2646f --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -0,0 +1,16 @@ +package bitmanipulation; + +public class PowerOfFour { + public static boolean isPowerOfFour(int n) { + // A power of 4 has only one bit set and that bit is at an even position + return n > 0 && (n & (n - 1)) == 0 && (n & 0x55555555) != 0; + } + + public static void main(String[] args) { + int num = 64; // change to test other numbers + if (isPowerOfFour(num)) + System.out.println(num + " is a power of 4."); + else + System.out.println(num + " is NOT a power of 4."); + } +} From 7dfe84e988544db24bef280aa53452c29d3ea25c Mon Sep 17 00:00:00 2001 From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com> Date: Wed, 29 Oct 2025 18:18:51 +0530 Subject: [PATCH 2/6] Moved PowerOfFour.java to main/java --- .../java/com/thealgorithms/bitmanipulation/PowerOfFour.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{test => main}/java/com/thealgorithms/bitmanipulation/PowerOfFour.java (100%) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java similarity index 100% rename from src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java rename to src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java From 08890bcc8e7ebc030ca0f82c459291516068ec3b Mon Sep 17 00:00:00 2001 From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com> Date: Wed, 29 Oct 2025 18:23:53 +0530 Subject: [PATCH 3/6] Fixed Checkstyle issues in PowerOfFour.java --- .../bitmanipulation/PowerOfFour.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java new file mode 100644 index 000000000000..a256b0816f2a --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -0,0 +1,16 @@ +package com.thealgorithms.bitmanipulation; + +public class PowerOfFour { + + private PowerOfFour() { + throw new IllegalStateException("Utility class"); + } + + public static boolean isPowerOfFour(int n) { + if (n <= 0) { + return false; + } else { + return (n & (n - 1)) == 0 && (n & 0x55555555) != 0; + } + } +} From 99f371b6eee339d65e87e88d7806d86d5f25211c Mon Sep 17 00:00:00 2001 From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com> Date: Wed, 29 Oct 2025 18:29:55 +0530 Subject: [PATCH 4/6] Fixed class name conflict and Checkstyle errors --- .../bitmanipulation/PowerOfFour.java | 32 +++++++++++------- .../bitmanipulation/PowerOfFour.java | 14 ++++++-- .../bitmanipulation/PowerOfFourTest.java | 33 +++++++++++++++++++ 3 files changed, 66 insertions(+), 13 deletions(-) create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java diff --git a/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java index dfde4aa2646f..fde287f5415c 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -1,16 +1,26 @@ -package bitmanipulation; +package com.thealgorithms.bitmanipulation; -public class PowerOfFour { - public static boolean isPowerOfFour(int n) { - // A power of 4 has only one bit set and that bit is at an even position - return n > 0 && (n & (n - 1)) == 0 && (n & 0x55555555) != 0; +/** + * This class provides a method to check if a given number is a power of four. + */ +public final class PowerOfFour { + + // Private constructor to prevent instantiation + private PowerOfFour() { + throw new AssertionError("Cannot instantiate utility class"); } - public static void main(String[] args) { - int num = 64; // change to test other numbers - if (isPowerOfFour(num)) - System.out.println(num + " is a power of 4."); - else - System.out.println(num + " is NOT a power of 4."); + /** + * Checks whether the given integer is a power of four. + * + * @param n the number to check + * @return true if n is a power of four, false otherwise + */ + public static boolean isPowerOfFour(int n) { + if (n <= 0) { + return false; + } else { + return (n & (n - 1)) == 0 && (n & 0x55555555) != 0; + } } } diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java index a256b0816f2a..fde287f5415c 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -1,11 +1,21 @@ package com.thealgorithms.bitmanipulation; -public class PowerOfFour { +/** + * This class provides a method to check if a given number is a power of four. + */ +public final class PowerOfFour { + // Private constructor to prevent instantiation private PowerOfFour() { - throw new IllegalStateException("Utility class"); + throw new AssertionError("Cannot instantiate utility class"); } + /** + * Checks whether the given integer is a power of four. + * + * @param n the number to check + * @return true if n is a power of four, false otherwise + */ public static boolean isPowerOfFour(int n) { if (n <= 0) { return false; diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java new file mode 100644 index 000000000000..1d46f1202c8d --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java @@ -0,0 +1,33 @@ +package com.thealgorithms.bitmanipulation; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for PowerOfFour class. + */ +public final class PowerOfFourTest { + + @Test + void testPowerOfFourTrueCases() { + Assertions.assertTrue(PowerOfFour.isPowerOfFour(1)); + Assertions.assertTrue(PowerOfFour.isPowerOfFour(4)); + Assertions.assertTrue(PowerOfFour.isPowerOfFour(16)); + Assertions.assertTrue(PowerOfFour.isPowerOfFour(64)); + } + + @Test + void testPowerOfFourFalseCases() { + Assertions.assertFalse(PowerOfFour.isPowerOfFour(0)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(2)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(8)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(12)); + } + + @Test + void testNegativeNumbers() { + Assertions.assertFalse(PowerOfFour.isPowerOfFour(-4)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(-16)); + } +} + From 07022c40f958ab40436d38d797d593ba96083b59 Mon Sep 17 00:00:00 2001 From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com> Date: Wed, 29 Oct 2025 18:33:13 +0530 Subject: [PATCH 5/6] Fix Checkstyle and rename test class to PowerOfFourTest --- .../java/com/thealgorithms/bitmanipulation/PowerOfFour.java | 4 ++-- .../com/thealgorithms/bitmanipulation/PowerOfFourTest.java | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java index fde287f5415c..f91f8412b3ce 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -5,9 +5,9 @@ */ public final class PowerOfFour { - // Private constructor to prevent instantiation + /** Private constructor to prevent instantiation. */ private PowerOfFour() { - throw new AssertionError("Cannot instantiate utility class"); + throw new AssertionError("Cannot instantiate utility class."); } /** diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java index 1d46f1202c8d..b202a58ed591 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java @@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test; /** - * Unit tests for PowerOfFour class. + * Unit tests for {@link PowerOfFour}. */ public final class PowerOfFourTest { @@ -30,4 +30,3 @@ void testNegativeNumbers() { Assertions.assertFalse(PowerOfFour.isPowerOfFour(-16)); } } - From 93553595058049358eb58f6be855022fc2844c54 Mon Sep 17 00:00:00 2001 From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com> Date: Wed, 29 Oct 2025 18:40:25 +0530 Subject: [PATCH 6/6] Add SieveOfEratosthenes algorithm with tests --- .../maths/SieveOfEratosthenes.java | 72 ++++++++----------- .../maths/SieveOfEratosthenesTest.java | 47 ++++-------- 2 files changed, 40 insertions(+), 79 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java b/src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java index f22d22e8c6af..e9e5e2c34ef5 100644 --- a/src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java +++ b/src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java @@ -1,66 +1,50 @@ package com.thealgorithms.maths; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; /** - * @brief utility class implementing Sieve of Eratosthenes + * Utility class that provides the Sieve of Eratosthenes algorithm. */ public final class SieveOfEratosthenes { + + /** Private constructor to prevent instantiation. */ private SieveOfEratosthenes() { + throw new AssertionError("Cannot instantiate utility class."); } - private static void checkInput(int n) { - if (n <= 0) { - throw new IllegalArgumentException("n must be positive."); + /** + * Returns an array of all prime numbers less than or equal to {@code n}. + * + * @param n the upper bound (inclusive) + * @return array of primes <= n (empty if n < 2) + */ + public static int[] sieve(final int n) { + if (n < 2) { + return new int[0]; } - } - private static Type[] sievePrimesTill(int n) { - checkInput(n); - Type[] isPrimeArray = new Type[n + 1]; - Arrays.fill(isPrimeArray, Type.PRIME); - isPrimeArray[0] = Type.NOT_PRIME; - isPrimeArray[1] = Type.NOT_PRIME; + boolean[] isPrime = new boolean[n + 1]; + Arrays.fill(isPrime, true); + isPrime[0] = false; + isPrime[1] = false; - double cap = Math.sqrt(n); - for (int i = 2; i <= cap; i++) { - if (isPrimeArray[i] == Type.PRIME) { - for (int j = 2; i * j <= n; j++) { - isPrimeArray[i * j] = Type.NOT_PRIME; + for (int p = 2; p * p <= n; p++) { + if (isPrime[p]) { + for (int multiple = p * p; multiple <= n; multiple += p) { + isPrime[multiple] = false; } } } - return isPrimeArray; - } - - private static int countPrimes(Type[] isPrimeArray) { - return (int) Arrays.stream(isPrimeArray).filter(element -> element == Type.PRIME).count(); - } - private static int[] extractPrimes(Type[] isPrimeArray) { - int numberOfPrimes = countPrimes(isPrimeArray); - int[] primes = new int[numberOfPrimes]; - int primeIndex = 0; - for (int curNumber = 0; curNumber < isPrimeArray.length; ++curNumber) { - if (isPrimeArray[curNumber] == Type.PRIME) { - primes[primeIndex++] = curNumber; + List primes = new ArrayList<>(); + for (int i = 2; i <= n; i++) { + if (isPrime[i]) { + primes.add(i); } } - return primes; - } - - /** - * @brief finds all of the prime numbers up to the given upper (inclusive) limit - * @param n upper (inclusive) limit - * @exception IllegalArgumentException n is non-positive - * @return the array of all primes up to the given number (inclusive) - */ - public static int[] findPrimesTill(int n) { - return extractPrimes(sievePrimesTill(n)); - } - private enum Type { - PRIME, - NOT_PRIME, + return primes.stream().mapToInt(Integer::intValue).toArray(); } } diff --git a/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java b/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java index ebbd5df712fc..8d332f04c512 100644 --- a/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java +++ b/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java @@ -1,46 +1,23 @@ package com.thealgorithms.maths; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -class SieveOfEratosthenesTest { - @Test - public void testfFindPrimesTill1() { - assertArrayEquals(new int[] {}, SieveOfEratosthenes.findPrimesTill(1)); - } - - @Test - public void testfFindPrimesTill2() { - assertArrayEquals(new int[] {2}, SieveOfEratosthenes.findPrimesTill(2)); - } - - @Test - public void testfFindPrimesTill4() { - var primesTill4 = new int[] {2, 3}; - assertArrayEquals(primesTill4, SieveOfEratosthenes.findPrimesTill(3)); - assertArrayEquals(primesTill4, SieveOfEratosthenes.findPrimesTill(4)); - } - - @Test - public void testfFindPrimesTill40() { - var primesTill40 = new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(37)); - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(38)); - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(39)); - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(40)); - } +/** + * Unit tests for {@link SieveOfEratosthenes}. + */ +public final class SieveOfEratosthenesTest { @Test - public void testfFindPrimesTill240() { - var primesTill240 = new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239}; - assertArrayEquals(primesTill240, SieveOfEratosthenes.findPrimesTill(239)); - assertArrayEquals(primesTill240, SieveOfEratosthenes.findPrimesTill(240)); + void testPrimesUpTo30() { + int[] expected = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; + Assertions.assertArrayEquals(expected, SieveOfEratosthenes.sieve(30)); } @Test - public void testFindPrimesTillThrowsExceptionForNonPositiveInput() { - assertThrows(IllegalArgumentException.class, () -> SieveOfEratosthenes.findPrimesTill(0)); + void testLessThanTwo() { + Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(1)); + Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(0)); + Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(-5)); } }