Primitive Abundant Number

A number N is said to be Primitive Abundant Number if N is an Abundant number and all it’s proper divisors are Deficient Numbers.

The first few Primitive Abundant Numbers are:

20, 70, 88, 104, 272, 304………

Check if N is a Primitive Abundant Number

Given a number N, the task is to find if this number is Primitive Abundant Number or not.

Examples:



Input: N = 20
Output: YES
Explanation:
Sum of 20’s proper divisors is – 1 + 2 + 4 + 5 + 10 = 22 > 20,
So, 20 is an abundant number.
The proper divisors of 1, 2, 4, 5 and 10 are0, 1, 3, 1 and 8 respectively,
Each of these numbers is a deficient number
Therefore, 20 is a primitive abundant number.

Input: N = 17
Output: No

Approach:

  1. Check if the number is an Abundant number or not, i.e, sum of all the proper divisors of the number denoted by sum(N) is greater than the value of the number N
  2. If the number is not abundant then return false else do the following
  3. Check if all proper divisors of N are Deficient Numbers or not, i.e, sum of all the divisors of the number denoted by divisorsSum(n) is less than twice the value of the number N.
  4. If both above conditions are true print “Yes” else print “No.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above
// approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to sum of divisors
int getSum(int n)
{
    int sum = 0;
  
    // Note that this loop
    // runs till square root of N
    for (int i = 1; i <= sqrt(n); i++) {
  
        if (n % i == 0) {
  
            // If divisors are equal,
            // take only one of them
            if (n / i == i)
                sum = sum + i;
  
            else // Otherwise take both
            {
                sum = sum + i;
                sum = sum + (n / i);
            }
        }
    }
  
    return sum;
}
  
// Function to check Abundant Number
bool checkAbundant(int n)
{
    // Return true if sum
    // of divisors is greater
    // than N.
    return (getSum(n) - n > n);
}
  
// Function to check Deficient Number
bool isDeficient(int n)
{
    // Check if sum(n) < 2 * n
    return (getSum(n) < (2 * n));
}
  
// Function to check all proper divisors
// of N is deficient number or not
bool checkPrimitiveAbundant(int num)
{
    // if number itself is not abundant
    // retuen false
    if (!checkAbundant(num)) {
        return false;
    }
  
    // find all divisors which divides 'num'
    for (int i = 2; i <= sqrt(num); i++) {
  
        // if 'i' is divisor of 'num'
        if (num % i == 0 && i != num) {
  
            // if both divisors are same then add
            // it only once else add both
            if (i * i == num) {
                if (!isDeficient(i)) {
                    return false;
                }
            }
            else if (!isDeficient(i) || !isDeficient(num / i)) {
                return false;
            }
        }
    }
  
    return true;
}
  
