Python Program to Check Abundant Number
To check if a number is abundant in Python, sum all its proper divisors using a loop and compare if the sum is greater than the number with
sum_of_divisors > number.Examples
Input12
Output12 is an abundant number
Input15
Output15 is not an abundant number
Input1
Output1 is not an abundant number
How to Think About It
To check if a number is abundant, find all its divisors except itself by checking which numbers divide it evenly. Add these divisors together. If the total sum is more than the original number, it is abundant; otherwise, it is not.
Algorithm
1
Get the input number from the user2
Initialize a sum variable to zero3
Loop from 1 to number-1 and check if the current number divides the input number evenly4
If yes, add it to the sum5
After the loop, compare the sum with the input number6
If sum is greater, print that the number is abundant; else print it is notCode
python
number = int(input('Enter a number: ')) sum_of_divisors = 0 for i in range(1, number): if number % i == 0: sum_of_divisors += i if sum_of_divisors > number: print(f'{number} is an abundant number') else: print(f'{number} is not an abundant number')
Output
Enter a number: 12
12 is an abundant number
Dry Run
Let's trace the number 12 through the code
1
Input number
number = 12
2
Initialize sum
sum_of_divisors = 0
3
Loop and check divisors
Check i from 1 to 11; add i to sum if 12 % i == 0
4
Sum divisors
Divisors: 1, 2, 3, 4, 6; sum = 1+2+3+4+6 = 16
5
Compare sum with number
16 > 12 is True
6
Print result
'12 is an abundant number'
| i | number % i == 0 | sum_of_divisors |
|---|---|---|
| 1 | True | 1 |
| 2 | True | 3 |
| 3 | True | 6 |
| 4 | True | 10 |
| 5 | False | 10 |
| 6 | True | 16 |
| 7 | False | 16 |
| 8 | False | 16 |
| 9 | False | 16 |
| 10 | False | 16 |
| 11 | False | 16 |
Why This Works
Step 1: Find divisors
We find all numbers less than the input that divide it evenly using number % i == 0.
Step 2: Sum divisors
We add these divisors to get the total sum of proper divisors.
Step 3: Compare sum and number
If the sum is greater than the number, it means the number is abundant.
Alternative Approaches
Using list comprehension and sum function
python
number = int(input('Enter a number: ')) divisors = [i for i in range(1, number) if number % i == 0] sum_of_divisors = sum(divisors) if sum_of_divisors > number: print(f'{number} is an abundant number') else: print(f'{number} is not an abundant number')
This approach is concise and uses Python's list comprehension and built-in sum for readability.
Optimized divisor search up to sqrt(number)
python
import math number = int(input('Enter a number: ')) sum_of_divisors = 1 if number > 1 else 0 for i in range(2, int(math.sqrt(number)) + 1): if number % i == 0: sum_of_divisors += i if i != number // i: sum_of_divisors += number // i if sum_of_divisors > number: print(f'{number} is an abundant number') else: print(f'{number} is not an abundant number')
This method reduces the number of checks by only going up to the square root, improving performance for large numbers.
Complexity: O(n) time, O(1) space
Time Complexity
The program checks all numbers from 1 to n-1 to find divisors, so it runs in O(n) time.
Space Complexity
It uses a fixed amount of extra space for sum and loop variables, so O(1) space.
Which Approach is Fastest?
The optimized method checking divisors up to the square root runs faster, approximately O(√n), compared to the simple O(n) loop.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Simple loop 1 to n-1 | O(n) | O(1) | Small to medium numbers |
| List comprehension with sum | O(n) | O(n) | Readable code, small numbers |
| Optimized sqrt(n) check | O(√n) | O(1) | Large numbers, better performance |
Check divisors only up to half of the number or its square root to save time.
Including the number itself as a divisor, which should be excluded when checking abundance.