Bash Script to Check Strong Number with Output
Use a Bash script that calculates the sum of factorials of each digit in a number and compares it to the original number, like
if [ $sum -eq $original ]; then echo "$original is a strong number"; else echo "$original is not a strong number"; fi.Examples
Input145
Output145 is a strong number
Input123
Output123 is not a strong number
Input1
Output1 is a strong number
How to Think About It
To check if a number is strong, break it into digits, find the factorial of each digit, and add them up. If the sum equals the original number, it is a strong number.
Algorithm
1
Get input number2
Initialize sum to zero3
For each digit in the number, calculate its factorial4
Add each factorial to sum5
Compare sum with original number6
Print if it is a strong number or notCode
bash
read -p "Enter a number: " num sum=0 original=$num factorial() { fact=1 for (( i=1; i<=$1; i++ )); do fact=$((fact * i)) done echo $fact } while [ $num -gt 0 ]; do digit=$((num % 10)) fact=$(factorial $digit) sum=$((sum + fact)) num=$((num / 10)) done if [ $sum -eq $original ]; then echo "$original is a strong number" else echo "$original is not a strong number" fi
Output
Enter a number: 145
145 is a strong number
Dry Run
Let's trace 145 through the code
1
Initialize variables
num=145, sum=0, original=145
2
Process digit 5
factorial(5)=120, sum=0+120=120, num=14
3
Process digit 4
factorial(4)=24, sum=120+24=144, num=1
4
Process digit 1
factorial(1)=1, sum=144+1=145, num=0
5
Compare sum and original
sum=145, original=145, equal => strong number
| Digit | Factorial | Sum after addition |
|---|---|---|
| 5 | 120 | 120 |
| 4 | 24 | 144 |
| 1 | 1 | 145 |
Why This Works
Step 1: Calculate factorial of each digit
The factorial function multiplies numbers from 1 to the digit to get its factorial.
Step 2: Sum factorials
Each digit's factorial is added to sum to accumulate the total.
Step 3: Compare sum with original number
If sum equals the original number, it confirms the number is strong.
Alternative Approaches
Using recursive factorial function
bash
read -p "Enter a number: " num sum=0 original=$num factorial() { if [ $1 -le 1 ]; then echo 1 else prev=$(factorial $(( $1 - 1 ))) echo $(( $1 * prev )) fi } while [ $num -gt 0 ]; do digit=$((num % 10)) fact=$(factorial $digit) sum=$((sum + fact)) num=$((num / 10)) done if [ $sum -eq $original ]; then echo "$original is a strong number" else echo "$original is not a strong number" fi
Recursive factorial is elegant but slower and uses more stack memory.
Precompute factorials in an array
bash
read -p "Enter a number: " num sum=0 original=$num factorials=(1 1 2 6 24 120 720 5040 40320 362880) while [ $num -gt 0 ]; do digit=$((num % 10)) sum=$((sum + factorials[digit])) num=$((num / 10)) done if [ $sum -eq $original ]; then echo "$original is a strong number" else echo "$original is not a strong number" fi
Using a lookup array is faster and simpler for digits 0-9.
Complexity: O(d) time, O(1) space
Time Complexity
The script processes each digit once, so time grows linearly with the number of digits, O(d).
Space Complexity
Only a few variables and possibly a small array for factorials are used, so space is constant, O(1).
Which Approach is Fastest?
Using a precomputed factorial array is fastest because it avoids repeated factorial calculations.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative factorial calculation | O(d * n) where n is digit value | O(1) | Simple scripts, small inputs |
| Recursive factorial | O(d * n) | O(n) stack | Learning recursion, not performance |
| Precomputed factorial array | O(d) | O(1) | Fastest for digit factorial lookups |
Use a lookup array for factorials of digits 0-9 to speed up the script.
Forgetting to reset the number variable before the loop causes wrong calculations.