Check if a number is a Strong Number or not

Problem Statement: Given an integer Print “YES” if it is a strong number else print “NO”.

Note : 

  1. When the sum of factorial of individual digits of a number is equal to the original number the number is called a strong number. 
  2. Strong number is also known as Krishnamurthi number/Peterson Number.

Examples:

Examples 1:
Input: N = 145
Output: Yes
Explanation: 1! + 4! + 5! = 145. Hence 145 is a strong number. 

Example 2:
Input:  26
Output: No
Explanation: 2! + 6! = 722. Hence 26 is not a strong number.

Solution

Disclaimer: Don’t jump directly to the solution, try it out yourself first.

Approach :

  • Declare a variable  to store the sum of factorials.Initialize it to 0.
  • Extract the individual digits of the given number and calculate the digits’s factorial.
  • Add the computed factorial to the sum variable. 
  • Once all the digits have been extracted  and their factorial has been added in the sum, compare the original number with sum.

If found equal print “YES” otherwise “NO”.

Code:

C++ Code

#include<iostream>
using namespace std;
//Function to calculate the factorial of the individual digits
int Factorial(int n) {
   int fact = 1;
   for (int i = 1; i <= n; i++) {
      fact = fact * i;
   }
   return fact;
}
//Function to compute sum of factorials
int Strong_No(int num) {
   int sum = 0;
   //Extract all the digits from num
   while (num > 0) {
      int digit = num % 10;
      sum = sum + Factorial(digit);
      num = num / 10;
   }
   return sum;
}
int main() {
   int number = 145;
   int answer = Strong_No(number);
   if (answer == number && number != 0) {
      cout << "YES";
   } 
else {
      cout << "NO";
   }
}

Output: YES

Time Complexity: O( N * M) where N is the number of digits and M  is the time taken to compute the factorial of the digit.

Space Complexity: O(1)

Java Code

public class Main {
   //Function to calculate the factorial of the individual digits
   static int Factorial(int n) {
      int fact = 1;
      for (int i = 1; i <= n; i++) {
         fact = fact * i;
      }
      return fact;
   }

   //Function to compute sum of factorials
   static int Strong_No(int num) {
      int sum = 0;

      //Extract all the digits from num
      while (num > 0) {
         int digit = num % 10;
         sum = sum + Factorial(digit);
         num = num / 10;
      }
      return sum;
   }

   public static void main(String[] args) {

      int number = 145;
      int answer = Strong_No(number);

      if (answer == number && number != 0) {

         System.out.println("YES");
      } 
      else {
         System.out.println("NO");
      }
   }
}

Output: YES

Time Complexity: O( N * M) where N is the number of digits and M  is the time taken to compute the factorial of the digit.

Space Complexity: O(1)

Special thanks to Somparna Chakrabarti for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article