mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-11 14:12:36 +08:00
Create package prime, matrix and games (#6139)
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
import static com.thealgorithms.maths.PrimeCheck.isPrime;
|
import static com.thealgorithms.maths.Prime.PrimeCheck.isPrime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a representation of the unsolved problem of Goldbach's Projection, according to which every
|
* This is a representation of the unsolved problem of Goldbach's Projection, according to which every
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths.Prime;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Java program for liouville lambda function
|
* Java program for liouville lambda function
|
||||||
@ -24,7 +24,7 @@ public final class LiouvilleLambdaFunction {
|
|||||||
* -1 when number has odd number of prime factors
|
* -1 when number has odd number of prime factors
|
||||||
* @throws IllegalArgumentException when number is negative
|
* @throws IllegalArgumentException when number is negative
|
||||||
*/
|
*/
|
||||||
static int liouvilleLambda(int number) {
|
public static int liouvilleLambda(int number) {
|
||||||
if (number <= 0) {
|
if (number <= 0) {
|
||||||
// throw exception when number is less than or is zero
|
// throw exception when number is less than or is zero
|
||||||
throw new IllegalArgumentException("Number must be greater than zero.");
|
throw new IllegalArgumentException("Number must be greater than zero.");
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths.Prime;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths.Prime;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Java program for mobius function
|
* Java program for mobius function
|
||||||
@ -25,7 +25,7 @@ public final class MobiusFunction {
|
|||||||
* 0 when number has repeated prime factor
|
* 0 when number has repeated prime factor
|
||||||
* -1 when number has odd number of prime factors
|
* -1 when number has odd number of prime factors
|
||||||
*/
|
*/
|
||||||
static int mobius(int number) {
|
public static int mobius(int number) {
|
||||||
if (number <= 0) {
|
if (number <= 0) {
|
||||||
// throw exception when number is less than or is zero
|
// throw exception when number is less than or is zero
|
||||||
throw new IllegalArgumentException("Number must be greater than zero.");
|
throw new IllegalArgumentException("Number must be greater than zero.");
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths.Prime;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths.Prime;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Authors:
|
* Authors:
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths.Prime;
|
||||||
/*
|
/*
|
||||||
* Java program for Square free integer
|
* Java program for Square free integer
|
||||||
* This class has a function which checks
|
* This class has a function which checks
|
@ -9,6 +9,8 @@ package com.thealgorithms.maths;
|
|||||||
*
|
*
|
||||||
* */
|
* */
|
||||||
|
|
||||||
|
import com.thealgorithms.maths.Prime.PrimeCheck;
|
||||||
|
|
||||||
public final class TwinPrime {
|
public final class TwinPrime {
|
||||||
private TwinPrime() {
|
private TwinPrime() {
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.matrix;
|
||||||
|
|
||||||
|
import static com.thealgorithms.matrix.utils.MatrixUtil.validateInputMatrix;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides a method to compute the rank of a matrix.
|
* This class provides a method to compute the rank of a matrix.
|
||||||
@ -63,47 +65,6 @@ public final class MatrixRank {
|
|||||||
return matrixCopy;
|
return matrixCopy;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void validateInputMatrix(double[][] matrix) {
|
|
||||||
if (matrix == null) {
|
|
||||||
throw new IllegalArgumentException("The input matrix cannot be null");
|
|
||||||
}
|
|
||||||
if (matrix.length == 0) {
|
|
||||||
throw new IllegalArgumentException("The input matrix cannot be empty");
|
|
||||||
}
|
|
||||||
if (!hasValidRows(matrix)) {
|
|
||||||
throw new IllegalArgumentException("The input matrix cannot have null or empty rows");
|
|
||||||
}
|
|
||||||
if (isJaggedMatrix(matrix)) {
|
|
||||||
throw new IllegalArgumentException("The input matrix cannot be jagged");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean hasValidRows(double[][] matrix) {
|
|
||||||
for (double[] row : matrix) {
|
|
||||||
if (row == null || row.length == 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Checks if the input matrix is a jagged matrix.
|
|
||||||
* Jagged matrix is a matrix where the number of columns in each row is not the same.
|
|
||||||
*
|
|
||||||
* @param matrix The input matrix
|
|
||||||
* @return True if the input matrix is a jagged matrix, false otherwise
|
|
||||||
*/
|
|
||||||
private static boolean isJaggedMatrix(double[][] matrix) {
|
|
||||||
int numColumns = matrix[0].length;
|
|
||||||
for (double[] row : matrix) {
|
|
||||||
if (row.length != numColumns) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The pivot row is the row in the matrix that is used to eliminate other rows and reduce the matrix to its row echelon form.
|
* @brief The pivot row is the row in the matrix that is used to eliminate other rows and reduce the matrix to its row echelon form.
|
||||||
* The pivot row is selected as the first row (from top to bottom) where the value in the current column (the pivot column) is not zero.
|
* The pivot row is selected as the first row (from top to bottom) where the value in the current column (the pivot column) is not zero.
|
@ -1,6 +1,9 @@
|
|||||||
package com.thealgorithms.matrix;
|
package com.thealgorithms.matrix;
|
||||||
|
|
||||||
// Problem Statement
|
// Problem Statement
|
||||||
|
|
||||||
|
import com.thealgorithms.matrix.utils.MatrixUtil;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
We have given an array of m x n (where m is the number of rows and n is the number of columns).
|
We have given an array of m x n (where m is the number of rows and n is the number of columns).
|
||||||
Print the new matrix in such a way that the new matrix is the mirror image of the original matrix.
|
Print the new matrix in such a way that the new matrix is the mirror image of the original matrix.
|
||||||
@ -17,41 +20,17 @@ public final class MirrorOfMatrix {
|
|||||||
private MirrorOfMatrix() {
|
private MirrorOfMatrix() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int[][] mirrorMatrix(final int[][] originalMatrix) {
|
public static double[][] mirrorMatrix(final double[][] originalMatrix) {
|
||||||
if (originalMatrix == null) {
|
MatrixUtil.validateInputMatrix(originalMatrix);
|
||||||
// Handle invalid input
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (originalMatrix.length == 0) {
|
|
||||||
return new int[0][0];
|
|
||||||
}
|
|
||||||
|
|
||||||
checkInput(originalMatrix);
|
|
||||||
|
|
||||||
int numRows = originalMatrix.length;
|
int numRows = originalMatrix.length;
|
||||||
int numCols = originalMatrix[0].length;
|
int numCols = originalMatrix[0].length;
|
||||||
|
|
||||||
int[][] mirroredMatrix = new int[numRows][numCols];
|
double[][] mirroredMatrix = new double[numRows][numCols];
|
||||||
|
|
||||||
for (int i = 0; i < numRows; i++) {
|
for (int i = 0; i < numRows; i++) {
|
||||||
mirroredMatrix[i] = reverseRow(originalMatrix[i]);
|
mirroredMatrix[i] = MatrixUtil.reverseRow(originalMatrix[i]);
|
||||||
}
|
}
|
||||||
return mirroredMatrix;
|
return mirroredMatrix;
|
||||||
}
|
}
|
||||||
private static int[] reverseRow(final int[] inRow) {
|
|
||||||
int[] res = new int[inRow.length];
|
|
||||||
for (int i = 0; i < inRow.length; ++i) {
|
|
||||||
res[i] = inRow[inRow.length - 1 - i];
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void checkInput(final int[][] matrix) {
|
|
||||||
// Check if all rows have the same number of columns
|
|
||||||
for (int i = 1; i < matrix.length; i++) {
|
|
||||||
if (matrix[i].length != matrix[0].length) {
|
|
||||||
throw new IllegalArgumentException("The input is not a matrix.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.thealgorithms.matrix.matrixexponentiation;
|
package com.thealgorithms.matrix.matrixexponentiation;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import com.thealgorithms.matrix.utils.MatrixUtil;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Anirudh Buvanesh (https://github.com/anirudhb11) For more information
|
* @author Anirudh Buvanesh (https://github.com/anirudhb11) For more information
|
||||||
@ -12,39 +13,11 @@ public final class Fibonacci {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Exponentiation matrix for Fibonacci sequence
|
// Exponentiation matrix for Fibonacci sequence
|
||||||
private static final int[][] FIB_MATRIX = {{1, 1}, {1, 0}};
|
private static final BigDecimal ONE = BigDecimal.valueOf(1);
|
||||||
private static final int[][] IDENTITY_MATRIX = {{1, 0}, {0, 1}};
|
private static final BigDecimal ZERO = BigDecimal.valueOf(0);
|
||||||
// First 2 fibonacci numbers
|
|
||||||
private static final int[][] BASE_FIB_NUMBERS = {{1}, {0}};
|
|
||||||
|
|
||||||
/**
|
private static final BigDecimal[][] FIB_MATRIX = {{ONE, ONE}, {ONE, ZERO}};
|
||||||
* Performs multiplication of 2 matrices
|
private static final BigDecimal[][] IDENTITY_MATRIX = {{ONE, ZERO}, {ZERO, ONE}};
|
||||||
*
|
|
||||||
* @param matrix1
|
|
||||||
* @param matrix2
|
|
||||||
* @return The product of matrix1 and matrix2
|
|
||||||
*/
|
|
||||||
private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2) {
|
|
||||||
// Check if matrices passed can be multiplied
|
|
||||||
int rowsInMatrix1 = matrix1.length;
|
|
||||||
int columnsInMatrix1 = matrix1[0].length;
|
|
||||||
|
|
||||||
int rowsInMatrix2 = matrix2.length;
|
|
||||||
int columnsInMatrix2 = matrix2[0].length;
|
|
||||||
|
|
||||||
assert columnsInMatrix1 == rowsInMatrix2;
|
|
||||||
int[][] product = new int[rowsInMatrix1][columnsInMatrix2];
|
|
||||||
for (int rowIndex = 0; rowIndex < rowsInMatrix1; rowIndex++) {
|
|
||||||
for (int colIndex = 0; colIndex < columnsInMatrix2; colIndex++) {
|
|
||||||
int matrixEntry = 0;
|
|
||||||
for (int intermediateIndex = 0; intermediateIndex < columnsInMatrix1; intermediateIndex++) {
|
|
||||||
matrixEntry += matrix1[rowIndex][intermediateIndex] * matrix2[intermediateIndex][colIndex];
|
|
||||||
}
|
|
||||||
product[rowIndex][colIndex] = matrixEntry;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return product;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the fibonacci number using matrix exponentiaition technique
|
* Calculates the fibonacci number using matrix exponentiaition technique
|
||||||
@ -53,26 +26,17 @@ public final class Fibonacci {
|
|||||||
* Outputs the nth * fibonacci number
|
* Outputs the nth * fibonacci number
|
||||||
* @return a 2 X 1 array as { {F_n+1}, {F_n} }
|
* @return a 2 X 1 array as { {F_n+1}, {F_n} }
|
||||||
*/
|
*/
|
||||||
public static int[][] fib(int n) {
|
public static BigDecimal[][] fib(int n) {
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
return IDENTITY_MATRIX;
|
return IDENTITY_MATRIX;
|
||||||
} else {
|
} else {
|
||||||
int[][] cachedResult = fib(n / 2);
|
BigDecimal[][] cachedResult = fib(n / 2);
|
||||||
int[][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult);
|
BigDecimal[][] matrixExpResult = MatrixUtil.multiply(cachedResult, cachedResult).get();
|
||||||
if (n % 2 == 0) {
|
if (n % 2 == 0) {
|
||||||
return matrixExpResult;
|
return matrixExpResult;
|
||||||
} else {
|
} else {
|
||||||
return matrixMultiplication(FIB_MATRIX, matrixExpResult);
|
return MatrixUtil.multiply(FIB_MATRIX, matrixExpResult).get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ]
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
int n = sc.nextInt();
|
|
||||||
int[][] result = matrixMultiplication(fib(n), BASE_FIB_NUMBERS);
|
|
||||||
System.out.println("Fib(" + n + ") = " + result[1][0]);
|
|
||||||
sc.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.matrix.utils;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -10,6 +10,7 @@ import java.util.stream.IntStream;
|
|||||||
* @date: 31 October 2021 (Sunday)
|
* @date: 31 October 2021 (Sunday)
|
||||||
*/
|
*/
|
||||||
public final class MatrixUtil {
|
public final class MatrixUtil {
|
||||||
|
|
||||||
private MatrixUtil() {
|
private MatrixUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,11 +19,52 @@ public final class MatrixUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static boolean hasEqualSizes(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
|
private static boolean hasEqualSizes(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
|
||||||
return (isValid(matrix1) && isValid(matrix2) && matrix1.length == matrix2.length && matrix1[0].length == matrix2[0].length);
|
return isValid(matrix1) && isValid(matrix2) && matrix1.length == matrix2.length && matrix1[0].length == matrix2[0].length;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
|
private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
|
||||||
return (isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length);
|
return isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void validateInputMatrix(double[][] matrix) {
|
||||||
|
if (matrix == null) {
|
||||||
|
throw new IllegalArgumentException("The input matrix cannot be null");
|
||||||
|
}
|
||||||
|
if (matrix.length == 0) {
|
||||||
|
throw new IllegalArgumentException("The input matrix cannot be empty");
|
||||||
|
}
|
||||||
|
if (!hasValidRows(matrix)) {
|
||||||
|
throw new IllegalArgumentException("The input matrix cannot have null or empty rows");
|
||||||
|
}
|
||||||
|
if (isJaggedMatrix(matrix)) {
|
||||||
|
throw new IllegalArgumentException("The input matrix cannot be jagged");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean hasValidRows(double[][] matrix) {
|
||||||
|
for (double[] row : matrix) {
|
||||||
|
if (row == null || row.length == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Checks if the input matrix is a jagged matrix.
|
||||||
|
* Jagged matrix is a matrix where the number of columns in each row is not the same.
|
||||||
|
*
|
||||||
|
* @param matrix The input matrix
|
||||||
|
* @return True if the input matrix is a jagged matrix, false otherwise
|
||||||
|
*/
|
||||||
|
private static boolean isJaggedMatrix(double[][] matrix) {
|
||||||
|
int numColumns = matrix[0].length;
|
||||||
|
for (double[] row : matrix) {
|
||||||
|
if (row.length != numColumns) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Optional<BigDecimal[][]> operate(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2, final BiFunction<BigDecimal, BigDecimal, BigDecimal> operation) {
|
private static Optional<BigDecimal[][]> operate(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2, final BiFunction<BigDecimal, BigDecimal, BigDecimal> operation) {
|
||||||
@ -80,4 +122,12 @@ public final class MatrixUtil {
|
|||||||
|
|
||||||
return Optional.of(result);
|
return Optional.of(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static double[] reverseRow(final double[] inRow) {
|
||||||
|
double[] res = new double[inRow.length];
|
||||||
|
for (int i = 0; i < inRow.length; ++i) {
|
||||||
|
res[i] = inRow[inRow.length - 1 - i];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.others;
|
package com.thealgorithms.puzzlesandgames;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that provides methods to solve Sudoku puzzles of any n x n size
|
* A class that provides methods to solve Sudoku puzzles of any n x n size
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.others;
|
package com.thealgorithms.puzzlesandgames;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.misc;
|
package com.thealgorithms.puzzlesandgames;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -8,9 +8,9 @@ import java.util.Map;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public final class WordBoggle {
|
public final class WordBoggle {
|
||||||
|
|
||||||
private WordBoggle() {
|
private WordBoggle() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* O(nm * 8^s + ws) time where n = width of boggle board, m = height of
|
* O(nm * 8^s + ws) time where n = width of boggle board, m = height of
|
||||||
* boggle board, s = length of longest word in string array, w = length of
|
* boggle board, s = length of longest word in string array, w = length of
|
@ -3,6 +3,7 @@ package com.thealgorithms.maths;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import com.thealgorithms.maths.Prime.SquareFreeInteger;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths.prime;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import com.thealgorithms.maths.Prime.LiouvilleLambdaFunction;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class LiouvilleLambdaFunctionTest {
|
class LiouvilleLambdaFunctionTest {
|
@ -1,8 +1,9 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths.prime;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import com.thealgorithms.maths.Prime.MillerRabinPrimalityCheck;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class MillerRabinPrimalityCheckTest {
|
class MillerRabinPrimalityCheckTest {
|
@ -1,8 +1,9 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths.prime;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import com.thealgorithms.maths.Prime.MobiusFunction;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class MobiusFunctionTest {
|
class MobiusFunctionTest {
|
@ -1,5 +1,6 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths.prime;
|
||||||
|
|
||||||
|
import com.thealgorithms.maths.Prime.PrimeCheck;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
@ -1,7 +1,8 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths.prime;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import com.thealgorithms.maths.Prime.PrimeFactorization;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.matrix;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
@ -1,7 +1,8 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.matrix;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import com.thealgorithms.matrix.utils.MatrixUtil;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
@ -1,7 +1,7 @@
|
|||||||
package com.thealgorithms.matrix;
|
package com.thealgorithms.matrix;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -10,44 +10,44 @@ class MirrorOfMatrixTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMirrorMatrixRegularMatrix() {
|
void testMirrorMatrixRegularMatrix() {
|
||||||
int[][] originalMatrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
double[][] originalMatrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||||
int[][] expectedMirrorMatrix = {{3, 2, 1}, {6, 5, 4}, {9, 8, 7}};
|
double[][] expectedMirrorMatrix = {{3, 2, 1}, {6, 5, 4}, {9, 8, 7}};
|
||||||
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
double[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
||||||
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMirrorMatrixEmptyMatrix() {
|
void testMirrorMatrixEmptyMatrix() {
|
||||||
int[][] originalMatrix = {};
|
double[][] originalMatrix = {};
|
||||||
int[][] expectedMirrorMatrix = {};
|
Exception e = assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(originalMatrix));
|
||||||
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
assertEquals("The input matrix cannot be empty", e.getMessage());
|
||||||
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMirrorMatrixSingleElementMatrix() {
|
void testMirrorMatrixSingleElementMatrix() {
|
||||||
int[][] originalMatrix = {{42}};
|
double[][] originalMatrix = {{42}};
|
||||||
int[][] expectedMirrorMatrix = {{42}};
|
double[][] expectedMirrorMatrix = {{42}};
|
||||||
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
double[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
||||||
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMirrorMatrixMultipleRowsOneColumnMatrix() {
|
void testMirrorMatrixMultipleRowsOneColumnMatrix() {
|
||||||
int[][] originalMatrix = {{1}, {2}, {3}, {4}};
|
double[][] originalMatrix = {{1}, {2}, {3}, {4}};
|
||||||
int[][] expectedMirrorMatrix = {{1}, {2}, {3}, {4}};
|
double[][] expectedMirrorMatrix = {{1}, {2}, {3}, {4}};
|
||||||
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
double[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
||||||
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMirrorMatrixNullInput() {
|
void testMirrorMatrixNullInput() {
|
||||||
int[][] originalMatrix = null;
|
double[][] originalMatrix = null;
|
||||||
assertNull(MirrorOfMatrix.mirrorMatrix(originalMatrix));
|
Exception e = assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(originalMatrix));
|
||||||
|
assertEquals("The input matrix cannot be null", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMirrotMarixThrows() {
|
void testMirrorMatrixThrows() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(new int[][] {{1}, {2, 3}}));
|
assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(new double[][] {{1}, {2, 3}}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.others;
|
package com.thealgorithms.puzzlesandgames;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.others;
|
package com.thealgorithms.puzzlesandgames;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
@ -1,4 +1,4 @@
|
|||||||
package com.thealgorithms.misc;
|
package com.thealgorithms.puzzlesandgames;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
Reference in New Issue
Block a user