![]() |
VOOZH | about |
A number n is said to be an Abundant Number if the sum of all the proper divisors of the number denoted by sum(n) is greater than the value of the number n. And the difference between these two values is called abundance.
Mathematically, if the below condition holds the number is said to be an Abundant number:
sum(n)> n
abundance = sum(n) - n
sum(n): aliquot sum - The sum of all proper divisors of n
Given a number n, our task is to find if this number is an Abundant number or not.
The first few Abundant Numbers are: 12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66 .....
Examples:
Input: 21
Output: NO
Input: 12
Output: YES
Input: 17
Output: NO
Method 1: A Simple solution is to iterate all the numbers from 1 to n-1 and check if the number divides n and calculate the sum. Check if this sum is greater than n or not.
YES
Time Complexity: O(n) for a given number n.
Auxiliary Space: O(1)
Optimized Solution:
If we observe carefully, the divisors of the number n are present in pairs. For example if n = 100, then all the pairs of divisors are: (1,100), (2,50), (4,25), (5,20), (10,10)
Using this fact we can speed up our program. While checking divisors we will have to be careful if there are two equal divisors as in the case of (10, 10). In such a case, we will take only one of them in the calculation of the sum.
Subtract the number n from the sum of all divisors to get the sum of proper divisors.
YES NO
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Approach 3: Dynamic Programming:
The approach uses dynamic programming to determine if a number is abundant or not.
Here is the Code of above approach:
Output
YES
Time Complexity: O(n*sqrt(n)). for a given number n.
Auxiliary Space: O(N)
References:
https://en.wikipedia.org/wiki/Abundant_number
-"