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:
- 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
- If the number is not abundant then return false else do the following
- 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.
- If both above conditions are true print “Yes” else print “No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getSum( int n)
{
int sum = 0;
for ( int i = 1; i <= sqrt (n); i++) {
if (n % i == 0) {
if (n / i == i)
sum = sum + i;
else
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
return sum;
}
bool checkAbundant( int n)
{
return (getSum(n) - n > n);
}
bool isDeficient( int n)
{
return (getSum(n) < (2 * n));
}
bool checkPrimitiveAbundant( int num)
{
if (!checkAbundant(num)) {
return false ;
}
for ( int i = 2; i <= sqrt (num); i++) {
if (num % i == 0 && i != num) {
if (i * i == num) {
if (!isDeficient(i)) {
return false ;
}
}
else if (!isDeficient(i) || !isDeficient(num / i)) {
return false ;
}
}
}
return true ;
}
int main()
{
int n = 20;
if (checkPrimitiveAbundant(n)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
class GFG{
static int getSum( int n)
{
int sum = 0 ;
for ( int i = 1 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (n / i == i)
sum = sum + i;
else
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
return sum;
}
static boolean checkAbundant( int n)
{
return (getSum(n) - n > n);
}
static boolean isDeficient( int n)
{
return (getSum(n) < ( 2 * n));
}
static boolean checkPrimitiveAbundant( int num)
{
if (!checkAbundant(num))
{
return false ;
}
for ( int i = 2 ; i <= Math.sqrt(num); i++)
{
if (num % i == 0 && i != num)
{
if (i * i == num)
{
if (!isDeficient(i))
{
return false ;
}
}
else if (!isDeficient(i) ||
!isDeficient(num / i))
{
return false ;
}
}
}
return true ;
}
public static void main(String[] args)
{
int n = 20 ;
if (checkPrimitiveAbundant(n))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
import math
def getSum(n):
sum = 0
for i in range ( 1 , int (math.sqrt(n) + 1 )):
if (n % i = = 0 ):
if (n / / i = = i):
sum = sum + i
else :
sum = sum + i
sum = sum + (n / / i)
return sum
def checkAbundant(n):
if (getSum(n) - n > n):
return True
return False
def isDeficient(n):
if (getSum(n) < ( 2 * n)):
return True
return False
def checkPrimitiveAbundant(num):
if not checkAbundant(num):
return False
for i in range ( 2 , int (math.sqrt(num) + 1 )):
if (num % i = = 0 and i ! = num):
if (i * i = = num):
if ( not isDeficient(i)):
return False
elif ( not isDeficient(i) or
not isDeficient(num / / i)):
return False
return True
n = 20
if (checkPrimitiveAbundant(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int getSum( int n)
{
int sum = 0;
for ( int i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (n / i == i)
sum = sum + i;
else
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
return sum;
}
static bool checkAbundant( int n)
{
return (getSum(n) - n > n);
}
static bool isDeficient( int n)
{
return (getSum(n) < (2 * n));
}
static bool checkPrimitiveAbundant( int num)
{
if (!checkAbundant(num))
{
return false ;
}
for ( int i = 2; i <= Math.Sqrt(num); i++)
{
if (num % i == 0 && i != num)
{
if (i * i == num)
{
if (!isDeficient(i))
{
return false ;
}
}
else if (!isDeficient(i) ||
!isDeficient(num / i))
{
return false ;
}
}
}
return true ;
}
public static void Main()
{
int n = 20;
if (checkPrimitiveAbundant(n))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function getSum( n) {
let sum = 0;
for ( let i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
sum = sum + i;
else {
sum = sum + i;
sum = sum + (n / i);
}
}
}
return sum;
}
function checkAbundant( n) {
return (getSum(n) - n > n);
}
function isDeficient( n) {
return (getSum(n) < (2 * n));
}
function checkPrimitiveAbundant( num) {
if (!checkAbundant(num)) {
return false ;
}
for ( let i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0 && i != num) {
if (i * i == num) {
if (!isDeficient(i)) {
return false ;
}
} else if (!isDeficient(i) || !isDeficient(num / i)) {
return false ;
}
}
}
return true ;
}
let n = 20;
if (checkPrimitiveAbundant(n)) {
document.write( "Yes" );
} else {
document.write( "No" );
}
</script>
|
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];
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;
}
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];
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;
}
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
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
def checkPrimitiveAbundant(n):
if dp[n] ! = - 1 :
return dp[n]
dp[n] = sum_of_divisors(n) > n
return dp[n]
if __name__ = = "__main__" :
n = 20
if checkPrimitiveAbundant(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG
{
const int N = 100000;
static int [] dp = new int [N];
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;
}
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" );
}
}
}
|
Javascript
const N = 1e5 + 5;
let dp = new Array(N);
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 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