Algorithm
Problem link- https://www.spoj.com/problems/STRONGN/
STRONGN - Strong Number
A number is called strong number if the sum of the factorial of its digit is equal to number itself. For example: 145 since
1! + 4! + 5! = 1 + 24 + 120 = 145
So, 145 is a strong number. Now given a positive number N and you have to find whether the number is strong number or not.
Input
An integer T (1 <= T <= 1000) denoting the number of test cases followed by T lines. Each containing a single integer N (0 <= N <= 1018)
Output
For each case output string “YES” if given number is strong number and “NO” otherwise.
Example
Input: 2 145 12 Output: YES NO
Code Examples
#1 Code Example with Python Programming
Code -
Python Programming
fac = [1] * 10
for i in range(1, 10):
fac[i] = fac[i - 1] * i
T = int(input())
for _ in range(T):
N = int(input())
sum = 0
for i in str(N):
sum += fac[ord(i) - 48]
if sum == N:
print('YES')
else:
print('NO')
Copy The Code &
Try With Live Editor
Input
145
12
Output
NO
Demonstration
SPOJ Solution-Strong Number-Solution in C, C++, Java, Python