Strong Number Check

Check if a number is strong number.

IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 8 of 13
strong-number-check.py
1# Take number
2num = int(input("Enter a number: "))
3
4# Calculate factorial function
5def fact(n):
6 if n == 0 or n == 1:
7 return 1
8 result = 1
9 for i in range(2, n + 1):
10 result *= i
11 return result
12
13# Check strong number
14temp = num
15strong_sum = 0
16while temp > 0:
17 digit = temp % 10
18 strong_sum += fact(digit)
19 temp //= 10
20
21if strong_sum == num:
22 print("Strong number")
23else:
24 print("Not a strong number")

Output

Enter a number: 145
Strong number

Enter a number: 123
Not a strong number

What's going on

Strong number: sum of factorial of digits equals the number.

Key Concepts:

Extract each digit
Calculate factorial of digit
Sum all factorials
Compare with original