Skip to content

refactor: removed main function, improved docstring, written JUnit tests for KrishnamurthyNumber. #5881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 24 additions & 28 deletions src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -43,15 +50,4 @@ public static boolean isKMurthy(int n) {
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.");
}
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}