Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Primitive Abundant Number
Next article icon

Primitive Abundant Number

Last Updated : 04 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 
 


 

Recommended: Please try your approach on {IDE} first, before moving on to the solution.


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++
// 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     // return 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; } 
Java
// 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     // return 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 
Python3
# 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     # return 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 
C#
// 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     // return 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 
JavaScript
<script> // Javascript implementation of the above // approach       // Function to sum of divisors     function getSum( n) {         let sum = 0;          // Note that this loop runs         // till square root of N         for ( let 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     function checkAbundant( n) {          // Return true if sum         // of divisors is greater         // than N.         return (getSum(n) - n > n);     }      // Function to check Deficient Number     function isDeficient( 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     function checkPrimitiveAbundant( num) {          // If number itself is not abundant         // return false         if (!checkAbundant(num)) {             return false;         }          // Find all divisors which divides 'num'         for ( let 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               let n = 20;          if (checkPrimitiveAbundant(n)) {             document.write("Yes");         } else {             document.write("No");         }  // This code contributed by aashish1995  </script> 

Output
Yes

Time Complexity: O(N1/2)

Auxiliary Space: O(1)

Approach 2: Dynamic Programming:

  • In this approach, we use dynamic programming to memoize the results of whether a number is abundant or not. We first initialize a DP array dp with all elements set to -1 to indicate that we haven't computed the result for that number yet.
  • We then define a sum_of_divisors function that computes the sum of divisors of a given number. We use this function to check whether a number is abundant or not. If the result for a particular number n is already present in the DP array, we return that result directly. Otherwise, we compute the result using the sum_of_divisors function and store it in the DP array for future use.
  • Finally, we check if the given number is abundant or not using the is_abundant function and print "Yes" or "No" accordingly.

Here is the code below:

