Skip to content

Commit 1481c9b

Browse files
committed
Create package prime, matrix and games
1 parent 7646991 commit 1481c9b

26 files changed

+143
-164
lines changed

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;
40-
}
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-
}
35+
}
5736
}

src/main/java/com/thealgorithms/matrix/matrixexponentiation/Fibonacci.java

+14-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.thealgorithms.matrix.matrixexponentiation;
22

3+
import java.math.BigDecimal;
34
import java.util.Scanner;
45

6+
import com.thealgorithms.matrix.utils.MatrixUtil;
7+
58
/**
69
* @author Anirudh Buvanesh (https://github.com/anirudhb11) For more information
710
* see https://www.geeksforgeeks.org/matrix-exponentiation/
@@ -12,39 +15,13 @@ private Fibonacci() {
1215
}
1316

1417
// 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}};
19-
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;
18+
private static final BigDecimal ONE = BigDecimal.valueOf(1);
19+
private static final BigDecimal ZERO = BigDecimal.valueOf(0);
3120

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-
}
21+
private static final BigDecimal[][] FIB_MATRIX = {{ONE, ONE}, {ONE, ZERO}};
22+
private static final BigDecimal[][] IDENTITY_MATRIX = {{ONE, ZERO}, {ZERO, ONE}};
23+
// First 2 fibonacci numbers
24+
private static final BigDecimal[][] BASE_FIB_NUMBERS = {{ONE}, {ZERO}};
4825

4926
/**
5027
* Calculates the fibonacci number using matrix exponentiaition technique
@@ -53,16 +30,16 @@ private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2) {
5330
* Outputs the nth * fibonacci number
5431
* @return a 2 X 1 array as { {F_n+1}, {F_n} }
5532
*/
56-
public static int[][] fib(int n) {
33+
public static BigDecimal[][] fib(int n) {
5734
if (n == 0) {
5835
return IDENTITY_MATRIX;
5936
} else {
60-
int[][] cachedResult = fib(n / 2);
61-
int[][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult);
37+
BigDecimal[][] cachedResult = fib(n / 2);
38+
BigDecimal[][] matrixExpResult = MatrixUtil.multiply(cachedResult, cachedResult).get();
6239
if (n % 2 == 0) {
6340
return matrixExpResult;
6441
} else {
65-
return matrixMultiplication(FIB_MATRIX, matrixExpResult);
42+
return MatrixUtil.multiply(FIB_MATRIX, matrixExpResult).get();
6643
}
6744
}
6845
}
@@ -71,7 +48,7 @@ public static void main(String[] args) {
7148
// Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ]
7249
Scanner sc = new Scanner(System.in);
7350
int n = sc.nextInt();
74-
int[][] result = matrixMultiplication(fib(n), BASE_FIB_NUMBERS);
51+
BigDecimal[][] result = MatrixUtil.multiply(fib(n), BASE_FIB_NUMBERS).get();
7552
System.out.println("Fib(" + n + ") = " + result[1][0]);
7653
sc.close();
7754
}

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

+51-4
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;
@@ -9,9 +9,7 @@
99
* @author: caos321
1010
* @date: 31 October 2021 (Sunday)
1111
*/
12-
public final class MatrixUtil {
13-
private MatrixUtil() {
14-
}
12+
public class MatrixUtil {
1513

1614
private static boolean isValid(final BigDecimal[][] matrix) {
1715
return matrix != null && matrix.length > 0 && matrix[0].length > 0;
@@ -25,6 +23,47 @@ private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecima
2523
return (isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length);
2624
}
2725

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

81120
return Optional.of(result);
82121
}
122+
123+
public static double[] reverseRow(final double[] inRow) {
124+
double[] res = new double[inRow.length];
125+
for (int i = 0; i < inRow.length; ++i) {
126+
res[i] = inRow[inRow.length - 1 - i];
127+
}
128+
return res;
129+
}
83130
}

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

+2-2
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

@@ -23,7 +23,7 @@
2323
* Space Complexity: O(n) - Linear space complexity due to the recursion stack.
2424
* Wikipedia: https://en.wikipedia.org/wiki/Tower_of_Hanoi
2525
*/
26-
final class TowerOfHanoi {
26+
class TowerOfHanoi {
2727

2828
private TowerOfHanoi() {
2929
}

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

+2-4
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,7 @@
88
import java.util.Set;
99

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

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

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import org.junit.jupiter.api.Test;
77

8+
import com.thealgorithms.maths.Prime.MobiusFunction;
9+
810
class MobiusFunctionTest {
911

1012
@Test

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

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.List;
77
import org.junit.jupiter.api.Test;
88

9+
import com.thealgorithms.maths.Prime.SquareFreeInteger;
10+
911
class SquareFreeIntegerTest {
1012

1113
@Test

0 commit comments

Comments
 (0)