Skip to content

Commit b32abe3

Browse files
refactor:removed main function, added JUnit test for KrishnamurthyNumber, improved docstring
1 parent 33dee07 commit b32abe3

File tree

3 files changed

+89
-28
lines changed

3 files changed

+89
-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)
Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
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 more information, refer to the
9+
* <a href="https://en.wikipedia.org/wiki/Strong_number">Krishnamurthy number</a> Wikipedia page.
10+
*
11+
* <b>Example usage:</b>
12+
* <pre>
13+
* boolean isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(145);
14+
* System.out.println(isKrishnamurthy); // Output: true
15+
*
16+
* isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(123);
17+
* System.out.println(isKrishnamurthy); // Output: false
18+
* </pre>
719
*/
8-
import java.io.BufferedReader;
9-
import java.io.IOException;
10-
import java.io.InputStreamReader;
11-
1220
public final class KrishnamurthyNumber {
21+
1322
private KrishnamurthyNumber() {
1423
}
1524

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
25+
/**
26+
* Checks if a number is a Krishnamurthy number.
27+
*
28+
* @param n The number to check
29+
* @return true if the number is a Krishnamurthy number, false otherwise
30+
*/
31+
public static boolean isKrishnamurthy(int n) {
2232
int tmp = n;
33+
int s = 0;
2334

24-
// Krishnamurthy numbers are positive
2535
if (n <= 0) {
2636
return false;
27-
} // checking if the number is a Krishnamurthy number
28-
else {
37+
} else {
2938
while (n != 0) {
3039
// initialising the variable fact that will store the factorials of the digits
3140
int fact = 1;
@@ -43,15 +52,4 @@ public static boolean isKMurthy(int n) {
4352
return tmp == s;
4453
}
4554
}
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-
}
5755
}
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+
}

0 commit comments

Comments
 (0)