Skip to content

Commit 4ef0682

Browse files
authored
Create package prime, matrix and games (#6139)
1 parent f9efd38 commit 4ef0682

27 files changed

+123
-160
lines changed

src/main/java/com/thealgorithms/maths/GoldbachConjecture.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.thealgorithms.maths;
22

3-
import static com.thealgorithms.maths.PrimeCheck.isPrime;
3+
import static com.thealgorithms.maths.Prime.PrimeCheck.isPrime;
44

55
/**
66
* This is a representation of the unsolved problem of Goldbach's Projection, according to which every

src/main/java/com/thealgorithms/maths/LiouvilleLambdaFunction.java renamed to src/main/java/com/thealgorithms/maths/Prime/LiouvilleLambdaFunction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.Prime;
22

33
/*
44
* Java program for liouville lambda function
@@ -24,7 +24,7 @@ private LiouvilleLambdaFunction() {
2424
* -1 when number has odd number of prime factors
2525
* @throws IllegalArgumentException when number is negative
2626
*/
27-
static int liouvilleLambda(int number) {
27+
public static int liouvilleLambda(int number) {
2828
if (number <= 0) {
2929
// throw exception when number is less than or is zero
3030
throw new IllegalArgumentException("Number must be greater than zero.");

src/main/java/com/thealgorithms/maths/MillerRabinPrimalityCheck.java renamed to src/main/java/com/thealgorithms/maths/Prime/MillerRabinPrimalityCheck.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.Prime;
22

33
import java.util.Random;
44

src/main/java/com/thealgorithms/maths/MobiusFunction.java renamed to src/main/java/com/thealgorithms/maths/Prime/MobiusFunction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.Prime;
22

33
/*
44
* Java program for mobius function
@@ -25,7 +25,7 @@ private MobiusFunction() {
2525
* 0 when number has repeated prime factor
2626
* -1 when number has odd number of prime factors
2727
*/
28-
static int mobius(int number) {
28+
public static int mobius(int number) {
2929
if (number <= 0) {
3030
// throw exception when number is less than or is zero
3131
throw new IllegalArgumentException("Number must be greater than zero.");

src/main/java/com/thealgorithms/maths/PrimeCheck.java renamed to src/main/java/com/thealgorithms/maths/Prime/PrimeCheck.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.Prime;
22

33
import java.util.Scanner;
44

src/main/java/com/thealgorithms/maths/PrimeFactorization.java renamed to src/main/java/com/thealgorithms/maths/Prime/PrimeFactorization.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.Prime;
22

33
/*
44
* Authors:

src/main/java/com/thealgorithms/maths/SquareFreeInteger.java renamed to src/main/java/com/thealgorithms/maths/Prime/SquareFreeInteger.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.Prime;
22
/*
33
* Java program for Square free integer
44
* This class has a function which checks

src/main/java/com/thealgorithms/maths/TwinPrime.java

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*
1010
* */
1111

12+
import com.thealgorithms.maths.Prime.PrimeCheck;
13+
1214
public final class TwinPrime {
1315
private TwinPrime() {
1416
}

src/main/java/com/thealgorithms/maths/MatrixRank.java renamed to src/main/java/com/thealgorithms/matrix/MatrixRank.java

+3-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.matrix;
2+
3+
import static com.thealgorithms.matrix.utils.MatrixUtil.validateInputMatrix;
24

35
/**
46
* This class provides a method to compute the rank of a matrix.
@@ -63,47 +65,6 @@ private static double[][] deepCopy(double[][] matrix) {
6365
return matrixCopy;
6466
}
6567

66-
private static void validateInputMatrix(double[][] matrix) {
67-
if (matrix == null) {
68-
throw new IllegalArgumentException("The input matrix cannot be null");
69-
}
70-
if (matrix.length == 0) {
71-
throw new IllegalArgumentException("The input matrix cannot be empty");
72-
}
73-
if (!hasValidRows(matrix)) {
74-
throw new IllegalArgumentException("The input matrix cannot have null or empty rows");
75-
}
76-
if (isJaggedMatrix(matrix)) {
77-
throw new IllegalArgumentException("The input matrix cannot be jagged");
78-
}
79-
}
80-
81-
private static boolean hasValidRows(double[][] matrix) {
82-
for (double[] row : matrix) {
83-
if (row == null || row.length == 0) {
84-
return false;
85-
}
86-
}
87-
return true;
88-
}
89-
90-
/**
91-
* @brief Checks if the input matrix is a jagged matrix.
92-
* Jagged matrix is a matrix where the number of columns in each row is not the same.
93-
*
94-
* @param matrix The input matrix
95-
* @return True if the input matrix is a jagged matrix, false otherwise
96-
*/
97-
private static boolean isJaggedMatrix(double[][] matrix) {
98-
int numColumns = matrix[0].length;
99-
for (double[] row : matrix) {
100-
if (row.length != numColumns) {
101-
return true;
102-
}
103-
}
104-
return false;
105-
}
106-
10768
/**
10869
* @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.
10970
* 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.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.thealgorithms.matrix;
22

33
// Problem Statement
4+
5+
import com.thealgorithms.matrix.utils.MatrixUtil;
6+
47
/*
58
We have given an array of m x n (where m is the number of rows and n is the number of columns).
69
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 {
1720
private MirrorOfMatrix() {
1821
}
1922

20-
public static int[][] mirrorMatrix(final int[][] originalMatrix) {
21-
if (originalMatrix == null) {
22-
// Handle invalid input
23-
return null;
24-
}
25-
if (originalMatrix.length == 0) {
26-
return new int[0][0];
27-
}
28-
29-
checkInput(originalMatrix);
23+
public static double[][] mirrorMatrix(final double[][] originalMatrix) {
24+
MatrixUtil.validateInputMatrix(originalMatrix);
3025

3126
int numRows = originalMatrix.length;
3227
int numCols = originalMatrix[0].length;
3328

34-
int[][] mirroredMatrix = new int[numRows][numCols];
29+
double[][] mirroredMatrix = new double[numRows][numCols];
3530

3631
for (int i = 0; i < numRows; i++) {
37-
mirroredMatrix[i] = reverseRow(originalMatrix[i]);
32+
mirroredMatrix[i] = MatrixUtil.reverseRow(originalMatrix[i]);
3833
}
3934
return mirroredMatrix;
4035
}
41-
private static int[] reverseRow(final int[] inRow) {
42-
int[] res = new int[inRow.length];
43-
for (int i = 0; i < inRow.length; ++i) {
44-
res[i] = inRow[inRow.length - 1 - i];
45-
}
46-
return res;
47-
}
48-
49-
private static void checkInput(final int[][] matrix) {
50-
// Check if all rows have the same number of columns
51-
for (int i = 1; i < matrix.length; i++) {
52-
if (matrix[i].length != matrix[0].length) {
53-
throw new IllegalArgumentException("The input is not a matrix.");
54-
}
55-
}
56-
}
5736
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.matrix.matrixexponentiation;
22

3-
import java.util.Scanner;
3+
import com.thealgorithms.matrix.utils.MatrixUtil;
4+
import java.math.BigDecimal;
45

56
/**
67
* @author Anirudh Buvanesh (https://github.com/anirudhb11) For more information
@@ -12,39 +13,11 @@ private Fibonacci() {
1213
}
1314

1415
// Exponentiation matrix for Fibonacci sequence
15-
private static final int[][] FIB_MATRIX = {{1, 1}, {1, 0}};
16-
private static final int[][] IDENTITY_MATRIX = {{1, 0}, {0, 1}};
17-
// First 2 fibonacci numbers
18-
private static final int[][] BASE_FIB_NUMBERS = {{1}, {0}};
16+
private static final BigDecimal ONE = BigDecimal.valueOf(1);
17+
private static final BigDecimal ZERO = BigDecimal.valueOf(0);
1918

20-
/**
21-
* Performs multiplication of 2 matrices
22-
*
23-
* @param matrix1
24-
* @param matrix2
25-
* @return The product of matrix1 and matrix2
26-
*/
27-
private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2) {
28-
// Check if matrices passed can be multiplied
29-
int rowsInMatrix1 = matrix1.length;
30-
int columnsInMatrix1 = matrix1[0].length;
31-
32-
int rowsInMatrix2 = matrix2.length;
33-
int columnsInMatrix2 = matrix2[0].length;
34-
35-
assert columnsInMatrix1 == rowsInMatrix2;
36-
int[][] product = new int[rowsInMatrix1][columnsInMatrix2];
37-
for (int rowIndex = 0; rowIndex < rowsInMatrix1; rowIndex++) {
38-
for (int colIndex = 0; colIndex < columnsInMatrix2; colIndex++) {
39-
int matrixEntry = 0;
40-
for (int intermediateIndex = 0; intermediateIndex < columnsInMatrix1; intermediateIndex++) {
41-
matrixEntry += matrix1[rowIndex][intermediateIndex] * matrix2[intermediateIndex][colIndex];
42-
}
43-
product[rowIndex][colIndex] = matrixEntry;
44-
}
45-
}
46-
return product;
47-
}
19+
private static final BigDecimal[][] FIB_MATRIX = {{ONE, ONE}, {ONE, ZERO}};
20+
private static final BigDecimal[][] IDENTITY_MATRIX = {{ONE, ZERO}, {ZERO, ONE}};
4821

4922
/**
5023
* Calculates the fibonacci number using matrix exponentiaition technique
@@ -53,26 +26,17 @@ private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2) {
5326
* Outputs the nth * fibonacci number
5427
* @return a 2 X 1 array as { {F_n+1}, {F_n} }
5528
*/
56-
public static int[][] fib(int n) {
29+
public static BigDecimal[][] fib(int n) {
5730
if (n == 0) {
5831
return IDENTITY_MATRIX;
5932
} else {
60-
int[][] cachedResult = fib(n / 2);
61-
int[][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult);
33+
BigDecimal[][] cachedResult = fib(n / 2);
34+
BigDecimal[][] matrixExpResult = MatrixUtil.multiply(cachedResult, cachedResult).get();
6235
if (n % 2 == 0) {
6336
return matrixExpResult;
6437
} else {
65-
return matrixMultiplication(FIB_MATRIX, matrixExpResult);
38+
return MatrixUtil.multiply(FIB_MATRIX, matrixExpResult).get();
6639
}
6740
}
6841
}
69-
70-
public static void main(String[] args) {
71-
// Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ]
72-
Scanner sc = new Scanner(System.in);
73-
int n = sc.nextInt();
74-
int[][] result = matrixMultiplication(fib(n), BASE_FIB_NUMBERS);
75-
System.out.println("Fib(" + n + ") = " + result[1][0]);
76-
sc.close();
77-
}
7842
}

src/main/java/com/thealgorithms/maths/MatrixUtil.java renamed to src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java

+53-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.matrix.utils;
22

33
import java.math.BigDecimal;
44
import java.util.Optional;
@@ -10,6 +10,7 @@
1010
* @date: 31 October 2021 (Sunday)
1111
*/
1212
public final class MatrixUtil {
13+
1314
private MatrixUtil() {
1415
}
1516

@@ -18,11 +19,52 @@ private static boolean isValid(final BigDecimal[][] matrix) {
1819
}
1920

2021
private static boolean hasEqualSizes(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
21-
return (isValid(matrix1) && isValid(matrix2) && matrix1.length == matrix2.length && matrix1[0].length == matrix2[0].length);
22+
return isValid(matrix1) && isValid(matrix2) && matrix1.length == matrix2.length && matrix1[0].length == matrix2[0].length;
2223
}
2324

2425
private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
25-
return (isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length);
26+
return isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length;
27+
}
28+
29+
public static void validateInputMatrix(double[][] matrix) {
30+
if (matrix == null) {
31+
throw new IllegalArgumentException("The input matrix cannot be null");
32+
}
33+
if (matrix.length == 0) {
34+
throw new IllegalArgumentException("The input matrix cannot be empty");
35+
}
36+
if (!hasValidRows(matrix)) {
37+
throw new IllegalArgumentException("The input matrix cannot have null or empty rows");
38+
}
39+
if (isJaggedMatrix(matrix)) {
40+
throw new IllegalArgumentException("The input matrix cannot be jagged");
41+
}
42+
}
43+
44+
private static boolean hasValidRows(double[][] matrix) {
45+
for (double[] row : matrix) {
46+
if (row == null || row.length == 0) {
47+
return false;
48+
}
49+
}
50+
return true;
51+
}
52+
53+
/**
54+
* @brief Checks if the input matrix is a jagged matrix.
55+
* Jagged matrix is a matrix where the number of columns in each row is not the same.
56+
*
57+
* @param matrix The input matrix
58+
* @return True if the input matrix is a jagged matrix, false otherwise
59+
*/
60+
private static boolean isJaggedMatrix(double[][] matrix) {
61+
int numColumns = matrix[0].length;
62+
for (double[] row : matrix) {
63+
if (row.length != numColumns) {
64+
return true;
65+
}
66+
}
67+
return false;
2668
}
2769

2870
private static Optional<BigDecimal[][]> operate(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2, final BiFunction<BigDecimal, BigDecimal, BigDecimal> operation) {
@@ -80,4 +122,12 @@ public static Optional<BigDecimal[][]> multiply(final BigDecimal[][] matrix1, fi
80122

81123
return Optional.of(result);
82124
}
125+
126+
public static double[] reverseRow(final double[] inRow) {
127+
double[] res = new double[inRow.length];
128+
for (int i = 0; i < inRow.length; ++i) {
129+
res[i] = inRow[inRow.length - 1 - i];
130+
}
131+
return res;
132+
}
83133
}

src/main/java/com/thealgorithms/others/Sudoku.java renamed to src/main/java/com/thealgorithms/puzzlesandgames/Sudoku.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.others;
1+
package com.thealgorithms.puzzlesandgames;
22

33
/**
44
* A class that provides methods to solve Sudoku puzzles of any n x n size

src/main/java/com/thealgorithms/others/TowerOfHanoi.java renamed to src/main/java/com/thealgorithms/puzzlesandgames/TowerOfHanoi.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.others;
1+
package com.thealgorithms.puzzlesandgames;
22

33
import java.util.List;
44

src/main/java/com/thealgorithms/misc/WordBoggle.java renamed to src/main/java/com/thealgorithms/puzzlesandgames/WordBoggle.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.misc;
1+
package com.thealgorithms.puzzlesandgames;
22

33
import java.util.ArrayList;
44
import java.util.HashMap;
@@ -8,9 +8,9 @@
88
import java.util.Set;
99

1010
public final class WordBoggle {
11+
1112
private WordBoggle() {
1213
}
13-
1414
/**
1515
* O(nm * 8^s + ws) time where n = width of boggle board, m = height of
1616
* boggle board, s = length of longest word in string array, w = length of

src/test/java/com/thealgorithms/maths/SquareFreeIntegerTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6+
import com.thealgorithms.maths.Prime.SquareFreeInteger;
67
import java.util.List;
78
import org.junit.jupiter.api.Test;
89

src/test/java/com/thealgorithms/maths/LiouvilleLambdaFunctionTest.java renamed to src/test/java/com/thealgorithms/maths/prime/LiouvilleLambdaFunctionTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.prime;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6+
import com.thealgorithms.maths.Prime.LiouvilleLambdaFunction;
67
import org.junit.jupiter.api.Test;
78

89
class LiouvilleLambdaFunctionTest {

0 commit comments

Comments
 (0)