Bash Script to Check Perfect Number with Output
Use a Bash script that sums all divisors of a number except itself and compares the sum to the number with code like:
sum=0; for ((i=1; i. Examples
Input6
Output6 is perfect
Input28
Output28 is perfect
Input10
Output10 is not perfect
How to Think About It
To check if a number is perfect, find all its divisors except itself by checking which numbers divide it evenly using the modulo operator. Add these divisors together and compare the sum to the original number. If they match, the number is perfect.
Algorithm
1
Get the input number.2
Initialize a sum variable to zero.3
Loop from 1 to one less than the number.4
For each number, check if it divides the input number evenly.5
If yes, add it to the sum.6
After the loop, compare the sum to the input number and print if it is perfect or not.Code
bash
read -p "Enter a number: " num sum=0 for ((i=1; i<num; i++)); do if (( num % i == 0 )); then sum=$((sum + i)) fi done if (( sum == num )); then echo "$num is perfect" else echo "$num is not perfect" fi
Output
Enter a number: 6
6 is perfect
Dry Run
Let's trace the number 6 through the code
1
Initialize sum
sum=0
2
Loop i from 1 to 5
Check divisors: i=1,2,3,4,5
3
Add divisors to sum
i=1 divides 6, sum=1; i=2 divides 6, sum=3; i=3 divides 6, sum=6; i=4 no; i=5 no
4
Compare sum to number
sum=6 equals num=6, so 6 is perfect
| i | num % i | sum |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 3 |
| 3 | 0 | 6 |
| 4 | 2 | 6 |
| 5 | 1 | 6 |
Why This Works
Step 1: Find divisors
The script uses num % i == 0 to find numbers that divide the input without remainder.
Step 2: Sum divisors
It adds all such divisors to sum to get the total of proper divisors.
Step 3: Check perfection
If the sum equals the original number, the number is perfect, so it prints the result.
Alternative Approaches
Using a while loop
bash
read -p "Enter a number: " num sum=0 i=1 while (( i < num )); do if (( num % i == 0 )); then sum=$((sum + i)) fi ((i++)) done if (( sum == num )); then echo "$num is perfect" else echo "$num is not perfect" fi
This uses a while loop instead of for; functionally the same but some prefer while for clarity.
Using a function
bash
is_perfect() {
local num=$1
local sum=0
for ((i=1; i<num; i++)); do
if (( num % i == 0 )); then
sum=$((sum + i))
fi
done
[[ $sum -eq $num ]]
}
read -p "Enter a number: " n
if is_perfect "$n"; then
echo "$n is perfect"
else
echo "$n is not perfect"
fiEncapsulates logic in a function for reuse and cleaner main code.
Complexity: O(n) time, O(1) space
Time Complexity
The script loops from 1 to n-1, so it runs in linear time O(n).
Space Complexity
Only a few variables are used, so space complexity is constant O(1).
Which Approach is Fastest?
All approaches use similar loops; using a function or while loop does not affect time complexity but may improve readability.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop | O(n) | O(1) | Simple and clear scripts |
| While loop | O(n) | O(1) | Preference for while syntax |
| Function encapsulation | O(n) | O(1) | Reusable and modular code |
Use modulo
% to find divisors easily in Bash loops.Forgetting to exclude the number itself when summing divisors causes incorrect results.