Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create package prime, matrix and games #6139

Merged
merged 20 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithms.maths;
package com.thealgorithms.maths.Prime;

/*
* Java program for liouville lambda function
Expand All @@ -24,7 +24,7 @@ private LiouvilleLambdaFunction() {
* -1 when number has odd number of prime factors
* @throws IllegalArgumentException when number is negative
*/
static int liouvilleLambda(int number) {
public static int liouvilleLambda(int number) {
if (number <= 0) {
// throw exception when number is less than or is zero
throw new IllegalArgumentException("Number must be greater than zero.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithms.maths;
package com.thealgorithms.maths.Prime;

import java.util.Random;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithms.maths;
package com.thealgorithms.maths.Prime;

/*
* Java program for mobius function
Expand All @@ -25,7 +25,7 @@ private MobiusFunction() {
* 0 when number has repeated prime factor
* -1 when number has odd number of prime factors
*/
static int mobius(int number) {
public static int mobius(int number) {
if (number <= 0) {
// throw exception when number is less than or is zero
throw new IllegalArgumentException("Number must be greater than zero.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithms.maths;
package com.thealgorithms.maths.Prime;

import java.util.Scanner;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithms.maths;
package com.thealgorithms.maths.Prime;

/*
* Authors:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithms.maths;
package com.thealgorithms.maths.Prime;
/*
* Java program for Square free integer
* This class has a function which checks
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/thealgorithms/maths/TwinPrime.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*
* */

import com.thealgorithms.maths.Prime.PrimeCheck;

public final class TwinPrime {
private TwinPrime() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -63,47 +65,6 @@ private static double[][] deepCopy(double[][] matrix) {
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.
* 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.
Expand Down
35 changes: 7 additions & 28 deletions src/main/java/com/thealgorithms/matrix/MirrorOfMatrix.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.thealgorithms.matrix;

// 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).
Print the new matrix in such a way that the new matrix is the mirror image of the original matrix.
Expand All @@ -17,41 +20,17 @@ public final class MirrorOfMatrix {
private MirrorOfMatrix() {
}

public static int[][] mirrorMatrix(final int[][] originalMatrix) {
if (originalMatrix == null) {
// Handle invalid input
return null;
}
if (originalMatrix.length == 0) {
return new int[0][0];
}

checkInput(originalMatrix);
public static double[][] mirrorMatrix(final double[][] originalMatrix) {
MatrixUtil.validateInputMatrix(originalMatrix);

int numRows = originalMatrix.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++) {
mirroredMatrix[i] = reverseRow(originalMatrix[i]);
mirroredMatrix[i] = MatrixUtil.reverseRow(originalMatrix[i]);
}
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.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.matrix.matrixexponentiation;

import com.thealgorithms.matrix.utils.MatrixUtil;
import java.math.BigDecimal;
import java.util.Scanner;

/**
Expand All @@ -12,39 +14,13 @@ private Fibonacci() {
}

// Exponentiation matrix for Fibonacci sequence
private static final int[][] FIB_MATRIX = {{1, 1}, {1, 0}};
private static final int[][] IDENTITY_MATRIX = {{1, 0}, {0, 1}};
// First 2 fibonacci numbers
private static final int[][] BASE_FIB_NUMBERS = {{1}, {0}};

/**
* Performs multiplication of 2 matrices
*
* @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;
private static final BigDecimal ONE = BigDecimal.valueOf(1);
private static final BigDecimal ZERO = BigDecimal.valueOf(0);

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

/**
* Calculates the fibonacci number using matrix exponentiaition technique
Expand All @@ -53,16 +29,16 @@ private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2) {
* Outputs the nth * fibonacci number
* @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) {
return IDENTITY_MATRIX;
} else {
int[][] cachedResult = fib(n / 2);
int[][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult);
BigDecimal[][] cachedResult = fib(n / 2);
BigDecimal[][] matrixExpResult = MatrixUtil.multiply(cachedResult, cachedResult).get();
if (n % 2 == 0) {
return matrixExpResult;
} else {
return matrixMultiplication(FIB_MATRIX, matrixExpResult);
return MatrixUtil.multiply(FIB_MATRIX, matrixExpResult).get();
}
}
}
Expand All @@ -71,8 +47,7 @@ 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]);
BigDecimal[][] result = MatrixUtil.multiply(fib(n), BASE_FIB_NUMBERS).get();
sc.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithms.maths;
package com.thealgorithms.matrix.utils;

import java.math.BigDecimal;
import java.util.Optional;
Expand All @@ -9,7 +9,8 @@
* @author: caos321
* @date: 31 October 2021 (Sunday)
*/
public final class MatrixUtil {
public class MatrixUtil {

private MatrixUtil() {
}

Expand All @@ -25,6 +26,47 @@ private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecima
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) {
if (!hasEqualSizes(matrix1, matrix2)) {
return Optional.empty();
Expand Down Expand Up @@ -80,4 +122,12 @@ public static Optional<BigDecimal[][]> multiply(final BigDecimal[][] matrix1, fi

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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithms.others;
package com.thealgorithms.puzzlesandgames;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithms.misc;
package com.thealgorithms.puzzlesandgames;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -8,9 +8,9 @@
import java.util.Set;

public final class WordBoggle {

private WordBoggle() {
}

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.thealgorithms.maths;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.List;
import org.junit.jupiter.api.Test;

import com.thealgorithms.maths.Prime.SquareFreeInteger;

class SquareFreeIntegerTest {

@Test
Expand Down
Loading
Loading