Java program to find special numbers in a range

Java program to find special numbers in a range:

In this post, we will learn how to find all special numbers in a given range. A number is called special number if the sum of factorials of digits of the number is equal to the number itself.

For example, 145 is a special number because if we calculate the sum of factorials of 1, 4 and 5, it will be equal to 145.

We will write one Java program that will take start and end numbers and it will print out all the special numbers in this range.

Algorithm:

We will follow the below algorithm to find all special numbers in a range:

  • Take the start and end numbers as inputs from the user and keep these numbers in two int variables.
  • Run one loop from start to the end numbers.
  • For each number, check if it is special or not.
    • For that, create one copy of the number.
    • Find the sum of factorials of each digit of the number.
    • If the sum is equal to the number itself, it is a special number. Else it is not.
    • If it is a special number, print it out.

Java program to print all special numbers in a range:

Below is the complete program:

import java.util.Scanner;

class Main {
    // 1
    private static int findFactorial(int n) {
        int fact = 1;
        for (int i = 2; i <= n; i++) {
            fact *= i;
        }
        return fact;
    }
    
    // 2
    private static boolean isSpecial(int number) {
        int copyNum = number;
        int rightmostDigit;
        int sum = 0;
        
        // 3
        while (copyNum != 0) {
            rightmostDigit = copyNum % 10;
            sum += findFactorial(rightmostDigit);
            copyNum /= 10;
        }
        return sum == number;
    }
    
    // 4
    public static void main(String[] args) {
        int start, end;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the start number: ");
        start = sc.nextInt();

        System.out.println("Enter the end number: ");
        end = sc.nextInt();

        for (int i = start; i <= end; i++) {
            if (isSpecial(i)) {
                System.out.println("Special number found: " + i);
            }
        }

    }
}

Explanation:

The commented numbers in the above program denotes the steps below:

  1. findFactorial method is used to find the factorial of a number. It takes one integer value as the argument and returns the factorial.
  2. isSpecial method is used to check if a number is special or not. It takes one integer value and returns one boolean.
  3. isSpecial method creates one copy of the given number. Using a while loop, it picks the rightmost digit of the number and finds the factorial for that digit and adds it to a sum variable. Once the loop ends, it returns one boolean value. true if both the number and the sum are equal, false otherwise.
  4. Inside main, we are first taking the start and end values from the user. Then using a for loop, we are finding special number in between start and end. If any special number is found, it prints one message.

Method 2: By using a HashMap:

There is a way to improve the above algorithm. We are calculating the factorial for each number and finding the sum of all factorials. We can also create one HashMap and keep the numbers from 0 to 9 as keys and the factorials for each as values. So, if we are finding the factorial, we can simply get the value from the HashMap for that number as key.

Below is the complete program:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class Main {

    private static final Map<Integer, Integer> factMap = new HashMap<Integer, Integer>() {{
        put(0, 1);
        put(1, 1);
        put(2, 2);
        put(3, 6);
        put(4, 24);
        put(5, 120);
        put(6, 720);
        put(7, 5040);
        put(8, 40320);
        put(9, 362880);
    }};
    
    private static boolean isSpecial(int number) {
        int copyNum = number;
        int rightmostDigit;
        int sum = 0;

        while (copyNum != 0) {
            rightmostDigit = copyNum % 10;
            sum += factMap.get(rightmostDigit);
            copyNum /= 10;
        }
        return sum == number;
    }

    public static void main(String[] args) {
        int start, end;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the start number: ");
        start = sc.nextInt();

        System.out.println("Enter the end number: ");
        end = sc.nextInt();

        for (int i = start; i <= end; i++) {
            if (isSpecial(i)) {
                System.out.println("Special number found: " + i);
            }
        }

    }
}

factMap is the HashMap of factorials and it holds all factorial values. Both programs will print similar output.

Sample output:

Enter the start number: 
1
Enter the end number: 
1000
Special number found: 1
Special number found: 2
Special number found: 145

You might also like: