mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
refactor: Enhance docs, code, add tests in KrishnaMurthyNumber (#6742)
* refactor: Enhance docs, code, add tests in `KrishnaMurthyNumber` * Lint
This commit is contained in:
@@ -3,10 +3,25 @@ package com.thealgorithms.maths;
|
||||
/**
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* </p>
|
||||
*
|
||||
* For example, 145 is a Krishnamurthy number because 1! + 4! + 5! = 1 + 24 + 120 = 145.
|
||||
* <p>
|
||||
* For example, 145 is a Krishnamurthy number because 1! + 4! + 5! = 1 + 24 +
|
||||
* 120 = 145.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* The only Krishnamurthy numbers in base 10 are: 1, 2, 145, and 40585.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* <b>Example usage:</b>
|
||||
* </p>
|
||||
*
|
||||
* <pre>
|
||||
* boolean isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(145);
|
||||
* System.out.println(isKrishnamurthy); // Output: true
|
||||
@@ -14,40 +29,43 @@ package com.thealgorithms.maths;
|
||||
* isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(123);
|
||||
* System.out.println(isKrishnamurthy); // Output: false
|
||||
* </pre>
|
||||
*
|
||||
* @see <a href="https://en.wikipedia.org/wiki/Factorion">Factorion
|
||||
* (Wikipedia)</a>
|
||||
*/
|
||||
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
|
||||
* <p>
|
||||
* A number is a Krishnamurthy number if the sum of the factorials of its digits
|
||||
* equals the number itself.
|
||||
* </p>
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -6,57 +6,115 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user