Last Updated on May 31, 2023 by Prepbytes
In the vast world of mathematics, certain numbers hold secrets and exhibit peculiar patterns that have captivated the minds of both mathematicians and enthusiasts alike. Among these intriguing numerical phenomena lies the concept of "strong numbers." Embarking on a journey to understand and explore these enigmatic figures unveils a fascinating realm of calculations and patterns, where the interplay of digits and factorials creates a captivating tapestry.
Strong numbers possess a unique property – the sum of the factorials of their individual digits is equal to the number itself. This intriguing characteristic has sparked curiosity and inspired many to dive deep into the intricacies of these special numbers. By unraveling the mysteries of strong numbers, we gain not only a profound understanding of their mathematical essence but also an opportunity to flex our programming muscles using Python.
What is a Strong Number?
A number is referred to be a Strong Number if the factorial sum of all its digits equals the number itself.
Confused? Okay, I’ll come up with a better explanation for this.
Take any number; let’s use 145 as an example. Since this number contains the digits 1, 4, and 5, their respective factorials will be 1, 24, and 120, and their sum will be 145. The number itself is equal to the factorial of all its digits added together. 145 is a powerful number as a result.
For further clarification, let’s look at another case.
We’ll use the number 154 this time. This number has the digits 1, 5, and 4, and their respective factorials are 1, 120, and 24. The sum of their factorials is 1+120+24, which equals 145. The number itself is not equal to the factorial of all its digits added together. Consequently, 154 is not a strong number.
Note: The factorial of a number n is the product of all the numbers from 1 to n.
- It is denoted by the "!" (exclamation) sign.
- Factorial of n=n!=1×2×3×…×n−1×n
- For example, 6!=1×2×3×4×5×6=720
Examples of Strong Numbers
145: It is a strong number as the sum of the factorial of all its digits (1!+4!+5!) is equal to the number itself (145).
2: It is a strong number as the sum of the factorial of all its digits (2!) is equal to the number itself (2).
40585: It is a strong number as the sum of the factorial of all its digits (4!+0!+5!+8!+5!) is equal to the number itself (40585).
Create a Python Program to check if a given number is a strong number or not.
Approach to the Problem
- Enter Input as integer from the user.
- Each digit in the provided number has a factorial, which you must find and then add.
- Verify that the inputted number equals the total of the factorials.
- Return "True" if the answer is "yes," else, return "False".
- Print the results.
- Exit.
Programs to Check if the Given Number is a Strong number in Python
There are several ways to determine whether a number is a strong number or not in Python. Let’s examine each one separately:
Python Program to Check for Strong Number Using a While Loop
In this program, a while loop nested inside another while loop will be used to check for strong numbers. The factorial of a digit is calculated using the nested while loop, while the parent while loop is used to check for the strong number.
Steps of the Algorithm
- Initialize sum variable to be 0.
- Initialize temp variable to be n.
- Run a while loop till temp is not 0.
- Inside the while loop, assign rem to be temp%10, initialize variables x and facto to be 1.
- Run a nested while loop while x is less than or equal to rem inside the main while loop.
- Inside the nested while loop, multiply facto by x and increment x by 1 in each iteration.
- After the end of the nested while loop, add facto to sum and divide temp by 10.
- End the main while loop.
- Check if sum is equal to n, if yes, return True.
- Return false.
Code Implementation
def isStrong(n: int) ->bool: # Number to store the sum of the factorials sum = 0; temp = n while temp: # The last digit of temp/n rem = temp%10 x = 1 facto = 1 # Factorial of the last digit of temp/n while x <= rem: facto *= x x += 1 # Accumulating the factorials of all the digits in the inputted number sum += facto # Removing the last digit of the number temp //= 10 # Return True if the sum of the factorial of all the digits is equal to the given number, then it is a strong number if sum == n: return True # if the sum of the factorial of all the digits is not equal to the given number, then it is not a strong number return False # Inputting an integer number num = int(input("Enter a number: ")) # Calling the isStrong() function ans = isStrong(num) # If the inputted number is a strong number if ans: print(f"{num} is a strong number") # If the inputted number is not a strong number else: print(f"{num} is not a strong number")
Input
145
Output
145 is a strong number
Explanation
In output 1, the inputted number is 145. This number contains the digits 1, 4, and 5, their factorials will be 1, 24, and 120 respectively, and the total sum of the factorials would be 1+24+120=145. The sum of the factorial of all the digits is equal to the number itself. Therefore, the number 145 is a strong number, as can be seen in the output.
Python Program to Check for Strong Number Using a For Loop and a Dictionary (Predetermined 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 0 to 9.
Steps of the Algorithm
- At first, create a dictionary facto to store the factorial of digits from
- 0 to 9, where the key will be the digit and the value will be its factorial.
- The isStrong() function
- Initialize sum variable to be 0.
- Iterate over the number (by typecasting it into a string) using a for loop, add facto[int(x)] to sum in each iteration.
- Check if sum is equal to n, if yes, return True.
- Return false.
Code Implementation
facto = {0: 1, 1: 1, 2: 2, 3: 6, 4: 24, 5: 120, 6: 720, 7: 5040, 8: 40320, 9: 362880} # A function to check whether the given number is a strong number or not def isStrong(n: int) ->bool: # Number to store the sum of the factorials sum = 0; # Accumulating the factorials of all the digits in the inputted number for x in str(n): sum += facto[int(x)] # Return True if the sum of the factorial of all the digits is equal to the given number, then it is a strong number if sum == n: return True # if the sum of the factorial of all the digits is not equal to the given number, then it is not a strong number return False # Inputting an integer number num = int(input("Enter a number: ")) # Calling the isStrong() function ans = isStrong(num) # If the inputted number is a strong number if ans: print(f"{num} is a strong number") # If the inputted number is not a strong number else: print(f"{num} is not a strong number")
Input
2
Output
2 is a strong number
Explanation
In output, the inputted number is 2. This number contains a single digit 2, its factorial will be 2, and the total sum of the factorials would be 2. The sum of the factorial of all the digits is equal to the number itself. Therefore, the number 2 is 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() function It returns the factorial of the number n passed to it.
- Check if n is less than or equal to 1, if yes, return 1.
- If no, return n∗factorial(n−1).
- Initialize a global variable sum to be 0.
- The isStrong() function.
- It returns True or False for a given number n is a strong number or not.
- Check if n is not equal to 0, if true, assign rem to be n%10, facto to be factorial(rem), add facto to sum, and lastly, call the isStrong() function by passing n//10 to it.
- Check if sum is equal to n, if yes, return True.
- Return false.
Code Implementation
def factorial(n: int) ->int: # If n is 1 or 0 if n <= 1: return 1 # If n is greater than 1 else: # Recursively calling the factorial function return n * factorial(n-1) # Number to store the sum of the factorials sum = 0; # A function to check whether the given number is a strong number or not def isStrong(n: int) ->bool: # To avoid resetting the sum to 0 in every function call global sum # If n is not 0 if n: # Last digit of the given number n rem = n % 10 # Finding the factorial of the last digit facto = factorial(rem) # Accumulating the factorials of all the digits of the given number sum += facto # Recursively calling the isStrong() function isStrong(n//10) # Return True if the sum of the factorial of all the digits is equal to the given number, then it is a strong number if sum == n: return True # If the sum of the factorial of all the digits is not equal to the given number, then it is not a strong number return False # Inputting an integer number num = int(input("Enter a number: ")) # Calling the isStrong() function ans = isStrong(num) # If the inputted number is a strong number if ans: print(f"{num} is a strong number") # If the inputted number is not a strong number else: print(f"{num} is not a strong number") # Strong number in python
Input
40585
Output
40585 is a strong number
Explanation
In output 1, the inputted number is 40585. This number contains the digits
4, 0, 5, 8, and 5, their factorials will be 24, 0, 120, 40320, and 120 respectively, and the total sum of the factorials would be 24+0+120+40320+120=40585. The sum of the factorial of all the digits is equal to the number itself. Therefore, the number 40585 is 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() function
- Initialize sum variable to be 0.
- Initialize temp variable to be n.
- Run a while loop till temp is not 0.
- Inside the while loop, assign rem to be temp%10.
- Assign facto to be math.factorial(rem).
- Add facto to sum and divide temp by 10.
- End the while loop.
- Check if sum is equal to n, if yes, return True.
- Return False.
Code Implementation
# Python program to check if a given number is a strong number or not (Using factorial() function) # Importing the math module import math # A function to check whether the given number is a strong number or not def isStrong(n: int) ->bool: # Number to store the sum of the factorials sum = 0; temp = n while temp: # The last digit of temp/n rem = temp%10 # Finding factorial using the inbuilt factorial() function facto = math.factorial(rem) # Accumulating the factorials of all the digits in the inputted number sum += facto # Removing the last digit of the number temp //= 10 # Return True if the sum of the factorial of all the digits is equal to the given number, then it is a strong number if sum == n: return True # if the sum of the factorial of all the digits is not equal to the given number, then it is not a strong number return False # Inputting an integer number num = int(input("Enter a number: ")) # Calling the isStrong() function ans = isStrong(num) # If the inputted number is a strong number if ans: print(f"{num} is a strong number") # If the inputted number is not a strong number else: print(f"{num} is not a strong number")
Input
1
Output
1 is a strong number
Explanation
In output 1, the inputted number is 1. This number contains a single digit 1, its factorial will be 1, and the total sum of the factorials would be 1. The sum of the factorial of all the digits is equal to the number itself. Therefore, the number 1 is a strong number, as can be seen in the output.
Conclusion
In conclusion, understanding and implementing strong numbers in Python can greatly enhance one’s programming skills and problem-solving abilities. Strong numbers provide a fascinating mathematical concept that can be effectively utilized in various programming tasks, such as checking the strength of a given number and solving related puzzles.
Through this article, we have explored the definition and characteristics of strong numbers, including their prime factorization and digit factorials. We have also examined a step-by-step approach to implementing a Python program to determine if a number is strong or not.
By employing concepts like loops, conditionals, and functions, we have built a robust solution that can handle a wide range of input values. Additionally, we have discussed potential optimizations and ways to extend the program’s functionality.
FAQ’s
Q1: What happens if I pass a negative number to check for strongness?
Ans. Negative numbers are not considered strong numbers because the factorial of a negative number is not defined. Therefore, if you pass a negative number to the strong number checking function, it will return False.
Q2: Can a number be both strong and prime at the same time?
Ans. Yes, it is possible for a number to be both strong and prime. For example, the number 40585 is both a strong number (4! + 0! + 5! + 8! + 5! = 40585) and a prime number.
Q3: Are there any optimized techniques for working with strong numbers in Python?
Ans. Yes, there are several optimizations you can apply when working with strong numbers in Python. These include avoiding unnecessary calculations by storing pre-computed factorials, implementing a digit-wise approach instead of converting the number to a string, and using efficient algorithms for prime factorization.
Q4: Can I find all strong numbers within a given range in Python?
Ans. Yes, you can find all strong numbers within a given range in Python. You can iterate through the range and check each number using the strong number checking function. If a number satisfies the strong number condition, you can add it to a list or perform any other desired action.