Skip to content

Commit bcdf75d

Browse files
authored
Merge branch 'master' into master
2 parents 823fd2d + 1e6ed97 commit bcdf75d

20 files changed

+224
-138
lines changed

pmd-exclude.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ com.thealgorithms.maths.SumOfArithmeticSeries=UselessParentheses
5555
com.thealgorithms.maths.TrinomialTriangle=UselessParentheses
5656
com.thealgorithms.maths.VampireNumber=CollapsibleIfStatements
5757
com.thealgorithms.maths.Volume=UselessParentheses
58-
com.thealgorithms.matrixexponentiation.Fibonacci=UnnecessaryFullyQualifiedName
5958
com.thealgorithms.misc.Sparsity=UselessParentheses
6059
com.thealgorithms.misc.ThreeSumProblem=UselessParentheses
6160
com.thealgorithms.misc.WordBoggle=UselessParentheses

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1313
<maven.compiler.source>21</maven.compiler.source>
1414
<maven.compiler.target>21</maven.compiler.target>
15-
<assertj.version>3.27.0</assertj.version>
15+
<assertj.version>3.27.2</assertj.version>
1616
</properties>
1717

1818
<dependencyManagement>
@@ -43,7 +43,7 @@
4343
<dependency>
4444
<groupId>org.mockito</groupId>
4545
<artifactId>mockito-core</artifactId>
46-
<version>5.14.2</version>
46+
<version>5.15.2</version>
4747
<scope>test</scope>
4848
</dependency>
4949

@@ -125,7 +125,7 @@
125125
<dependency>
126126
<groupId>com.puppycrawl.tools</groupId>
127127
<artifactId>checkstyle</artifactId>
128-
<version>10.21.0</version>
128+
<version>10.21.1</version>
129129
</dependency>
130130
</dependencies>
131131
</plugin>

