Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.thealgorithms.bitmanipulation;

/**
* 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.");
}

/**
* 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;
}
}
}
72 changes: 28 additions & 44 deletions src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,50 @@
package com.thealgorithms.maths;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @brief utility class implementing <a href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a>
* 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 &lt; 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<Integer> 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.thealgorithms.bitmanipulation;

/**
* 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");
}

/**
* 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.thealgorithms.bitmanipulation;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Unit tests for {@link PowerOfFour}.
*/
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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}