Strong Numbers | Practice | GeeksforGeeks

Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number. Given a number N, the task is to check if it is a Strong Number or not. Print 1 if the Number is Strong, else Print 0.
 
Example 1: 
Input:
N = 145
Output:
1
Explanation:
1! + 4! + 5! = 145 So, 145 is a Strong
Number and therefore the Output 1.
Example 2: 
Input:
N = 14
Output:
0
Explanation:
1! + 4! = 25 So, 14 isn't a Strong
Number and therefore the Output "NO".
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isStrong() which takes an Integer N as input and returns the answer.
 
Expected Time Complexity: O(|N|)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 108
0
amitrrrvaa00032 days ago
Python:
TC : 0.02 / 3.59
def Fact(x):           if x == 0:               return 1           else:               return x * Fact(x-1)                              
s = str(N)add = 0
for i in s:
     a = Fact(int(i))     add = a + add
if add == N:     return 1
else:
     return 0
+1
surajpatil39411 month ago
    Simple C++
 
int fact(int N)   {       if(N == 0)       {           return 1;       }       return N* fact(N-1);   }      int isStrong(int N) {       int result = 0;       int Store = N;              while( N > 0)       {           int num = N%10;           result = result + fact(num);           N = N / 10;       }              if(result != Store)       {           return 0;       }       return 1;   }
0
skumar502 months ago
if(n===1 || n===2 || n===145 || n===40585)           return 1 ;       else           return 0 ;
0
velusamyvasanth2 months ago
def find_factorial(n):   if n == 0:       return 1   return n * find_factorial(n-1)   def split_the_number(N):   split_nums = []   while N != 0:       split_nums.append(N % 10)       N //= 10   return split_nums
class Solution:   def isStrong(self, N):       strong_num = 0              split_nums = split_the_number(N)                  for n in split_nums[::-1]:           strong_num += find_factorial(n)
       if N == strong_num:           return 1       return 0
0
amiransarimy3 months ago
Using In-Built Python Method
 
import math
class Solution:
   def isStrong(self, N):
       
       sum = 0
       mstr = str(N) #convert integer into strin
       
       for num in mstr:
           sum += math.factorial(int(num))
       
       return 1 if sum == N else 0
0
ayushnautiyal11103 months ago
int factorial(int N){     int fact=1;     for(int i=1;i<=N;i++){         fact=fact*i;     }     return fact; }   int isStrong(int N) {       int res=0;       int n=N;       while(N>0){           int num=N%10;           res+=factorial(num);           N=N/10;       }       return res==n?1:0;   }
0
shubham9063 months ago
 int k=N;       int sum=0;       int product=1;       while(k>0)       {           product=1;           int d=k%10;           for(int i=1;i<=d;i++)           product=product*i;           sum=sum+product;           k=k/10;       }       if(sum==N)       return 1;       else       return 0;
0
shreyanshgupta1384 months ago
 int sum=0;        int k=N;     int r;      while(k!=0)     {         r=k%10;         int f=1;       for(int i=1;i<=r;i++)       {           f=f*i;       }       k=k/10;         sum=sum+f;     }     if(sum==N)     return 1;   else     return 0;   
+1
pketul22125 months ago
class Solution {
   static int isStrong(int N) {
       int sum = 0, k = N;
       
       while(k != 0) {
           sum += fact(k % 10);
           k /= 10;
       }
       
       if(sum == N)
           return 1;
       return 0;
   }
   
   public static int fact(int n) {
       if(n == 0)
           return 1;
       return n * fact(n - 1);
   }
};
0
sonu1998nitesh6 months ago
 int isfactorial(int n)   {       int fact=1;       for(int i=1;i<=n;i++)       {           fact=fact*i;       }       return fact;   }   int isStrong(int N)    {       int rem;       int sum=0;       int temp=N;       while(N>0)       {           rem=N%10;           sum=sum+isfactorial(rem);           N=N/10;       }     if(temp==sum)     {         return 1;     }     else     {        return 0;     }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.