spotbugs-exclude.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
<Match>
4242
<Bug pattern="INT_BAD_REM_BY_1" />
4343
</Match>
44-
<Match>
45-
<Bug pattern="ICAST_IDIV_CAST_TO_DOUBLE" />
46-
</Match>
4744
<Match>
4845
<Bug pattern="FE_FLOATING_POINT_EQUALITY" />
4946
</Match>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public static double average(double[] numbers) {
3737
* @return the average of the given numbers
3838
* @throws IllegalArgumentException if the input array is {@code null} or empty
3939
*/
40-
public static double average(int[] numbers) {
40+
public static long average(int[] numbers) {
4141
if (numbers == null || numbers.length == 0) {
4242
throw new IllegalArgumentException("Numbers array cannot be empty or null");
4343
}
4444
long sum = 0;
4545
for (int number : numbers) {
4646
sum += number;
4747
}
48-
return (double) (sum / numbers.length);
48+
return sum / numbers.length;
4949
}
5050
}
Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,48 @@
11
package com.thealgorithms.maths;
22

33
import java.util.ArrayList;
4-
import java.util.Collections;
54

65
/**
7-
* n number theory, a vampire number (or true vampire number) is a composite
6+
* In number theory, a vampire number (or true vampire number) is a composite
87
* natural number with an even number of digits, that can be factored into two
98
* natural numbers each with half as many digits as the original number and not
109
* both with trailing zeroes, where the two factors contain precisely all the
1110
* digits of the original number, in any order, counting multiplicity. The first
12-
* vampire number is 1260 = 21 × 60. *
11+
* vampire number is 1260 = 21 × 60.
1312
*
14-
* <p>
15-
* link: https://en.wikipedia.org/wiki/Vampire_number *
16-
*
17-
* <p>
13+
* @see <a href='https://en.wikipedia.org/wiki/Vampire_number'>Vampire number on Wikipedia</a>
1814
*/
1915
public final class VampireNumber {
16+
// Forbid instantiation.
2017
private VampireNumber() {
2118
}
2219

23-
public static void main(String[] args) {
24-
test(10, 1000);
25-
}
26-
27-
static void test(int startValue, int stopValue) {
28-
int countofRes = 1;
29-
StringBuilder res = new StringBuilder();
30-
31-
for (int i = startValue; i <= stopValue; i++) {
32-
for (int j = i; j <= stopValue; j++) {
33-
// System.out.println(i+ " "+ j);
34-
if (isVampireNumber(i, j, true)) {
35-
countofRes++;
36-
res.append("").append(countofRes).append(": = ( ").append(i).append(",").append(j).append(" = ").append(i * j).append(")").append("\n");
37-
}
38-
}
39-
}
40-
System.out.println(res);
41-
}
42-
43-
static boolean isVampireNumber(int a, int b, boolean noPseudoVamireNumbers) {
44-
// this is for pseudoVampireNumbers pseudovampire number need not be of length n/2 digits
45-
// for example 126 = 6 x 21
46-
if (noPseudoVamireNumbers) {
47-
if (a * 10 <= b || b * 10 <= a) {
48-
return false;
49-
}
20+
static boolean isVampireNumber(int a, int b, boolean ignorePseudoVampireNumbers) {
21+
// Pseudo vampire numbers don't have to be of n/2 digits. E.g., 126 = 6 x 21 is such a number.
22+
if (ignorePseudoVampireNumbers && String.valueOf(a).length() != String.valueOf(b).length()) {
23+
return false;
5024
}
5125

52-
String mulDigits = splitIntoDigits(a * b, 0);
53-
String faktorDigits = splitIntoDigits(a, b);
26+
String mulDigits = splitIntoSortedDigits(a * b);
27+
String factorDigits = splitIntoSortedDigits(a, b);
5428

55-
return mulDigits.equals(faktorDigits);
29+
return mulDigits.equals(factorDigits);
5630
}
5731

58-
// methode to Split the numbers to Digits
59-
static String splitIntoDigits(int num, int num2) {
60-
StringBuilder res = new StringBuilder();
61-
32+
// Method to split a pair of numbers to digits and sort them in the ascending order.
33+
static String splitIntoSortedDigits(int... nums) {
34+
// Collect all digits in a list.
6235
ArrayList<Integer> digits = new ArrayList<>();
63-
while (num > 0) {
64-
digits.add(num % 10);
65-
num /= 10;
66-
}
67-
while (num2 > 0) {
68-
digits.add(num2 % 10);
69-
num2 /= 10;
70-
}
71-
Collections.sort(digits);
72-
for (int i : digits) {
73-
res.append(i);
36+
for (int num : nums) {
37+
while (num > 0) {
38+
digits.add(num % 10);
39+
num /= 10;
40+
}
7441
}
7542

43+
// Sort all digits and convert to String.
44+
StringBuilder res = new StringBuilder();
45+
digits.stream().sorted().forEach(res::append);
7646
return res.toString();
7747
}
7848
}

src/main/java/com/thealgorithms/misc/InverseOfMatrix.java renamed to src/main/java/com/thealgorithms/matrix/InverseOfMatrix.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.misc;
1+
package com.thealgorithms.matrix;
22

33
/**
44
* This class provides methods to compute the inverse of a square matrix

src/main/java/com/thealgorithms/misc/MatrixTranspose.java renamed to src/main/java/com/thealgorithms/matrix/MatrixTranspose.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.misc;
1+
package com.thealgorithms.matrix;
22

33
/**
44
*

src/main/java/com/thealgorithms/misc/MedianOfMatrix.java renamed to src/main/java/com/thealgorithms/matrix/MedianOfMatrix.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.misc;
1+
package com.thealgorithms.matrix;
22

33
import java.util.ArrayList;
44
import java.util.Collections;

src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java renamed to src/main/java/com/thealgorithms/matrix/MirrorOfMatrix.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.misc;
1+
package com.thealgorithms.matrix;
22

33
// Problem Statement
44
/*
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1-
package com.thealgorithms.others;
2-
3-
import java.util.ArrayList;
4-
import java.util.List;
5-
6-
public class PrintAMatrixInSpiralOrder {
7-
/**
8-
* Search a key in row and column wise sorted matrix
9-
*
10-
* @param matrix matrix to be searched
11-
* @param row number of rows matrix has
12-
* @param col number of columns matrix has
13-
* @author Sadiul Hakim : https://github.com/sadiul-hakim
14-
*/
15-
16-
public List<Integer> print(int[][] matrix, int row, int col) {
17-
18-
// r traverses matrix row wise from first
19-
int r = 0;
20-
// c traverses matrix column wise from first
21-
int c = 0;
22-
int i;
23-
24-
List<Integer> result = new ArrayList<>();
25-
26-
while (r < row && c < col) {
27-
// print first row of matrix
28-
for (i = c; i < col; i++) {
29-
result.add(matrix[r][i]);
30-
}
31-
32-
// increase r by one because first row printed
33-
r++;
34-
35-
// print last column
36-
for (i = r; i < row; i++) {
37-
result.add(matrix[i][col - 1]);
38-
}
39-
40-
// decrease col by one because last column has been printed
41-
col--;
42-
43-
// print rows from last except printed elements
44-
if (r < row) {
45-
for (i = col - 1; i >= c; i--) {
46-
result.add(matrix[row - 1][i]);
47-
}
48-
49-
row--;
50-
}
51-
52-
// print columns from first except printed elements
53-
if (c < col) {
54-
for (i = row - 1; i >= r; i--) {
55-
result.add(matrix[i][c]);
56-
}
57-
c++;
58-
}
59-
}
60-
return result;
61-
}
62-
}
1+
package com.thealgorithms.matrix;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class PrintAMatrixInSpiralOrder {
7+
/**
8+
* Search a key in row and column wise sorted matrix
9+
*
10+
* @param matrix matrix to be searched
11+
* @param row number of rows matrix has
12+
* @param col number of columns matrix has
13+
* @author Sadiul Hakim : https://github.com/sadiul-hakim
14+
*/
15+
16+
public List<Integer> print(int[][] matrix, int row, int col) {
17+
18+
// r traverses matrix row wise from first
19+
int r = 0;
20+
// c traverses matrix column wise from first
21+
int c = 0;
22+
int i;
23+
24+
List<Integer> result = new ArrayList<>();
25+
26+
while (r < row && c < col) {
27+
// print first row of matrix
28+
for (i = c; i < col; i++) {
29+
result.add(matrix[r][i]);
30+
}
31+
32+
// increase r by one because first row printed
33+
r++;
34+
35+
// print last column
36+
for (i = r; i < row; i++) {
37+
result.add(matrix[i][col - 1]);
38+
}
39+
40+
// decrease col by one because last column has been printed
41+
col--;
42+
43+
// print rows from last except printed elements
44+
if (r < row) {
45+
for (i = col - 1; i >= c; i--) {
46+
result.add(matrix[row - 1][i]);
47+
}
48+
49+
row--;
50+
}
51+
52+
// print columns from first except printed elements
53+
if (c < col) {
54+
for (i = row - 1; i >= r; i--) {
55+
result.add(matrix[i][c]);
56+
}
57+
c++;
58+
}
59+
}
60+
return result;
61+
}
62+
}

src/main/java/com/thealgorithms/others/RotateMatrixBy90Degrees.java renamed to src/main/java/com/thealgorithms/matrix/RotateMatrixBy90Degrees.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.others;
1+
package com.thealgorithms.matrix;
22

33
import java.util.Scanner;
44
/**

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.matrixexponentiation;
1+
package com.thealgorithms.matrix.matrixexponentiation;
22

33
import java.util.Scanner;
44

@@ -55,14 +55,14 @@ private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2) {
5555
*/
5656
public static int[][] fib(int n) {
5757
if (n == 0) {
58-
return Fibonacci.IDENTITY_MATRIX;
58+
return IDENTITY_MATRIX;
5959
} else {
6060
int[][] cachedResult = fib(n / 2);
6161
int[][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult);
6262
if (n % 2 == 0) {
6363
return matrixExpResult;
6464
} else {
65-
return matrixMultiplication(Fibonacci.FIB_MATRIX, matrixExpResult);
65+
return matrixMultiplication(FIB_MATRIX, matrixExpResult);
6666
}
6767
}
6868
}

src/main/java/com/thealgorithms/others/KochSnowflake.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static BufferedImage getKochSnowflake(int imageWidth, int steps) {
105105
double offsetX = imageWidth / 10.;
106106
double offsetY = imageWidth / 3.7;
107107
Vector2 vector1 = new Vector2(offsetX, offsetY);
108-
Vector2 vector2 = new Vector2(imageWidth / 2, Math.sin(Math.PI / 3) * imageWidth * 0.8 + offsetY);
108+
Vector2 vector2 = new Vector2(imageWidth / 2.0, Math.sin(Math.PI / 3.0) * imageWidth * 0.8 + offsetY);
109109
Vector2 vector3 = new Vector2(imageWidth - offsetX, offsetY);
110110
ArrayList<Vector2> initialVectors = new ArrayList<Vector2>();
111111
initialVectors.add(vector1);

0 commit comments

Comments
 (0)