0
0
PythonProgramBeginner · 2 min read

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 user
2
Initialize a sum variable to zero
3
Loop from 1 to number-1 and check if the current number divides the input number evenly
4
If yes, add it to the sum
5
After the loop, compare the sum with the input number
6
If sum is greater, print that the number is abundant; else print it is not
💻

Code

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'

inumber % i == 0sum_of_divisors
1True1
2True3
3True6
4True10
5False10
6True16
7False16
8False16
9False16
10False16
11False16
💡

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.

ApproachTimeSpaceBest For
Simple loop 1 to n-1O(n)O(1)Small to medium numbers
List comprehension with sumO(n)O(n)Readable code, small numbers
Optimized sqrt(n) checkO(√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.