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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 42 deletions
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.
Lines changed: 7 additions & 28 deletions
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
}

0 commit comments

Comments
 (0)