VOOZH about

URL: https://www.geeksforgeeks.org/dsa/abundant-number/

⇱ Abundant Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Abundant Number

Last Updated : 19 Oct, 2023

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 ..... 

👁 Abundant Numbers

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.


Output
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.


Output
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.

  • The idea is to create a boolean array of size n+1, where n is the number whose abundance needs to be checked. This boolean array is used to store the result of whether a number is abundant or not. Initially, all the elements in the array are set to false.
  • Then, the program loops through all the numbers from 1 to n, and for each number, it calculates the sum of its divisors. If the sum of divisors is greater than the number itself, the number is marked as abundant in the boolean array.
  • Finally, the function returns the value at index n in the boolean array, which tells whether the number n is abundant or not.
  • This approach uses dynamic programming because it stores the results of previously computed subproblems in the boolean array and uses them to compute the abundance of larger numbers. This avoids redundant computations and makes the program more efficient.

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

-"

Comment
Article Tags: