Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python Program to check Abundant Number

Abundant Number:

Abundant number is a number in which the sum of the proper divisors of the number is greater than the number itself.

To check for Abundant number, find and add the proper divisors of the number , compare the sum with the number if the sum is greater than the number then the number is an Abundant number else not an Abundant number.  Diffrence between sum and the number is known as abundant.

Example:

  • 12
    • 1*12
    • 2*6
    • 3*4
    • 1 + 2 + 3 + 4 + 6=16 > 12 (Abundant Number)
  • 21
    • 1*21
    • 3*7
    • 1 + 3 + 7=11 < 21 (Not an Abundant Number)
Finding Abundant Number using Python

Implementation:

  • Find the proper divisior of the given number.
  • Find the sum of all the divisors.
  • If the sum is greater than the number then then the number is Abundant Number.

Python Code:

#enter the number to check
print(‘Enter the number:’)
n=int(input())
sum=1 # 1 can divide any number 
for i in range(2,n):
    if(n%i==0):    #if number is divisible by i add the number 
        sum=sum+i 

if(sum>n):
    print(n,‘is Abundant Number’)

else:
    print(n,‘is not Abundant Number’)
Output:

Enter the number:

12

12 is Abundant Number

Optimal solution:

  • We can observe that divisor of a number always comes in pair. For example n=100 then all pairs are (1,100) , (2,50) , (4,25) , (5,20) , (10,10).
  • Using this fact we can speed up our program.

Python Code:

#enter the number to check
import math
print(‘Enter the number:’)
n=int(input())
sum=1 # 1 can divide any number 
i=2
while(i<=math.sqrt(n)):
    if(n%i==0):   #if number is divisible by i add the number
        if(n//i==i): # if quotient is equal to divisor add only one of them 
            sum=sum+i 
        else:
            sum=sum + i + n/i
    i=i+1
if(sum>n):
    print(n,‘is Abundant Number’)

else:
    print(n,‘is not Abundant Number’)
Output:

Enter the number:

21

21 is not Abundant Number