Skip to content

Commit 9bcc612

Browse files
Merge branch 'master' into fix/volume-input-validation
2 parents 0a395f2 + 3bb4459 commit 9bcc612

20 files changed

Lines changed: 251 additions & 75 deletions

‎.github/workflows/build.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v7
1212
- name: Set up JDK
13-
uses: actions/setup-java@v6.2.0
13+
uses: actions/setup-java@v6.3.0
1414
with:
1515
java-version: 21
1616
distribution: 'temurin'

‎.github/workflows/codeql.yml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ jobs:
2424
uses: actions/checkout@v7
2525

2626
- name: Set up JDK
27-
uses: actions/setup-java@v6.2.0
27+
uses: actions/setup-java@v6.3.0
2828
with:
2929
java-version: 21
3030
distribution: 'temurin'
3131

3232
- name: Initialize CodeQL
33-
uses: github/codeql-action/init@v4.39.3
33+
uses: github/codeql-action/init@v4.39.4
3434
with:
3535
languages: 'java-kotlin'
3636

3737
- name: Build
3838
run: mvn --batch-mode --update-snapshots verify
3939

4040
- name: Perform CodeQL Analysis
41-
uses: github/codeql-action/analyze@v4.39.3
41+
uses: github/codeql-action/analyze@v4.39.4
4242
with:
4343
category: "/language:java-kotlin"
4444

@@ -55,12 +55,12 @@ jobs:
5555
uses: actions/checkout@v7
5656

5757
- name: Initialize CodeQL
58-
uses: github/codeql-action/init@v4.39.3
58+
uses: github/codeql-action/init@v4.39.4
5959
with:
6060
languages: 'actions'
6161

6262
- name: Perform CodeQL Analysis
63-
uses: github/codeql-action/analyze@v4.39.3
63+
uses: github/codeql-action/analyze@v4.39.4
6464
with:
6565
category: "/language:actions"
6666
...

‎.github/workflows/infer.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- uses: actions/checkout@v7
1919

2020
- name: Set up JDK
21-
uses: actions/setup-java@v6.2.0
21+
uses: actions/setup-java@v6.3.0
2222
with:
2323
java-version: 21
2424
distribution: 'temurin'

