Strong Number in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Ever heard about Strong Numbers? They are extremely rare numbers that are equal to the sum of the factorial of all their digits.

In Python, we can check for strong numbers using different methods like using a while loop, for loop and hashing, recursion, and inbuilt math factorial() function.

What is a Strong Number?

If the sum of the factorial of all the digits of a number is equal to the number itself, that number is called a Strong Number.

Confused? Okay, I'll find a better way to explain this.

Take any number, for instance, let us take 145145. This number contains the digits 11, 44, and 55, their factorials will be 11, 2424, and 120120 respectively, and the total sum of the factorials would be 1+24+120=1451+24+120=145. The sum of the factorial of all the digits is equal to the number itself. Therefore, the number 145145 is a strong number.

Now, let us look at another example for clarity.

This time, we will take the number 154154. This number contains the digits 11, 55, and 44, their factorials will be 11, 120120, and 2424 respectively, and the total sum of the factorials would be 1+120+24=1451+120+24=145. The sum of the factorial of all the digits is not equal to the number itself. Therefore, the number 154154 is not a strong number :disappointed:.

Note: The factorial of a number nn is the product of all the numbers from 11 to nn.
It is denoted by the "!" (exclamation) sign.

Factorial of n=n!=1×2×3×...×n1×nn = n! = 1 \times 2 \times 3 \; \times \; ... \; \times \; n-1 \times n

For example, 6!=1×2×3×4×5×6=7206! = 1 \times 2 \times 3 \times 4 \times 5 \times 6 = 720

Examples of Strong Numbers

  • 145145: It is a strong number as the sum of the factorial of all its digits (1!+4!+5!1! + 4! + 5!) is equal to the number itself (145145).
  • 22: It is a strong number as the sum of the factorial of all its digits (2!2!) is equal to the number itself (22).
  • 4058540585: It is a strong number as the sum of the factorial of all its digits (4!+0!+5!+8!+5!4! + 0! + 5! + 8! + 5!) is equal to the number itself (4058540585).

The Problem

Create a Python Program to check if a given number is a strong number or not.

Approach to the Problem

  • Ask the user to enter an integer number.
  • Find the factorial of each digit in the given number and add them.
  • Check if the sum of the factorials is equal to the inputted number.
  • If yes, return "True", else return "False".
  • Print the output.
  • Exit.

Programs to Check if the Given Number is a Strong number in Python

In Python, there are various methods to check if a number is a strong number or not. Let us look at each of them:

Python Program to Check for Strong Number Using a While Loop

In this program, we will check for strong numbers using a while loop nested inside another while loop. The parent while loop is used to check for the strong number, and the nested while loop is used to calculate the factorial of a digit.

Steps of the Algorithm

  1. Initialize sumsum variable to be 00.
  2. Initialize temptemp variable to be nn.
  3. Run a while loop till temptemp is not 00.
  4. Inside the while loop, assign remrem to be temp%10temp\%10, initialize variables xx and factofacto to be 11.
  5. Run a nested while loop while xx is less than or equal to remrem inside the main while loop.
  6. Inside the nested while loop, multiply factofacto by xx and increment xx by 11 in each iteration.
  7. After the end of the nested while loop, add factofacto to sumsum and divide temptemp by 1010.
  8. End the main while loop.
  9. Check if sumsum is equal to nn, if yes, return True.
  10. Return false.

Implementation

Output 1

Output 2

Explanation

In output 1, the inputted number is 145145. This number contains the digits 11, 44, and 55, their factorials will be 11, 2424, and 120120 respectively, and the total sum of the factorials would be 1+24+120=1451+24+120=145. The sum of the factorial of all the digits is equal to the number itself. Therefore, the number 145145 is a strong number, as can be seen in the output.

In output 2, the inputted number is 154154. This number contains the digits 11, 55, and 44, their factorials will be 11, 120120, and 2424 respectively, and the total sum of the factorials would be 1+120+24=1451+120+24=145. The sum of the factorial of all the digits is equal to the number itself. Therefore, the number 154154 is not a strong number, as can be seen in the output.

Python Program to Check for Strong Number Using a For Loop and a Dictionary (Pre-Determined Factorials)

In this program, we will check for strong numbers using a for loop and a dictionary.
The for loop will be used for checking strong numbers and the dictionary will be used to store the factorial of digits from 00 to 99.

Steps of the Algorithm

At first, create a dictionary factofacto to store the factorial of digits from 00 to 99, where the key will be the digit and the value will be its factorial.

