Create package prime, matrix and games (#6139)

This commit is contained in:
varada610
2025-01-27 03:10:41 -08:00
committed by GitHub
parent f9efd382d1
commit 4ef06822ca
27 changed files with 123 additions and 160 deletions

View File

@@ -0,0 +1,64 @@
package com.thealgorithms.maths.prime;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.thealgorithms.maths.Prime.LiouvilleLambdaFunction;
import org.junit.jupiter.api.Test;
class LiouvilleLambdaFunctionTest {
@Test
void testLiouvilleLambdaMustThrowExceptionIfNumberIsZero() {
// given
int number = 0;
String expectedMessage = "Number must be greater than zero.";
// when
Exception exception = assertThrows(IllegalArgumentException.class, () -> { LiouvilleLambdaFunction.liouvilleLambda(number); });
String actualMessage = exception.getMessage();
// then
assertEquals(expectedMessage, actualMessage);
}
@Test
void testLiouvilleLambdaMustThrowExceptionIfNumberIsNegative() {
// given
int number = -1;
String expectedMessage = "Number must be greater than zero.";
// when
Exception exception = assertThrows(IllegalArgumentException.class, () -> { LiouvilleLambdaFunction.liouvilleLambda(number); });
String actualMessage = exception.getMessage();
// then
assertEquals(expectedMessage, actualMessage);
}
@Test
void testLiouvilleLambdaMustReturnNegativeOne() {
// given
int number = 11;
int expectedOutput = -1;
// when
int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
// then
assertEquals(expectedOutput, actualOutput);
}
@Test
void testLiouvilleLambdaMustReturnPositiveOne() {
// given
int number = 10;
int expectedOutput = 1;
// when
int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
// then
assertEquals(expectedOutput, actualOutput);
}
}

View File

@@ -0,0 +1,31 @@
package com.thealgorithms.maths.prime;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.thealgorithms.maths.Prime.MillerRabinPrimalityCheck;
import org.junit.jupiter.api.Test;
class MillerRabinPrimalityCheckTest {
@Test
void testDeterministicMillerRabinForPrimes() {
assertTrue(MillerRabinPrimalityCheck.deterministicMillerRabin(2));
assertTrue(MillerRabinPrimalityCheck.deterministicMillerRabin(37));
assertTrue(MillerRabinPrimalityCheck.deterministicMillerRabin(123457));
assertTrue(MillerRabinPrimalityCheck.deterministicMillerRabin(6472601713L));
}
@Test
void testDeterministicMillerRabinForNotPrimes() {
assertFalse(MillerRabinPrimalityCheck.deterministicMillerRabin(1));
assertFalse(MillerRabinPrimalityCheck.deterministicMillerRabin(35));
assertFalse(MillerRabinPrimalityCheck.deterministicMillerRabin(123453));
assertFalse(MillerRabinPrimalityCheck.deterministicMillerRabin(647260175));
}
@Test
void testMillerRabinForPrimes() {
assertTrue(MillerRabinPrimalityCheck.millerRabin(11, 5));
assertTrue(MillerRabinPrimalityCheck.millerRabin(97, 5));
assertTrue(MillerRabinPrimalityCheck.millerRabin(6720589, 5));
assertTrue(MillerRabinPrimalityCheck.millerRabin(9549401549L, 5));
}
}

View File

@@ -0,0 +1,155 @@
package com.thealgorithms.maths.prime;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.thealgorithms.maths.Prime.MobiusFunction;
import org.junit.jupiter.api.Test;
class MobiusFunctionTest {
@Test
void testMobiusForZero() {
// given
int number = 0;
String expectedMessage = "Number must be greater than zero.";
// when
Exception exception = assertThrows(IllegalArgumentException.class, () -> { MobiusFunction.mobius(number); });
String actualMessage = exception.getMessage();
// then
assertEquals(expectedMessage, actualMessage);
}
@Test
void testMobiusForNegativeNumber() {
// given
int number = -1;
String expectedMessage = "Number must be greater than zero.";
// when
Exception exception = assertThrows(IllegalArgumentException.class, () -> { MobiusFunction.mobius(number); });
String actualMessage = exception.getMessage();
// then
assertEquals(expectedMessage, actualMessage);
}
@Test
void testMobiusFunction() {
int[] expectedResultArray = {
1,
-1,
-1,
0,
-1,
1,
-1,
0,
0,
1,
-1,
0,
-1,
1,
1,
0,
-1,
0,
-1,
0,
1,
1,
-1,
0,
0,
1,
0,
0,
-1,
-1,
-1,
0,
1,
1,
1,
0,
-1,
1,
1,
0,
-1,
-1,
-1,
0,
0,
1,
-1,
0,
0,
0,
1,
0,
-1,
0,
1,
0,
1,
1,
-1,
0,
-1,
1,
0,
0,
1,
-1,
-1,
0,
1,
-1,
-1,
0,
-1,
1,
0,
0,
1,
-1,
-1,
0,
0,
1,
-1,
0,
1,
1,
1,
0,
-1,
0,
1,
0,
1,
1,
1,
0,
-1,
0,
0,
0,
};
for (int i = 1; i <= 100; i++) {
// given
int expectedValue = expectedResultArray[i - 1];
// when
int actualValue = MobiusFunction.mobius(i);
// then
assertEquals(expectedValue, actualValue);
}
}
}

View File

@@ -0,0 +1,43 @@
package com.thealgorithms.maths.prime;
import com.thealgorithms.maths.Prime.PrimeCheck;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class PrimeCheckTest {
@Test
void test1() {
Assertions.assertTrue(PrimeCheck.isPrime(2));
}
@Test
void test2() {
Assertions.assertFalse(PrimeCheck.isPrime(-1));
}
@Test
void test3() {
Assertions.assertFalse(PrimeCheck.isPrime(4));
}
@Test
void test4() {
Assertions.assertTrue(PrimeCheck.isPrime(5));
}
@Test
void test5() {
Assertions.assertFalse(PrimeCheck.isPrime(15));
}
@Test
void test6() {
Assertions.assertTrue(PrimeCheck.isPrime(11));
}
@Test
void test7() {
Assertions.assertFalse(PrimeCheck.isPrime(49));
}
}

View File

@@ -0,0 +1,33 @@
package com.thealgorithms.maths.prime;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.thealgorithms.maths.Prime.PrimeFactorization;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class PrimeFactorizationTest {
@ParameterizedTest
@MethodSource("provideNumbersAndFactors")
void testPrimeFactorization(int number, List<Integer> expectedFactors) {
assertEquals(expectedFactors, PrimeFactorization.pfactors(number), "Prime factors for number: " + number);
}
@ParameterizedTest
@MethodSource("provideNumbersAndSizes")
void testPrimeFactorsSize(int number, int expectedSize) {
assertEquals(expectedSize, PrimeFactorization.pfactors(number).size(), "Size of prime factors list for number: " + number);
}
private static Stream<Arguments> provideNumbersAndFactors() {
return Stream.of(Arguments.of(0, List.of()), Arguments.of(1, List.of()), Arguments.of(2, List.of(2)), Arguments.of(3, List.of(3)), Arguments.of(4, List.of(2, 2)), Arguments.of(18, List.of(2, 3, 3)), Arguments.of(100, List.of(2, 2, 5, 5)), Arguments.of(198, List.of(2, 3, 3, 11)));
}
private static Stream<Arguments> provideNumbersAndSizes() {
return Stream.of(Arguments.of(2, 1), Arguments.of(3, 1), Arguments.of(4, 2), Arguments.of(18, 3), Arguments.of(100, 4), Arguments.of(198, 4));
}
}