diff --git a/src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java b/src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java index 03f18aca786f..a00ef7e3b15d 100644 --- a/src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java +++ b/src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java @@ -3,10 +3,25 @@ /** * 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. + *
+ * A Krishnamurthy number (also known as a Strong number or Factorion) 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. + *+ * For example, 145 is a Krishnamurthy number because 1! + 4! + 5! = 1 + 24 + + * 120 = 145. + *
+ * + *+ * The only Krishnamurthy numbers in base 10 are: 1, 2, 145, and 40585. + *
+ * + ** Example usage: + *
+ * ** boolean isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(145); * System.out.println(isKrishnamurthy); // Output: true @@ -14,40 +29,43 @@ * isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(123); * System.out.println(isKrishnamurthy); // Output: false *+ * + * @see Factorion + * (Wikipedia) */ public final class KrishnamurthyNumber { + // Pre-computed factorials for digits 0-9 to improve performance + private static final int[] FACTORIALS = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; + private KrishnamurthyNumber() { } /** * Checks if a number is a Krishnamurthy number. * - * @param n The number to check + *
+ * A number is a Krishnamurthy number if the sum of the factorials of its digits + * equals the number itself. + *
+ * + * @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; - if (n <= 0) { return false; - } else { - while (n != 0) { - // initialising the variable fact that will store the factorials of the digits - int fact = 1; - // computing factorial of each digit - for (int i = 1; i <= n % 10; i++) { - fact = fact * i; - } - // computing the sum of the factorials - s = s + fact; - // discarding the digit for which factorial has been calculated - n = n / 10; - } + } - // evaluating if sum of the factorials of the digits equals the number itself - return tmp == s; + int original = n; + int sum = 0; + + while (n != 0) { + int digit = n % 10; + sum = sum + FACTORIALS[digit]; + n = n / 10; } + + return sum == original; } } diff --git a/src/main/java/com/thealgorithms/others/Krishnamurthy.java b/src/main/java/com/thealgorithms/others/Krishnamurthy.java deleted file mode 100644 index 8e5ba7c6f1c7..000000000000 --- a/src/main/java/com/thealgorithms/others/Krishnamurthy.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.thealgorithms.others; - -import java.util.Scanner; - -final class Krishnamurthy { - private Krishnamurthy() { - } - - static int fact(int n) { - int i; - int p = 1; - for (i = n; i >= 1; i--) { - p = p * i; - } - return p; - } - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int a; - int b; - int s = 0; - System.out.print("Enter the number : "); - a = sc.nextInt(); - int n = a; - while (a > 0) { - b = a % 10; - s = s + fact(b); - a = a / 10; - } - if (s == n) { - System.out.print(n + " is a krishnamurthy number"); - } else { - System.out.print(n + " is not a krishnamurthy number"); - } - sc.close(); - } -} diff --git a/src/test/java/com/thealgorithms/maths/KrishnamurthyNumberTest.java b/src/test/java/com/thealgorithms/maths/KrishnamurthyNumberTest.java index 595acde2b5d8..3c9d4f886b3d 100644 --- a/src/test/java/com/thealgorithms/maths/KrishnamurthyNumberTest.java +++ b/src/test/java/com/thealgorithms/maths/KrishnamurthyNumberTest.java @@ -6,57 +6,115 @@ import org.junit.jupiter.api.Test; /** - * Unit tests for the KrishnamurthyNumber class. + * Unit tests for the {@link KrishnamurthyNumber} class. */ -public class KrishnamurthyNumberTest { +class KrishnamurthyNumberTest { /** - * Test the isKrishnamurthy method with a known Krishnamurthy number. + * Test with known Krishnamurthy number 145. + * 1! + 4! + 5! = 1 + 24 + 120 = 145 */ @Test - public void testIsKrishnamurthyTrue() { + void testIsKrishnamurthyWith145() { assertTrue(KrishnamurthyNumber.isKrishnamurthy(145)); } /** - * Test the isKrishnamurthy method with a number that is not a Krishnamurthy number. + * Test with a number that is not a Krishnamurthy number. */ @Test - public void testIsKrishnamurthyFalse() { + void testIsKrishnamurthyWithNonKrishnamurthyNumber() { assertFalse(KrishnamurthyNumber.isKrishnamurthy(123)); } /** - * Test the isKrishnamurthy method with zero. + * Test with zero, which is not a Krishnamurthy number. */ @Test - public void testIsKrishnamurthyZero() { + void testIsKrishnamurthyWithZero() { assertFalse(KrishnamurthyNumber.isKrishnamurthy(0)); } /** - * Test the isKrishnamurthy method with a negative number. + * Test with negative numbers, which cannot be Krishnamurthy numbers. */ @Test - public void testIsKrishnamurthyNegative() { + void testIsKrishnamurthyWithNegativeNumbers() { + assertFalse(KrishnamurthyNumber.isKrishnamurthy(-1)); assertFalse(KrishnamurthyNumber.isKrishnamurthy(-145)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(-100)); } /** - * Test the isKrishnamurthy method with a single-digit Krishnamurthy number. + * Test with single-digit Krishnamurthy numbers. + * 1! = 1 and 2! = 2, so both 1 and 2 are Krishnamurthy numbers. */ @Test - public void testIsKrishnamurthySingleDigitTrue() { + void testIsKrishnamurthyWithSingleDigitKrishnamurthyNumbers() { assertTrue(KrishnamurthyNumber.isKrishnamurthy(1)); assertTrue(KrishnamurthyNumber.isKrishnamurthy(2)); } /** - * Test the isKrishnamurthy method with a single-digit number that is not a Krishnamurthy number. + * Test with single-digit numbers that are not Krishnamurthy numbers. */ @Test - public void testIsKrishnamurthySingleDigitFalse() { + void testIsKrishnamurthyWithSingleDigitNonKrishnamurthyNumbers() { assertFalse(KrishnamurthyNumber.isKrishnamurthy(3)); assertFalse(KrishnamurthyNumber.isKrishnamurthy(4)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(5)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(6)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(7)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(8)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(9)); + } + + /** + * Test with the largest Krishnamurthy number: 40585. + * 4! + 0! + 5! + 8! + 5! = 24 + 1 + 120 + 40320 + 120 = 40585 + */ + @Test + void testIsKrishnamurthyWithLargestKrishnamurthyNumber() { + assertTrue(KrishnamurthyNumber.isKrishnamurthy(40585)); + } + + /** + * Test with various non-Krishnamurthy numbers. + */ + @Test + void testIsKrishnamurthyWithVariousNonKrishnamurthyNumbers() { + assertFalse(KrishnamurthyNumber.isKrishnamurthy(10)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(50)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(100)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(144)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(146)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(150)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(200)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(999)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(1000)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(40584)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(40586)); + } + + /** + * Test with numbers close to known Krishnamurthy numbers. + */ + @Test + void testIsKrishnamurthyWithNumbersCloseToKrishnamurthy() { + assertFalse(KrishnamurthyNumber.isKrishnamurthy(144)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(146)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(40584)); + assertFalse(KrishnamurthyNumber.isKrishnamurthy(40586)); + } + + /** + * Test with all known Krishnamurthy numbers in base 10. + */ + @Test + void testAllKnownKrishnamurthyNumbers() { + assertTrue(KrishnamurthyNumber.isKrishnamurthy(1)); + assertTrue(KrishnamurthyNumber.isKrishnamurthy(2)); + assertTrue(KrishnamurthyNumber.isKrishnamurthy(145)); + assertTrue(KrishnamurthyNumber.isKrishnamurthy(40585)); } }