Strong number:
A Strong number is a special number in which the total of all digit factorials equals the number itself.
Ex: 145 the sum of factorial of digits = 1 ! + 4 ! +5 ! = 1 + 24 +125
To determine whether a given number is strong or not. We take each digit from the supplied number and calculate its factorial , we will do this for each digit of the number.
We do the sum of factorials once we have the factorial of all digits. If the total equals the supplied number, the given number is strong; otherwise, it is not.
Given a number the task is to check whether the given number is strong number or not.
Examples:
Example1:
Input:
given number = 145
Output:
The given number 145 is strong number
Example2:
Input:
given number = 176
Output:
The given number 176 is not a strong number
Python Program to Check Whether the given Number is Strong Number or Not
There are several ways to check whether the given number is strong number or not some of them are:
Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.
To determine if a number is a strong number or not, divide it into distinct units by each of its digits. The factorial of each of the digits is then computed. Then we will add the values received after the factorial operation for each of the digits. Finally, it must be determined whether this sum equals the supplied number. If the answer is yes, the number is a strong number.
Method #1:Using While loop and writing original factorial function
Approach:
- Obtain the desired number as input to determine if it is a strong number, or provide the input number as static.
- Let’s create a variable called N to hold this input number.
- Put the value of N in a temporary variable called tempNum.
- Set a variable, say totalSum to zero. This will save the factorial sum of each of N’s digits.
- The number’s final digit must be saved in a variable, such as last_Digit = N % 10.
- last_Digit factorial must be kept in a variable, say factNum.
- When the factorial of the last digit is found, it should be added to the totalSum = totalSum+ factNum
- Following each factorial operation, the number must be reduced in terms of units by dividing it by ten that is N = N /10
- Steps 4–7 should be repeated until N > 0.
Below is the implementation:
# python program to cheeck whether the given numner is strong number or not def checkStrongNumb(givenNumb): # Taking a variable totalSum and initializing it with 0 totalSum = 0 # Take a temporary variable and store the given number (givenNumb) # in the temporary variable tempNum tempNum = givenNumb # using while to extract digit by digit of the given number while(givenNumb): s = 1 factNum = 1 # Getting the last digit of the given number remainder = givenNumb % 10 # calculating the factorial of the digit(extracted by remainder variable) while(s <= remainder): factNum = factNum * s s = s + 1 # Adding the factorial to the totalSum totalSum = totalSum + factNum # Dividing the given number by 10 givenNumb = givenNumb//10 # checking if the totalSum is equal to the given number # if it is true then it is strong number then return true if(totalSum == tempNum): return True # if nothing is returned then it is not a strong number so return False return False # Given number given_numb = 145 # passing the givennumber to checkStrongNumb to check whether it is strong number or not if(checkStrongNumb(given_numb)): print("The given number", given_numb, "is strong number") else: print("The given number", given_numb, "is not a strong number")
Output:
The given number 145 is strong number
Method #2:Using While loop and factorial() function
We use the factorial() function to do the above problem quickly
Approach:
- Obtain the desired number as input to determine if it is a strong number, or provide the input number as static.
- Let’s create a variable called N to hold this input number.
- Put the value of N in a temporary variable called tempNum.
- Set a variable, say totalSum to zero. This will save the factorial sum of each of N’s digits.
- The number’s final digit must be saved in a variable, such as last_Digit = N % 10.
- Calculate the factorial of the last_Digit using math.factorial() function
- When the factorial of the last digit is found, it should be added to the totalSum = totalSum+ factNum
- Following each factorial operation, the number must be reduced in terms of units by dividing it by ten that is N = N /10
- Steps 4–7 should be repeated until N > 0.
Below is the implementation:
# importing math module import math # python program to cheeck whether the given numner is strong number or not def checkStrongNumb(givenNumb): # Taking a variable totalSum and initializing it with 0 totalSum = 0 # Take a temporary variable and store the given number (givenNumb) # in the temporary variable tempNum tempNum = givenNumb # using while to extract digit by digit of the given number while(givenNumb): s = 1 factNum = 1 # Getting the last digit of the given number remainder = givenNumb % 10 # calculating the factorial of the digit(extracted by remainder variable) # using math.fatorial function factNum = math.factorial(remainder) # Adding the factorial to the totalSum totalSum = totalSum + factNum # Dividing the given number by 10 givenNumb = givenNumb//10 # checking if the totalSum is equal to the given number # if it is true then it is strong number then return true if(totalSum == tempNum): return True # if nothing is returned then it is not a strong number so return False return False # Given number given_numb = 145 # passing the givennumber to checkStrongNumb to check whether it is strong number or not if(checkStrongNumb(given_numb)): print("The given number", given_numb, "is strong number") else: print("The given number", given_numb, "is not a strong number")
Output:
The given number 145 is strong number
Related Programs: