Skip to content

Commit da2feb4

Browse files
authored
Merge branch 'master' into improvement/improved-gcd-class
2 parents 90ec7da + ef72b1e commit da2feb4

File tree

9 files changed

+264
-28
lines changed

9 files changed

+264
-28
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@
982982
* [JosephusProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/JosephusProblemTest.java)
983983
* [KaprekarNumbersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java)
984984
* [KaratsubaMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KaratsubaMultiplicationTest.java)
985+
* [KrishnamurthyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KrishnamurthyNumberTest.java)
985986
* [LeastCommonMultipleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LeastCommonMultipleTest.java)
986987
* [LeonardoNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LeonardoNumberTest.java)
987988
* [LiouvilleLambdaFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LiouvilleLambdaFunctionTest.java)

src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public static int cutRod(int[] price, int n) {
2222
if (price == null || price.length == 0) {
2323
throw new IllegalArgumentException("Price array cannot be null or empty.");
2424
}
25+
if (n < 0) {
26+
throw new IllegalArgumentException("Rod length cannot be negative.");
27+
}
28+
2529
// Create an array to store the maximum obtainable values for each rod length.
2630
int[] val = new int[n + 1];
2731
val[0] = 0;
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
package com.thealgorithms.maths;
22

3-
/* This is a program to check if a number is a Krishnamurthy number or not.
4-
A number is a Krishnamurthy number if the sum of the factorials of the digits of the number is equal
5-
to the number itself. For example, 1, 2 and 145 are Krishnamurthy numbers. Krishnamurthy number is
6-
also referred to as a Strong number.
3+
/**
4+
* Utility class for checking if a number is a Krishnamurthy number.
5+
*
6+
* A Krishnamurthy number (also known as a Strong number) is a number whose sum of the factorials of its digits is equal to the number itself.
7+
*
8+
* For example, 145 is a Krishnamurthy number because 1! + 4! + 5! = 1 + 24 + 120 = 145.
9+
* <b>Example usage:</b>
10+
* <pre>
11+
* boolean isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(145);
12+
* System.out.println(isKrishnamurthy); // Output: true
13+
*
14+
* isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(123);
15+
* System.out.println(isKrishnamurthy); // Output: false
16+
* </pre>
717
*/
8-
import java.io.BufferedReader;
9-
import java.io.IOException;
10-
import java.io.InputStreamReader;
11-
1218
public final class KrishnamurthyNumber {
19+
1320
private KrishnamurthyNumber() {
1421
}
1522

16-
// returns True if the number is a Krishnamurthy number and False if it is not.
17-
18-
public static boolean isKMurthy(int n) {
19-
// initialising the variable s that will store the sum of the factorials of the digits to 0
20-
int s = 0;
21-
// storing the number n in a temporary variable tmp
23+
/**
24+
* Checks if a number is a Krishnamurthy number.
25+
*
26+
* @param n The number to check
27+
* @return true if the number is a Krishnamurthy number, false otherwise
28+
*/
29+
public static boolean isKrishnamurthy(int n) {
2230
int tmp = n;
31+
int s = 0;
2332

24-
// Krishnamurthy numbers are positive
2533
if (n <= 0) {
2634
return false;
27-
} // checking if the number is a Krishnamurthy number
28-
else {
35+
} else {
2936
while (n != 0) {
3037
// initialising the variable fact that will store the factorials of the digits
3138
int fact = 1;
@@ -43,15 +50,4 @@ public static boolean isKMurthy(int n) {
4350
return tmp == s;
4451
}
4552
}
46-
47-
public static void main(String[] args) throws IOException {
48-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
49-
System.out.println("Enter a number to check if it is a Krishnamurthy number: ");
50-
int n = Integer.parseInt(br.readLine());
51-
if (isKMurthy(n)) {
52-
System.out.println(n + " is a Krishnamurthy number.");
53-
} else {
54-
System.out.println(n + " is NOT a Krishnamurthy number.");
55-
}
56-
}
5753
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.thealgorithms.sorts;
2+
3+
public class AdaptiveMergeSort implements SortAlgorithm {
4+
@SuppressWarnings("unchecked")
5+
public <T extends Comparable<T>> T[] sort(T[] array) {
6+
if (array.length <= 1) {
7+
return array;
8+
}
9+
T[] aux = array.clone();
10+
sort(array, aux, 0, array.length - 1);
11+
return array;
12+
}
13+
14+
private <T extends Comparable<T>> void sort(T[] array, T[] aux, int low, int high) {
15+
if (low >= high) {
16+
return;
17+
}
18+
int mid = low + (high - low) / 2;
19+
sort(array, aux, low, mid);
20+
sort(array, aux, mid + 1, high);
21+
merge(array, aux, low, mid, high);
22+
}
23+
24+
private <T extends Comparable<T>> void merge(T[] array, T[] aux, int low, int mid, int high) {
25+
System.arraycopy(array, low, aux, low, high - low + 1);
26+
int i = low;
27+
int j = mid + 1;
28+
for (int k = low; k <= high; k++) {
29+
if (i > mid) {
30+
array[k] = aux[j++];
31+
} else if (j > high) {
32+
array[k] = aux[i++];
33+
} else if (aux[j].compareTo(aux[i]) < 0) {
34+
array[k] = aux[j++];
35+
} else {
36+
array[k] = aux[i++];
37+
}
38+
}
39+
}
40+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.sorts;
2+
3+
public class StalinSort implements SortAlgorithm {
4+
@SuppressWarnings("unchecked")
5+
public <T extends Comparable<T>> T[] sort(T[] array) {
6+
if (array.length == 0) {
7+
return array;
8+
}
9+
int currentIndex = 0;
10+
for (int i = 1; i < array.length; i++) {
11+
if (array[i].compareTo(array[currentIndex]) >= 0) {
12+
currentIndex++;
13+
array[currentIndex] = array[i];
14+
}
15+
}
16+
// Create a result array with sorted elements
17+
T[] result = (T[]) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), currentIndex + 1);
18+
System.arraycopy(array, 0, result, 0, currentIndex + 1);
19+
return result;
20+
}
21+
}

src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,10 @@ void testCutRodEmptyPrices() {
9393
int length = 5;
9494
assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "An empty prices array should throw an IllegalArgumentException.");
9595
}
96+
@Test
97+
void testCutRodNegativeLength() {
98+
int[] prices = {1, 5, 8, 9, 10}; // Prices are irrelevant for negative length
99+
int length = -1;
100+
assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "A negative rod length should throw an IllegalArgumentException.");
101+
}
96102
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Unit tests for the KrishnamurthyNumber class.
10+
*/
11+
public class KrishnamurthyNumberTest {
12+
13+
/**
14+
* Test the isKrishnamurthy method with a known Krishnamurthy number.
15+
*/
16+
@Test
17+
public void testIsKrishnamurthyTrue() {
18+
assertTrue(KrishnamurthyNumber.isKrishnamurthy(145));
19+
}
20+
21+
/**
22+
* Test the isKrishnamurthy method with a number that is not a Krishnamurthy number.
23+
*/
24+
@Test
25+
public void testIsKrishnamurthyFalse() {
26+
assertFalse(KrishnamurthyNumber.isKrishnamurthy(123));
27+
}
28+
29+
/**
30+
* Test the isKrishnamurthy method with zero.
31+
*/
32+
@Test
33+
public void testIsKrishnamurthyZero() {
34+
assertFalse(KrishnamurthyNumber.isKrishnamurthy(0));
35+
}
36+
37+
/**
38+
* Test the isKrishnamurthy method with a negative number.
39+
*/
40+
@Test
41+
public void testIsKrishnamurthyNegative() {
42+
assertFalse(KrishnamurthyNumber.isKrishnamurthy(-145));
43+
}
44+
45+
/**
46+
* Test the isKrishnamurthy method with a single-digit Krishnamurthy number.
47+
*/
48+
@Test
49+
public void testIsKrishnamurthySingleDigitTrue() {
50+
assertTrue(KrishnamurthyNumber.isKrishnamurthy(1));
51+
assertTrue(KrishnamurthyNumber.isKrishnamurthy(2));
52+
}
53+
54+
/**
55+
* Test the isKrishnamurthy method with a single-digit number that is not a Krishnamurthy number.
56+
*/
57+
@Test
58+
public void testIsKrishnamurthySingleDigitFalse() {
59+
assertFalse(KrishnamurthyNumber.isKrishnamurthy(3));
60+
assertFalse(KrishnamurthyNumber.isKrishnamurthy(4));
61+
}
62+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class AdaptiveMergeSortTest {
8+
9+
@Test
10+
public void testSortIntegers() {
11+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
12+
Integer[] input = {4, 23, 6, 78, 1, 54, 231, 9, 12};
13+
Integer[] expected = {1, 4, 6, 9, 12, 23, 54, 78, 231};
14+
Integer[] result = adaptiveMergeSort.sort(input);
15+
assertArrayEquals(expected, result);
16+
}
17+
18+
@Test
19+
public void testSortStrings() {
20+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
21+
String[] input = {"c", "a", "e", "b", "d"};
22+
String[] expected = {"a", "b", "c", "d", "e"};
23+
String[] result = adaptiveMergeSort.sort(input);
24+
assertArrayEquals(expected, result);
25+
}
26+
27+
@Test
28+
public void testSortWithDuplicates() {
29+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
30+
Integer[] input = {1, 3, 2, 2, 5, 4};
31+
Integer[] expected = {1, 2, 2, 3, 4, 5};
32+
Integer[] result = adaptiveMergeSort.sort(input);
33+
assertArrayEquals(expected, result);
34+
}
35+
36+
@Test
37+
public void testSortEmptyArray() {
38+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
39+
Integer[] input = {};
40+
Integer[] expected = {};
41+
Integer[] result = adaptiveMergeSort.sort(input);
42+
assertArrayEquals(expected, result);
43+
}
44+
45+
@Test
46+
public void testSortSingleElement() {
47+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
48+
Integer[] input = {42};
49+
Integer[] expected = {42};
50+
Integer[] result = adaptiveMergeSort.sort(input);
51+
assertArrayEquals(expected, result);
52+
}
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class StalinSortTest {
8+
9+
@Test
10+
public void testSortIntegers() {
11+
StalinSort stalinSort = new StalinSort();
12+
Integer[] input = {4, 23, 6, 78, 1, 54, 231, 9, 12};
13+
Integer[] expected = {4, 23, 78, 231};
14+
Integer[] result = stalinSort.sort(input);
15+
assertArrayEquals(expected, result);
16+
}
17+
18+
@Test
19+
public void testSortStrings() {
20+
StalinSort stalinSort = new StalinSort();
21+
String[] input = {"c", "a", "e", "b", "d"};
22+
String[] expected = {"c", "e"};
23+
String[] result = stalinSort.sort(input);
24+
assertArrayEquals(expected, result);
25+
}
26+
27+
@Test
28+
public void testSortWithDuplicates() {
29+
StalinSort stalinSort = new StalinSort();
30+
Integer[] input = {1, 3, 2, 2, 5, 4};
31+
Integer[] expected = {1, 3, 5};
32+
Integer[] result = stalinSort.sort(input);
33+
assertArrayEquals(expected, result);
34+
}
35+
36+
@Test
37+
public void testSortEmptyArray() {
38+
StalinSort stalinSort = new StalinSort();
39+
Integer[] input = {};
40+
Integer[] expected = {};
41+
Integer[] result = stalinSort.sort(input);
42+
assertArrayEquals(expected, result);
43+
}
44+
45+
@Test
46+
public void testSortSingleElement() {
47+
StalinSort stalinSort = new StalinSort();
48+
Integer[] input = {42};
49+
Integer[] expected = {42};
50+
Integer[] result = stalinSort.sort(input);
51+
assertArrayEquals(expected, result);
52+
}
53+
}

0 commit comments

Comments
 (0)