‎src/main/java/com/thealgorithms/bitmanipulation/GrayCodeConversion.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* Gray code is a binary numeral system where two successive values differ in only one bit.
5-
* This is a simple conversion between binary and Gray code.
5+
* Each input and result is treated as a 32-bit pattern, including the sign bit.
66
* Example:
77
* 7 -> 0111 -> 0100 -> 4
88
* 4 -> 0100 -> 0111 -> 7
@@ -24,7 +24,7 @@ private GrayCodeConversion() {
2424
* @return The corresponding Gray code.
2525
*/
2626
public static int binaryToGray(int num) {
27-
return num ^ (num >> 1);
27+
return num ^ (num >>> 1);
2828
}
2929

3030
/**
@@ -35,8 +35,8 @@ public static int binaryToGray(int num) {
3535
*/
3636
public static int grayToBinary(int gray) {
3737
int binary = gray;
38-
while (gray > 0) {
39-
gray >>= 1;
38+
while (gray != 0) {
39+
gray >>>= 1;
4040
binary ^= gray;
4141
}
4242
return binary;

‎src/main/java/com/thealgorithms/ciphers/ADFGVXCipher.java‎

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,18 @@ public String encrypt(String plaintext, String key) {
8080
public String decrypt(String ciphertext, String key) {
8181
String fractionatedText = reverseColumnarTransposition(ciphertext, key);
8282

83+
if (fractionatedText.length() % 2 != 0) {
84+
throw new IllegalArgumentException("Invalid ciphertext: odd number of ADFGVX letters.");
85+
}
86+
8387
StringBuilder plaintext = new StringBuilder();
8488
for (int i = 0; i < fractionatedText.length(); i += 2) {
8589
String pair = fractionatedText.substring(i, i + 2);
86-
plaintext.append(POLYBIUS_MAP.get(pair));
90+
Character letter = POLYBIUS_MAP.get(pair);
91+
if (letter == null) {
92+
throw new IllegalArgumentException("Invalid ciphertext: unknown ADFGVX pair \"" + pair + "\".");
93+
}
94+
plaintext.append(letter);
8795
}
8896

8997
return plaintext.toString();
@@ -110,11 +118,7 @@ private String columnarTransposition(String text, String key) {
110118

111119
// Read columns based on the alphabetical order of the key
112120
StringBuilder ciphertext = new StringBuilder();
113-
char[] sortedKey = key.toCharArray();
114-
Arrays.sort(sortedKey);
115-
116-
for (char keyChar : sortedKey) {
117-
int column = key.indexOf(keyChar);
121+
for (int column : columnsInKeyOrder(key)) {
118122
for (char[] row : table) {
119123
if (row[column] != '_') {
120124
ciphertext.append(row[column]);
@@ -125,6 +129,24 @@ private String columnarTransposition(String text, String key) {
125129
return ciphertext.toString();
126130
}
127131

132+
/**
133+
* Returns the column indices in the order the key imposes: alphabetical by
134+
* key letter, with repeated letters ordered by their first occurrence.
135+
* Using key.indexOf for repeated letters would map both occurrences to the
136+
* same column and make the transposition lossy.
137+
*/
138+
private int[] columnsInKeyOrder(String key) {
139+
Integer[] order = new Integer[key.length()];
140+
for (int i = 0; i < order.length; i++) {
141+
order[i] = i;
142+
}
143+
Arrays.sort(order, (a, b) -> {
144+
int byLetter = Character.compare(key.charAt(a), key.charAt(b));
145+
return byLetter != 0 ? byLetter : Integer.compare(a, b);
146+
});
147+
return Arrays.stream(order).mapToInt(Integer::intValue).toArray();
148+
}
149+
128150
/**
129151
* Helper method: Reverses the columnar transposition during decryption
130152
*
@@ -133,31 +155,36 @@ private String columnarTransposition(String text, String key) {
133155
* @return The reversed text
134156
*/
135157
private String reverseColumnarTransposition(String ciphertext, String key) {
136-
int numRows = (int) Math.ceil((double) ciphertext.length() / key.length());
137-
char[][] table = new char[numRows][key.length()];
158+
int numCols = key.length();
159+
int fullRows = ciphertext.length() / numCols;
160+
int remainder = ciphertext.length() % numCols;
161+
// The last row of the encryption table is only partially filled: the
162+
// first `remainder` columns hold one extra character. The previous
163+
// implementation assumed every column had the same length, which
164+
// misaligned the whole grid whenever the length was not a multiple of
165+
// the key length.
166+
int[] columnLength = new int[numCols];
167+
for (int column = 0; column < numCols; column++) {
168+
columnLength[column] = fullRows + (column < remainder ? 1 : 0);
169+
}
138170

139-
char[] sortedKey = key.toCharArray();
140-
Arrays.sort(sortedKey);
171+
int numRows = fullRows + (remainder > 0 ? 1 : 0);
172+
char[][] table = new char[numRows][numCols];
141173

142174
int index = 0;
143175
// Populate the table column by column according to the sorted key
144-
for (char keyChar : sortedKey) {
145-
int column = key.indexOf(keyChar);
146-
for (int row = 0; row < numRows; row++) {
147-
if (index < ciphertext.length()) {
148-
table[row][column] = ciphertext.charAt(index++);
149-
} else {
150-
table[row][column] = '_';
151-
}
176+
for (int column : columnsInKeyOrder(key)) {
177+
for (int row = 0; row < columnLength[column]; row++) {
178+
table[row][column] = ciphertext.charAt(index++);
152179
}
153180
}
154181

155182
// Read the table row by row to reconstruct the fractionated text
156183
StringBuilder fractionatedText = new StringBuilder();
157-
for (char[] row : table) {
158-
for (char cell : row) {
159-
if (cell != '_') {
160-
fractionatedText.append(cell);
184+
for (int row = 0; row < numRows; row++) {
185+
for (int column = 0; column < numCols; column++) {
186+
if (row < columnLength[column]) {
187+
fractionatedText.append(table[row][column]);
161188
}
162189
}
163190
}

‎src/main/java/com/thealgorithms/ciphers/HillCipher.java‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,22 @@ public String decrypt(String message, int[][] inverseKeyMatrix) {
6868
return plainText.toString();
6969
}
7070

71-
// Validates that the determinant of the key matrix is not zero modulo 26
71+
// Validates that the key matrix is invertible modulo 26, which requires the
72+
// determinant to be coprime with 26. Checking only for a zero determinant is not
73+
// enough: a determinant of 2 or 13 (or any other non-unit) passes that check but
74+
// has no inverse, so encryption collapses distinct plaintexts onto the same
75+
// ciphertext and decryption becomes impossible.
7276
private void validateDeterminant(int[][] keyMatrix, int n) {
73-
int det = determinant(keyMatrix, n) % 26;
74-
if (det == 0) {
75-
throw new IllegalArgumentException("Invalid key matrix. Determinant is zero modulo 26.");
77+
int det = Math.floorMod(determinant(keyMatrix, n), 26);
78+
if (gcd(det, 26) != 1) {
79+
throw new IllegalArgumentException("Invalid key matrix. Determinant must be coprime with 26 to be invertible modulo 26.");
7680
}
7781
}
7882

83+
private int gcd(int a, int b) {
84+
return b == 0 ? a : gcd(b, a % b);
85+
}
86+
7987
// Computes the determinant of a matrix recursively
8088
private int determinant(int[][] matrix, int n) {
8189
int det = 0;

‎src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ public class PlayfairCipher {
66
private String key;
77

88
public PlayfairCipher(String key) {
9-
this.key = key;
9+
// Normalize the key the same way the plaintext is normalized in encrypt():
10+
// uppercase, J folded into I, and non-letters removed. Without this a
11+
// lowercase or mixed-case key, a key containing J, or a key with
12+
// punctuation silently produces a wrong matrix.
13+
this.key = key.toUpperCase().replace("J", "I").replaceAll("[^A-Z]", "");
1014
generateMatrix();
1115
}
1216

‎src/main/java/com/thealgorithms/conversions/OctalToBinary.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private OctalToBinary() {
3737
*/
3838
public static long convertOctalToBinary(int octalNumber) {
3939
long binaryNumber = 0;
40-
int digitPosition = 1;
40+
long digitPosition = 1;
4141

4242
while (octalNumber != 0) {
4343
int octalDigit = octalNumber % 10;
Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,45 @@
11
package com.thealgorithms.maths;
22

3-
/*
4-
* Java program for Pronic Number
5-
* Pronic Number: A number n is a pronic number if
6-
* it is equal to product of two consecutive numbers m and m+1.
3+
/**
4+
* Pronic Number: A number n is a pronic number (or oblong number) if
5+
* it is equal to the product of two consecutive integers m and m + 1 (for m >= 0).
76
* Wikipedia: https://en.wikipedia.org/wiki/Pronic_number
87
*
9-
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
10-
*
11-
* */
12-
8+
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
9+
*/
1310
public final class PronicNumber {
1411
private PronicNumber() {
1512
}
1613

1714
/**
18-
* This method checks if the given number is pronic number or non-pronic number
15+
* Checks if the given number is a pronic number iteratively.
1916
*
20-
* @param inputNumber Integer value which is to be checked if is a pronic number or not
21-
* @return true if input number is a pronic number, false otherwise
17+
* @param inputNumber integer value to be checked
18+
* @return true if inputNumber is a pronic number, false otherwise
2219
*/
23-
static boolean isPronic(int inputNumber) {
24-
if (inputNumber == 0) {
25-
return true;
20+
public static boolean isPronic(int inputNumber) {
21+
if (inputNumber < 0) {
22+
return false;
2623
}
27-
// Iterating from 0 to input_number
28-
for (int i = 0; i <= inputNumber; i++) {
29-
// Checking if product of i and (i+1) is equals input_number
30-
if (i * (i + 1) == inputNumber && i != inputNumber) {
31-
// return true if product of i and (i+1) is equals input_number
24+
for (int i = 0; (long) i * (i + 1) <= inputNumber; i++) {
25+
if ((long) i * (i + 1) == inputNumber) {
3226
return true;
3327
}
3428
}
35-
36-
// return false if product of i and (i+1) for all values from 0 to input_number is not
37-
// equals input_number
3829
return false;
3930
}
4031

4132
/**
42-
* This method checks if the given number is pronic number or non-pronic number using square root of number for finding divisors
33+
* Checks if the given number is a pronic number in O(1) time using square root.
4334
*
44-
* @param number Integer value which is to be checked if is a pronic number or not
45-
* @return true if input number is a pronic number, false otherwise
35+
* @param number integer value to be checked
36+
* @return true if number is a pronic number, false otherwise
4637
*/
4738
public static boolean isPronicNumber(int number) {
48-
int squareRoot = (int) Math.sqrt(number); // finding just smaller divisor of the number than its square root.
49-
return squareRoot * (squareRoot + 1) == number;
39+
if (number < 0) {
40+
return false;
41+
}
42+
int squareRoot = (int) Math.sqrt(number);
43+
return (long) squareRoot * (squareRoot + 1) == number;
5044
}
5145
}

‎src/main/java/com/thealgorithms/maths/PythagoreanTriple.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public static boolean isPythagTriple(int a, int b, int c) {
3434
int[] sides = {a, b, c};
3535
java.util.Arrays.sort(sides);
3636

37-
int x = sides[0];
38-
int y = sides[1];
39-
int hypotenuse = sides[2];
37+
long x = sides[0];
38+
long y = sides[1];
39+
long hypotenuse = sides[2];
4040

4141
return x * x + y * y == hypotenuse * hypotenuse;
4242
}

0 commit comments

Comments
 (0)