livepython.net

294. Python Program to Check If a Number Is a Strong Number

MediumPhase 2 · Practice

You know every tool for this one: nested for loops, a running total and if. It just takes a few steps.

Overview

A strong number is equal to the sum of the factorials of its digits. Checks like this are common interview questions, because they need a loop inside a loop.

Problem Statement

Ivan reads about strong numbers in a puzzle book and wants to test some numbers. Read a whole number n and check if it is a strong number:

  • The factorial of a digit is that digit times every whole number below it, down to 1. The factorial of 5 is 5 * 4 * 3 * 2 * 1, which is 120.
  • The factorial of 0 is 1. This is a rule of maths.
  • Work out the factorial of every digit of n, and add them all up.
  • If the total equals n, then n is a strong number.

1 and 2 are strong numbers too: the factorial of 1 is 1, and the factorial of 2 is 2.

Print one line. For a strong number, print 145 is a strong number, with your number in place of 145. Otherwise, print 146 is not a strong number, with your number in place of 146.

Examples

Example 1. Input 145 gives:

145 is a strong number

Why: the factorials of 1, 4 and 5 are 1, 24 and 120. 1 + 24 + 120 = 145, the number itself.

Example 2. Input 146 gives:

146 is not a strong number

Why: the factorials of 1, 4 and 6 are 1, 24 and 720. They add up to 745, not 146.

Input

One whole number n, 1 or more. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line, as shown above. The tests try 8 different inputs.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 10 · For Loops
solution.py
# write your code here

EXPECTED OUTPUT

1145 is a strong number

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.