mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Remove main function, improve docstring, add JUnit tests for KrishnamurthyNumber
. (#5881)
This commit is contained in:
@ -982,6 +982,7 @@
|
||||
* [JosephusProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/JosephusProblemTest.java)
|
||||
* [KaprekarNumbersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java)
|
||||
* [KaratsubaMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KaratsubaMultiplicationTest.java)
|
||||
* [KrishnamurthyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KrishnamurthyNumberTest.java)
|
||||
* [LeastCommonMultipleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LeastCommonMultipleTest.java)
|
||||
* [LeonardoNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LeonardoNumberTest.java)
|
||||
* [LiouvilleLambdaFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LiouvilleLambdaFunctionTest.java)
|
||||
|
@ -1,31 +1,38 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
/* This is a program to check if a number is a Krishnamurthy number or not.
|
||||
A number is a Krishnamurthy number if the sum of the factorials of the digits of the number is equal
|
||||
to the number itself. For example, 1, 2 and 145 are Krishnamurthy numbers. Krishnamurthy number is
|
||||
also referred to as a Strong number.
|
||||
/**
|
||||
* Utility class for checking if a number is a Krishnamurthy number.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* For example, 145 is a Krishnamurthy number because 1! + 4! + 5! = 1 + 24 + 120 = 145.
|
||||
* <b>Example usage:</b>
|
||||
* <pre>
|
||||
* boolean isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(145);
|
||||
* System.out.println(isKrishnamurthy); // Output: true
|
||||
*
|
||||
* isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(123);
|
||||
* System.out.println(isKrishnamurthy); // Output: false
|
||||
* </pre>
|
||||
*/
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public final class KrishnamurthyNumber {
|
||||
|
||||
private KrishnamurthyNumber() {
|
||||
}
|
||||
|
||||
// returns True if the number is a Krishnamurthy number and False if it is not.
|
||||
|
||||
public static boolean isKMurthy(int n) {
|
||||
// initialising the variable s that will store the sum of the factorials of the digits to 0
|
||||
int s = 0;
|
||||
// storing the number n in a temporary variable tmp
|
||||
/**
|
||||
* Checks if a number is a Krishnamurthy number.
|
||||
*
|
||||
* @param n The number to check
|
||||
* @return true if the number is a Krishnamurthy number, false otherwise
|
||||
*/
|
||||
public static boolean isKrishnamurthy(int n) {
|
||||
int tmp = n;
|
||||
int s = 0;
|
||||
|
||||
// Krishnamurthy numbers are positive
|
||||
if (n <= 0) {
|
||||
return false;
|
||||
} // checking if the number is a Krishnamurthy number
|
||||
else {
|
||||
} else {
|
||||
while (n != 0) {
|
||||
// initialising the variable fact that will store the factorials of the digits
|
||||
int fact = 1;
|
||||
@ -43,15 +50,4 @@ public final class KrishnamurthyNumber {
|
||||
return tmp == s;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.println("Enter a number to check if it is a Krishnamurthy number: ");
|
||||
int n = Integer.parseInt(br.readLine());
|
||||
if (isKMurthy(n)) {
|
||||
System.out.println(n + " is a Krishnamurthy number.");
|
||||
} else {
|
||||
System.out.println(n + " is NOT a Krishnamurthy number.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,62 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the KrishnamurthyNumber class.
|
||||
*/
|
||||
public class KrishnamurthyNumberTest {
|
||||
|
||||
/**
|
||||
* Test the isKrishnamurthy method with a known Krishnamurthy number.
|
||||
*/
|
||||
@Test
|
||||
public void testIsKrishnamurthyTrue() {
|
||||
assertTrue(KrishnamurthyNumber.isKrishnamurthy(145));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the isKrishnamurthy method with a number that is not a Krishnamurthy number.
|
||||
*/
|
||||
@Test
|
||||
public void testIsKrishnamurthyFalse() {
|
||||
assertFalse(KrishnamurthyNumber.isKrishnamurthy(123));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the isKrishnamurthy method with zero.
|
||||
*/
|
||||
@Test
|
||||
public void testIsKrishnamurthyZero() {
|
||||
assertFalse(KrishnamurthyNumber.isKrishnamurthy(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the isKrishnamurthy method with a negative number.
|
||||
*/
|
||||
@Test
|
||||
public void testIsKrishnamurthyNegative() {
|
||||
assertFalse(KrishnamurthyNumber.isKrishnamurthy(-145));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the isKrishnamurthy method with a single-digit Krishnamurthy number.
|
||||
*/
|
||||
@Test
|
||||
public void testIsKrishnamurthySingleDigitTrue() {
|
||||
assertTrue(KrishnamurthyNumber.isKrishnamurthy(1));
|
||||
assertTrue(KrishnamurthyNumber.isKrishnamurthy(2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the isKrishnamurthy method with a single-digit number that is not a Krishnamurthy number.
|
||||
*/
|
||||
@Test
|
||||
public void testIsKrishnamurthySingleDigitFalse() {
|
||||
assertFalse(KrishnamurthyNumber.isKrishnamurthy(3));
|
||||
assertFalse(KrishnamurthyNumber.isKrishnamurthy(4));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user