C++
#include<bits/stdc++.h> using namespace std;  const int N = 1e5 + 5; int dp[N];  // Function to calculate sum of divisors int sum_of_divisors(int n) {     int sum = 1;     for (int i = 2; i * i <= n; i++) {         if (n % i == 0) {             sum += i;             if (n / i != i) {                 sum += n / i;             }         }     }     return sum; }  // Function to check if a number is Abundant or not bool checkPrimitiveAbundant(int n) {     if (dp[n] != -1) {         return dp[n];     }     return dp[n] = (sum_of_divisors(n) > n); }  int main() {     memset(dp, -1, sizeof(dp));      int n = 20;     if (checkPrimitiveAbundant(n)) {         cout << "Yes";     }     else {         cout << "No";     }     return 0; } 
Java
import java.util.*;  public class Main {     static final int N = 100000;     static int[] dp = new int[N];      // Function to calculate sum of divisors     static int sumOfDivisors(int n) {         int sum = 1;         for (int i = 2; i * i <= n; i++) {             if (n % i == 0) {                 sum += i;                 if (n / i != i) {                     sum += n / i;                 }             }         }         return sum;     }      // Function to check if a number is Abundant or not     static boolean checkPrimitiveAbundant(int n) {         if (dp[n] != 0) {             return dp[n] == 1;         }         return (dp[n] = (sumOfDivisors(n) > n) ? 1 : -1) == 1;     }      public static void main(String[] args) {         Arrays.fill(dp, 0);          int n = 20;         if (checkPrimitiveAbundant(n)) {             System.out.println("Yes");         } else {             System.out.println("No");         }     } } 
Python
N = 100005 dp = [-1] * N  # Function to calculate sum of divisors def sum_of_divisors(n):     sum = 1     for i in range(2, int(n**0.5) + 1):         if n % i == 0:             sum += i             if n // i != i:                 sum += n // i     return sum  # Function to check if a number is Abundant or not def checkPrimitiveAbundant(n):     if dp[n] != -1:  # Check if the result is already calculated and stored in dp array         return dp[n]     dp[n] = sum_of_divisors(n) > n  # Store the result of abundant check in dp array     return dp[n]  if __name__ == "__main__":     n = 20     if checkPrimitiveAbundant(n):         print("Yes")  # If n is primitive abundant, print "Yes"     else:         print("No")  # Otherwise, print "No" 
C#
using System;  public class GFG {     const int N = 100000;     static int[] dp = new int[N];      // Function to calculate sum of divisors     static int SumOfDivisors(int n)     {         int sum = 1;         for (int i = 2; i * i <= n; i++)         {             if (n % i == 0)             {                 sum += i;                 if (n / i != i)                 {                     sum += n / i;                 }             }         }         return sum;     }      // Function to check if a number is Abundant or not     static bool CheckPrimitiveAbundant(int n)     {         if (dp[n] != 0)         {             return dp[n] == 1;         }         return (dp[n] = (SumOfDivisors(n) > n) ? 1 : -1) == 1;     }      public static void Main(string[] args)     {         Array.Fill(dp, 0);          int n = 20;         if (CheckPrimitiveAbundant(n))         {             Console.WriteLine("Yes");         }         else         {             Console.WriteLine("No");         }     } }  // This Code is Contributed by Dwaipayan Bandyopadhyay 
JavaScript
const N = 1e5 + 5; let dp = new Array(N);  // Function to calculate sum of divisors function sum_of_divisors(n) {     let sum = 1;     for (let i = 2; i * i <= n; i++) {         if (n % i == 0) {             sum += i;             if (n / i != i) {                 sum += n / i;             }         }     }     return sum; }  // Function to check if a number is Abundant or not function checkPrimitiveAbundant(n) {     if (dp[n] != undefined) {         return dp[n];     }     return dp[n] = (sum_of_divisors(n) > n); }  let n = 20; if (checkPrimitiveAbundant(n)) {     console.log("Yes"); } else {     console.log("No"); } 

Output: 

Yes

Time Complexity: O(N log log N), where N is the input number.

Auxiliary Space: O(N), as we need to create an array of size N to store the sum of divisors for all numbers from 1 to N.

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


Next Article
Primitive Abundant Number

S

spp____
Improve
Article Tags :
  • Mathematical
  • DSA
  • series
  • divisors
Practice Tags :
  • Mathematical
  • series

Similar Reads

    Primitive root of a prime number n modulo n
    Given a prime number n, the task is to find its primitive root under modulo n. The primitive root of a prime number n is an integer r between[1, n-1] such that the values of r^x(mod n) where x is in the range[0, n-2] are different. Return -1 if n is a non-prime number. Examples: Input : 7 Output : S
    15 min read
    Almost Perfect Number
    Given a number n, check it is the Almost Perfect number or not. Almost perfect number is a natural number whose sum of all divisors including 1 and the number itself is equal to 2n - 1.Example : Input: n = 16Output: YesExplanation: sum of divisors = 1 + 2 + 4 + 8 + 16 = 31 = 2n - 1 Input: n = 9Outpu
    4 min read
    Find the number of primitive roots modulo prime
    Given a prime p . The task is to count all the primitive roots of p .A primitive root is an integer x (1 <= x < p) such that none of the integers x - 1, x2 - 1, ...., xp - 2 - 1 are divisible by p but xp - 1 - 1 is divisible by p . Examples: Input: P = 3 Output: 1 The only primitive root modul
    5 min read
    Pernicious number
    A pernicious number is a positive integer which has prime number of ones in its binary representation. The first pernicious number is 3 since 3 = (11)(in binary representation) and 1 + 1 = 2, which is a prime.Properties of Pernicious Numbers :1. There isn't any pernicious number which is also power
    5 min read
    Trimorphic Number
    Given a number N, the task is to check whether the number is Trimorphic number or not. A number is called Trimorphic number if and only if its cube ends in the same digits as the number itself. In other words, number appears at the end of its cube. Examples: Input : 5 Output : trimorphic Explanation
    10 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences