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 #6138

Closed
wants to merge 9 commits into from
Closed
Changes from all 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,4 +1,4 @@
package com.thealgorithms.maths;
package com.thealgorithms.maths.Prime;

/*
* Java program for liouville lambda function
@@ -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.");
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;

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
@@ -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.");
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;

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

/*
* Authors:
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
2 changes: 2 additions & 0 deletions src/main/java/com/thealgorithms/maths/TwinPrime.java
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@
*
* */

import com.thealgorithms.maths.Prime.PrimeCheck;

public final class TwinPrime {
private TwinPrime() {
}
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.
@@ -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.
21 changes: 21 additions & 0 deletions src/main/java/com/thealgorithms/matrix/MirrorOfMatrix.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.thealgorithms.matrix;

// Problem Statement
<<<<<<< HEAD

import com.thealgorithms.matrix.utils.MatrixUtil;

=======
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
/*
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.
@@ -17,6 +23,10 @@ public final class MirrorOfMatrix {
private MirrorOfMatrix() {
}

<<<<<<< HEAD
public static double[][] mirrorMatrix(final double[][] originalMatrix) {
MatrixUtil.validateInputMatrix(originalMatrix);
=======
public static int[][] mirrorMatrix(final int[][] originalMatrix) {
if (originalMatrix == null) {
// Handle invalid input
@@ -27,10 +37,20 @@ public static int[][] mirrorMatrix(final int[][] originalMatrix) {
}

checkInput(originalMatrix);
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f

int numRows = originalMatrix.length;
int numCols = originalMatrix[0].length;

<<<<<<< HEAD
double[][] mirroredMatrix = new double[numRows][numCols];

for (int i = 0; i < numRows; i++) {
mirroredMatrix[i] = MatrixUtil.reverseRow(originalMatrix[i]);
}
return mirroredMatrix;
}
=======
int[][] mirroredMatrix = new int[numRows][numCols];

for (int i = 0; i < numRows; i++) {
@@ -54,4 +74,5 @@ private static void checkInput(final int[][] matrix) {
}
}
}
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.thealgorithms.matrix.matrixexponentiation;

<<<<<<< HEAD
import java.math.BigDecimal;
import java.util.Scanner;

import com.thealgorithms.matrix.utils.MatrixUtil;

=======
import java.util.Scanner;

>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
/**
* @author Anirudh Buvanesh (https://github.com/anirudhb11) For more information
* see https://www.geeksforgeeks.org/matrix-exponentiation/
@@ -12,6 +20,15 @@ private Fibonacci() {
}

// Exponentiation matrix for Fibonacci sequence
<<<<<<< HEAD
private static final BigDecimal ONE = BigDecimal.valueOf(1);
private static final BigDecimal ZERO = BigDecimal.valueOf(0);

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}};
=======
private static final int[][] FIB_MATRIX = {{1, 1}, {1, 0}};
private static final int[][] IDENTITY_MATRIX = {{1, 0}, {0, 1}};
// First 2 fibonacci numbers
@@ -45,6 +62,7 @@ private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2) {
}
return product;
}
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f

/**
* Calculates the fibonacci number using matrix exponentiaition technique
@@ -53,6 +71,18 @@ 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} }
*/
<<<<<<< HEAD
public static BigDecimal[][] fib(int n) {
if (n == 0) {
return IDENTITY_MATRIX;
} else {
BigDecimal[][] cachedResult = fib(n / 2);
BigDecimal[][] matrixExpResult = MatrixUtil.multiply(cachedResult, cachedResult).get();
if (n % 2 == 0) {
return matrixExpResult;
} else {
return MatrixUtil.multiply(FIB_MATRIX, matrixExpResult).get();
=======
public static int[][] fib(int n) {
if (n == 0) {
return IDENTITY_MATRIX;
@@ -63,6 +93,7 @@ public static int[][] fib(int n) {
return matrixExpResult;
} else {
return matrixMultiplication(FIB_MATRIX, matrixExpResult);
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
}
}
}
@@ -71,7 +102,11 @@ 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();
<<<<<<< HEAD
BigDecimal[][] result = MatrixUtil.multiply(fib(n), BASE_FIB_NUMBERS).get();
=======
int[][] result = matrixMultiplication(fib(n), BASE_FIB_NUMBERS);
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
System.out.println("Fib(" + n + ") = " + result[1][0]);
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;
@@ -9,9 +9,7 @@
* @author: caos321
* @date: 31 October 2021 (Sunday)
*/
public final class MatrixUtil {
private MatrixUtil() {
}
public class MatrixUtil {

private static boolean isValid(final BigDecimal[][] matrix) {
return matrix != null && matrix.length > 0 && matrix[0].length > 0;
@@ -25,6 +23,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();
@@ -80,4 +119,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
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;

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

private TowerOfHanoi() {
}
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;
@@ -8,9 +8,7 @@
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
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@

import org.junit.jupiter.api.Test;

import com.thealgorithms.maths.Prime.MobiusFunction;

class MobiusFunctionTest {

@Test
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@
import java.util.List;
import org.junit.jupiter.api.Test;

import com.thealgorithms.maths.Prime.SquareFreeInteger;

class SquareFreeIntegerTest {

@Test
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.thealgorithms.maths;
package com.thealgorithms.maths.prime;

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

import org.junit.jupiter.api.Test;

import com.thealgorithms.maths.Prime.LiouvilleLambdaFunction;

class LiouvilleLambdaFunctionTest {

@Test
Loading