// Driver Code
int main()
{
  
    int n = 20;
    if (checkPrimitiveAbundant(n)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above
// approach
class GFG{ 
      
// Function to sum of divisors
static int getSum(int n)
{
    int sum = 0;
  
    // Note that this loop runs 
    // till square root of N
    for(int i = 1; i <= Math.sqrt(n); i++)
    {
       if (n % i == 0)
       {
             
           // If divisors are equal,
           // take only one of them
           if (n / i == i)
               sum = sum + i;
                 
           // Otherwise take both
           else
           {
               sum = sum + i;
               sum = sum + (n / i);
           }
       }
    }
    return sum;
}
  
// Function to check Abundant Number
static boolean checkAbundant(int n)
{
      
    // Return true if sum
    // of divisors is greater
    // than N.
    return (getSum(n) - n > n);
}
  
// Function to check Deficient Number
static boolean isDeficient(int n)
{
      
    // Check if sum(n) < 2 * n
    return (getSum(n) < (2 * n));
}
  
// Function to check all proper divisors
// of N is deficient number or not
static boolean checkPrimitiveAbundant(int num)
{
      
    // If number itself is not abundant
    // retuen false
    if (!checkAbundant(num))
    {
        return false;
    }
  
    // Find all divisors which divides 'num'
    for(int i = 2; i <= Math.sqrt(num); i++)
    {
          
       // if 'i' is divisor of 'num'
       if (num % i == 0 && i != num)
       {
             
           // if both divisors are same then 
           // add it only once else add both
           if (i * i == num)
           {
               if (!isDeficient(i))
               {
                   return false;
               }
           }
           else if (!isDeficient(i) || 
                    !isDeficient(num / i))
           {
               return false;
           }
       }
    }
    return true;
}
  
// Driver Code
public static void main(String[] args) 
{
    int n = 20;
      
    if (checkPrimitiveAbundant(n))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
  
// This code is contributed by Ritik Bansal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above
# approach
import math
  
# Function to sum of divisors
def getSum(n):
    sum = 0
      
    # Note that this loop
    # runs till square root of N
    for i in range(1, int(math.sqrt(n) + 1)):
        if (n % i == 0):
              
            # If divisors are equal,
            # take only one of them
            if (n // i == i):
                sum = sum + i
            else:
                  
                # Otherwise take both
                sum = sum + i
                sum = sum + (n // i)
    return sum
  
# Function to check Abundant Number
def checkAbundant(n):
      
    # Return True if sum
    # of divisors is greater
    # than N.
    if (getSum(n) - n > n):
        return True
    return False
  
# Function to check Deficient Number
def isDeficient(n):
      
    # Check if sum(n) < 2 * n
    if (getSum(n) < (2 * n)):
        return True
    return False
  
# Function to check all proper divisors
# of N is deficient number or not
def checkPrimitiveAbundant(num):
      
    # if number itself is not abundant
    # retuen False
    if not checkAbundant(num):
        return False
      
    # find all divisors which divides 'num'
    for i in range(2, int(math.sqrt(num) + 1)):
          
        # if 'i' is divisor of 'num'
        if (num % i == 0 and i != num):
            # if both divisors are same then add
            # it only once else add both
            if (i * i == num):
                if (not isDeficient(i)):
                    return False
            elif (not isDeficient(i) or 
                  not isDeficient(num // i)): 
                return False
    return True
  
# Driver Code
n = 20
if (checkPrimitiveAbundant(n)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by shubhamsingh10

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above
// approach
using System;
class GFG{ 
      
// Function to sum of divisors
static int getSum(int n)
{
    int sum = 0;
  
    // Note that this loop runs 
    // till square root of N
    for(int i = 1; i <= Math.Sqrt(n); i++)
    {
       if (n % i == 0)
       {
             
           // If divisors are equal,
           // take only one of them
           if (n / i == i)
               sum = sum + i;
             
           // Otherwise take both
           else
           {
               sum = sum + i;
               sum = sum + (n / i);
           }
       }
    }
    return sum;
}
  
// Function to check Abundant Number
static bool checkAbundant(int n)
{
      
    // Return true if sum
    // of divisors is greater
    // than N.
    return (getSum(n) - n > n);
}
  
// Function to check Deficient Number
static bool isDeficient(int n)
{
      
    // Check if sum(n) < 2 * n
    return (getSum(n) < (2 * n));
}
  
// Function to check all proper divisors
// of N is deficient number or not
static bool checkPrimitiveAbundant(int num)
{
      
    // If number itself is not abundant
    // retuen false
    if (!checkAbundant(num))
    {
        return false;
    }
  
    // Find all divisors which divides 'num'
    for(int i = 2; i <= Math.Sqrt(num); i++)
    {
         
       // If 'i' is divisor of 'num'
       if (num % i == 0 && i != num)
       {
             
           // If both divisors are same then 
           // add it only once else add both
           if (i * i == num)
           {
               if (!isDeficient(i))
               {
                   return false;
               }
           }
           else if (!isDeficient(i) || 
                    !isDeficient(num / i))
           {
               return false;
           }
       }
    }
    return true;
}
  
// Driver Code
public static void Main() 
{
    int n = 20;
      
    if (checkPrimitiveAbundant(n))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
  
// This code is contributed by Code_Mech

chevron_right


Output:

Yes

References: https://en.wikipedia.org/wiki/Primitive_abundant_number

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.