-
Notifications
You must be signed in to change notification settings - Fork 0
/
storngNumber.cpp
72 lines (68 loc) · 1.33 KB
/
storngNumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <bits/stdc++.h>
using namespace std;
/*
1. Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number
2. Strong number is also known as Krishnamurthi number/Peterson Number.
*/
/*Input: 145
Output: 1
Explanation: 1! + 4! + 5! = 145
*/
class Solution
{
public:
public:
long long int fact(int rem)
{
long long int fact = 1;
while (rem)
{
fact *= rem;
rem--;
}
return fact;
}
int is_StrongNumber(int n)
{
// method --1
/*long long int sum = 0;
int num = n;
while (num)
{
sum += fact(num % 10);
num = num / 10;
}
if (sum == n)
return 1;
return 0;
*/
// Method--2 Using precomputation of factorial of digit 0-9
long long int fac[10];
for(int i=0;i<10;i++)
fac[i]=fact(i);
long long int sum =0;
int num = n;
while(num)
{
sum+=fac[num%10];
num = num/10;
}
if(sum==n)
return 1;
return 0;
}
};
int main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
Solution ob;
int ans = ob.is_StrongNumber(n);
cout << ans << "\n";
}
return 0;
}