The isStrong() function

  1. Initialize sumsum variable to be 00.
  2. Iterate over the number (by typecasting it into a string) using a for loop, add facto[int(x)] to sum in each iteration.
  3. Check if sumsum is equal to nn, if yes, return True.
  4. Return false.

Implementation

Output 1

Output 2

Explanation

In output 1, the inputted number is 22. This number contains a single digit 22, its factorial will be 22, and the total sum of the factorials would be 22. The sum of the factorial of all the digits is equal to the number itself. Therefore, the number 22 is a strong number, as can be seen in the output.

In output 2, the inputted number is 3030. This number contains the digits 33, and 00, their factorials will be 66 and 00 respectively, and the total sum of the factorials would be 6+0=66+0=6. The sum of the factorial of all the digits is not equal to the number itself. Therefore, the number 3030 is not a strong number, as can be seen in the output.

Python Program to Check for Strong Number Using Recursion

In this program, we will check for a strong number using recursion. There will be two recursive functions in this program, isStrong() for checking strong numbers and factorial() for calculating the factorial of a number.

Steps of the Algorithm

The factorial()factorial() function It returns the factorial of the number nn passed to it.

  1. Check if nn is less than or equal to 11, if yes, return 11.
  2. If no, return nfactorial(n1)n * factorial(n - 1).

Initialize a global variable sumsum to be 00.

The isStrong()isStrong() function
It returns TrueTrue or FalseFalse for a given number nn is a strong number or not.

  1. Check if nn is not equal to 00, if true, assign remrem to be n%10n \% 10, factofacto to be factorial(rem)factorial(rem), add factofacto to sumsum, and lastly, call the isStrong()isStrong() function by passing n//10n//10 to it.
  2. Check if sumsum is equal to nn, if yes, return True.
  3. Return false.

Implementation

Output 1

Output 2

Explanation

In output 1, the inputted number is 4058540585. This number contains the digits 44, 00, 55, 88, and 55, their factorials will be 2424, 00, 120120, 4032040320, and 120120 respectively, and the total sum of the factorials would be 24+0+120+40320+120=4058524 + 0 + 120 + 40320 + 120 = 40585. The sum of the factorial of all the digits is equal to the number itself. Therefore, the number 4058540585 is a strong number, as can be seen in the output.

In output 2, the inputted number is 4084440844. This number contains the digits 44, 00, 88, 44, and 44, their factorials will be 2424, 00, 4032040320, 2424, and 2424 respectively, and the total sum of the factorials would be 24+0+40320+24+24=4039224 + 0 + 40320 + 24 + 24 = 40392. The sum of the factorial of all the digits is not equal to the number itself. Therefore, the number 4084440844 is not a strong number, as can be seen in the output.

Python Program to Check for Strong Number Using the Math factorial() Function

In this program, we will check for the strong numbers using a while loop, inside which we will use the math factorial() function to calculate the factorial of a digit.

Steps of the Algorithm

Import math module.

The isStrong()isStrong() function

  1. Initialize sumsum variable to be 00.
  2. Initialize temptemp variable to be nn.
  3. Run a while loop till temptemp is not 00.
  4. Inside the while loop, assign remrem to be temp%10temp\%10.
  5. Assign factofacto to be math.factorial(rem)math.factorial(rem).
  6. Add factofacto to sumsum and divide temptemp by 1010.
  7. End the while loop.
  8. Check if sumsum is equal to nn, if yes, return True.
  9. Return False.

Implementation

Output 1

Output 2

Explanation

In output 1, the inputted number is 11. This number contains a single digit 11, its factorial will be 11, and the total sum of the factorials would be 11. The sum of the factorial of all the digits is equal to the number itself. Therefore, the number 11 is a strong number, as can be seen in the output.

In output 2, the inputted number is 9191. This number contains the digits 99 and 11, their factorials will be 362880362880 and 11 respectively, and the total sum of the factorials would be 362880+1=362881362880 + 1 = 362881. The sum of the factorial of all the digits is not equal to the number itself. Therefore, the number 9191 is not a strong number, as can be seen in the output.

Check out this article to learn about Math Module in Python.

Conclusion

  • Strong numbers are integer numbers whose sum of the factorial of all their digits is equal to them.
  • Factorial of a number nn is equal to n×n1×n2×...×3×2×1n \times n - 1 \times n - 2 \; \times \; ... \; \times \; 3 \times 2 \times 1.
  • The math factorial() function returns the factorial of the number passed to it.
  • There are 4 ways to check if the given number is a strong number or not: Using While loops, Using For loop, Using Recursion, and Using the factorial() function.

Read More: