Strong Number Check

Check if number is strong (sum of factorial of digits equals number).

IntermediatePhase 2 - Level 2: Number-based Looping LogicExample 12 of 14
strong-number-check.py
1# Helper function for factorial
2def factorial(n):
3 if n == 0 or n == 1:
4 return 1
5 return n * factorial(n - 1)
6
7# Take number
8num = int(input("Enter a number: "))
9
10# Calculate sum of factorial of digits
11temp = abs(num)
12strong_sum = 0
13while temp > 0:
14 digit = temp % 10
15 strong_sum += factorial(digit)
16 temp //= 10
17
18# Check
19if abs(num) == strong_sum:
20 print("Strong number")
21else:
22 print("Not a strong number")

Output

Enter a number: 145
Strong number

What's going on

Sum of factorial of digits equals the number.

Key Concepts:

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