Last Updated on March 14, 2023 by Prepbytes
There are various types of special numbers and series in Mathematics such as Armstrong Numbers, Fibonacci Numbers, Strong Numbers, Palindrome Numbers, etc. Here we will discuss Strong Numbers in detail, Algorithms, and Code for Strong Numbers in Java and Python. So, let’s start.
Strong Numbers
“In Mathematics, Strong Numbers are defined as the number whose sum of the factorial of digits is equal to the number itself.” The following examples will help you in a clear understanding of the above statement.
Example 1 of Strong Numbers
Let us consider a number, 145.
Here, n = 145
and, the Sum of Factorial of digits is = 1! + 4! + 5!
= 1 + 24 + 120
= 145
Since the sum of the factorial of digits of the number is equal to the number. So, 145 is a Strong Number.
Example 2 of Strong Numbers
Now, let another number be 534.
Here, n = 534
and, the Sum of Factorial of digits is = 5! + 3! + 4!
= 120 + 6 + 24
= 150
Here, the sum of the factorial of digits of the number (i.e., 150) does not equal to the number (i.e., 534). So, 534 is not a Strong Number.
Now that you have understood the concept of Strong Numbers well, let’s advance to the different methods of Checking whether the given number is strong or not.
Method 1: Iterative Solution for Strong Numbers
The iterative solution for the identification of Strong Numbers is given below along with a stepwise algorithm and code for Strong Numbers in Python and Java.
Algorithm: How to Check for Strong Number
Here’s the algorithm to check if a number is a strong number:
Step 1: Take input from the user and store it in a variable num.
Step 2: Create three variables: original (to store the original value of num), sum (to store the sum of factorials of digits), and digit (to store the current digit of num).
Step 3: Set original equal to num.
Step 4: Initialize the sum to 0.
Step 5: Use a while loop to iterate through each digit of num:
a. Get the last digit of num using the modulus operator (%).
b. Calculate the factorial of that digit using a for a loop. Multiply each number from 1 to the digit to get the factorial.
c. Add the factorial to the sum variable and divide the number by 10 to move to the next digit.
Step 6: After the while loop, check if the sum variable is equal to the original variable. If it is, then the number is a strong number. Otherwise, it’s not a strong number.
Code Implementation for Strong Numbers in Java and Python
Here is the code for the above-mentioned algorithm for Checking Strong Numbers in Python and Java.
import java.util.*; public class StrongNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int original = num; int sum = 0; while (num > 0) { int digit = num % 10; int fact = 1; for (int i = 1; i <= digit; i++) { fact *= i; } sum += fact; num /= 10; } if (sum == original) { System.out.println(original + " is a strong number"); } else { System.out.println(original + " is not a strong number"); } sc.close(); } }
num = int(input()) original = num sum = 0 while num > 0: digit = num % 10 fact = 1 for i in range(1, digit+1): fact *= i sum += fact num //= 10 if sum == original: print(original, "is a strong number") else: print(original, "is not a strong number")
Input 1:
145
Output 1:
145 is a strong number
Input 2:
345
Output 2:
345 is not a strong number
Explanation:
- We first take input from the user and store its value in the variable num.
- We then create two variables: original, and sum. The variable original stores the original value of num, and sum stores the sum of factorials of digits.
- We then use a while loop to iterate through each digit of the number. Inside the while loop, we first get the last digit of the number using the modulus operator (%). We then calculate the factorial of that digit using a for loop. We multiply each number from 1 to the digit to get the factorial. We then add the factorial to the sum variable and divide the number by 10 to move to the next digit of the number.
- After the while loop, we check if the “sum” variable is equal to the “original” variable. If it is, then the number is a strong number, and we print a message saying so. If it’s not, we print a message saying that the number is not a strong number.
Method 2: Dynamic Programming Solution for Strong Numbers
A dynamic programming approach to checking if a number is a strong number involves calculating factorials of digits beforehand and storing them in an array. This reduces the number of computations required during the execution of the program to a large extent.
Algorithm: How to Check for Strong Number
Here is the algorithm for Checking for Strong Numbers using the Dynamic Programming Approach.
Step 1: Take input from the user and store it in a variable num.
Step 2: Create two variables: original (to store the original value of num) and sum (to store the sum of factorials of digits).
Step 3: Create a list called factorials to store the factorials of digits.
Set the first two elements of the factorials list to 1.
Step 4: Use a for loop to iterate from 2 to 9 (since factorials of 10 and above are not required in this case):
a. Calculate the factorial of the current number using the previous factorial value and append it to the factorials list.
Step 5: After calculating the factorials, use a while loop to iterate through each digit of the number:
a. Get the last digit of the number using the modulus operator (%).
b. Add the factorial of that digit (which we stored in the factorials list) to the sum variable and divide the number by 10 to move to the next digit.
Step 6: After the while loop, check if the sum variable is equal to the original variable. If it is, then the number is a strong number. Otherwise, it’s not a strong number.
Code Implementation for Strong Numbers in Python and Java
The code for Strong Number in Java and Python is given below:
import java.util.Scanner; public class StrongNumberDP { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int original = num; int[] factorials = new int[10]; factorials[0] = factorials[1] = 1; for (int i = 2; i < 10; i++) { factorials[i] = i * factorials[i - 1]; } int sum = 0; while (num > 0) { int digit = num % 10; sum += factorials[digit]; num /= 10; } if (sum == original) { System.out.println(original + " is a strong number"); } else { System.out.println(original + " is not a strong number"); } sc.close(); } }
num = int(input()) original = num factorials = [1, 1] for i in range(2, 10): factorials.append(i * factorials[i-1]) sum = 0 while num > 0: digit = num % 10 sum += factorials[digit] num //= 10 if sum == original: print(original, "is a strong number") else: print(original, "is not a strong number")
Input 1:
451
Output 1:
451 is not a strong number
Input 2:
145
Output 2:
145 is a strong number
Explanation:
- We first take input and store the value in the variable named “num”
- We then create two variables: original, and sum. The variable original stores the original value of num, and sum stores the sum of factorials of digits.
- We create an array called factorials to store the factorials of digits.
- We set the first two elements of the factorials array to 1 since 0! = 1! = 1.
- We then use a for loop to iterate from 2 to 9 (since factorials of 10 and above are not required in this case). Inside the for loop, we calculate the factorial of the current number using the previous factorial value and store it in the factorials array.
- After calculating the factorials, we follow the same steps as followed in the iterative approach.
Conclusion
In this article, we learned how to check if a number is a strong number using Java and Python. We also went through the algorithm to check a strong number. The code for both Java and Python is relatively simple and easy to understand. Moreover, to optimize our solution, we also discussed the Dynamic Programming Approach which helps in reducing the number of calculations for finding the factorial of the digits. By implementing the algorithm and code we provided, you can quickly determine whether a given number is a strong number or not.
Frequently Asked Questions(FAQs)
Here are some Frequently Asked Questions on Strong Numbers.
Ques 1. Is every perfect number a strong number?
Ans. No, not every perfect number is a strong number.
Ques 2. What is the largest known strong number?
Ans. The largest known strong number is 531,441.
Ques 3. How many strong numbers are there between 1 and 100?
Ans. There are only two strong numbers between 1 and 100, which are 1 and 145.
Ques 4. Can a negative number be a strong number?
Ans. No, a negative number cannot be a strong number because it has no factorial representation.