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

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

public class KadanesAlgorithmTest {
@Test
public void testPositiveNumbers() {
int[] arr = {1, 2, 3, 4};
Assertions.assertEquals(10, KadanesAlgorithm.maxSubArraySum(arr));
}

@Test
public void testNegativeNumbers() {
int[] arr = {-1, -2, -3, -4};
Assertions.assertEquals(-1, KadanesAlgorithm.maxSubArraySum(arr));
}

@Test
public void testMixedNumbers() {
int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
Assertions.assertEquals(6, KadanesAlgorithm.maxSubArraySum(arr)); // [4, -1, 2, 1]
}
}
54 changes: 54 additions & 0 deletions src/main/java/com/thealgorithms/graph/TopologicalSortDFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.thealgorithms.graph;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
* Implementation of Topological Sort using Depth-First Search (DFS).
*
* <p>Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering
* of vertices such that for every directed edge u → v, vertex u comes before v
* in the ordering.
*/
public final class TopologicalSortDFS {

// Private constructor to prevent instantiation
private TopologicalSortDFS() {
throw new AssertionError("Cannot instantiate utility class");
}

/**
* Performs topological sorting on a directed acyclic graph.
*
* @param vertices the number of vertices in the graph
* @param adjacencyList the adjacency list representing the graph
* @return a list containing vertices in topologically sorted order
*/
public static List<Integer> topologicalSort(int vertices, List<List<Integer>> adjacencyList) {
boolean[] visited = new boolean[vertices];
Stack<Integer> stack = new Stack<>();

for (int i = 0; i < vertices; i++) {
if (!visited[i]) {
dfs(i, visited, stack, adjacencyList);
}
}

List<Integer> result = new ArrayList<>();
while (!stack.isEmpty()) {
result.add(stack.pop());
}
return result;
}

private static void dfs(int node, boolean[] visited, Stack<Integer> stack, List<List<Integer>> adjacencyList) {
visited[node] = true;
for (int neighbor : adjacencyList.get(node)) {
if (!visited[neighbor]) {
dfs(neighbor, visited, stack, adjacencyList);
}
}
stack.push(node);
}
}
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
@@ -0,0 +1,44 @@
package com.thealgorithms.graph;

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

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

/**
* Unit tests for {@link TopologicalSortDFS}.
*/
public final class TopologicalSortDFSTest {

@Test
void testSimpleGraph() {
int vertices = 6;
List<List<Integer>> adjacencyList = new ArrayList<>();
for (int i = 0; i < vertices; i++) {
adjacencyList.add(new ArrayList<>());
}

adjacencyList.get(5).add(2);
adjacencyList.get(5).add(0);
adjacencyList.get(4).add(0);
adjacencyList.get(4).add(1);
adjacencyList.get(2).add(3);
adjacencyList.get(3).add(1);

List<Integer> result = TopologicalSortDFS.topologicalSort(vertices, adjacencyList);

// A valid topological order is one of the possible ones
List<Integer> expected = Arrays.asList(5, 4, 2, 3, 1, 0);
Assertions.assertTrue(result.containsAll(expected) && expected.containsAll(result));
}

@Test
void testEmptyGraph() {
int vertices = 0;
List<List<Integer>> adjacencyList = new ArrayList<>();
List<Integer> result = TopologicalSortDFS.topologicalSort(vertices, adjacencyList);
Assertions.assertTrue(result.isEmpty());
}
}
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));
}
}
Loading