Deficient Number - GeeksforGeeks
A number n is said to be Deficient Number if sum of all the divisors of the number denoted by divisorsSum(n) is less than twice the value of the number n. And the difference between these two values is called the deficiency.
Mathematically, if below condition holds the number is said to be Deficient:
1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19 …..
Given a number n, our task is to find if this number is Deficient number or not.
Read full article from Deficient Number - GeeksforGeeks
A number n is said to be Deficient Number if sum of all the divisors of the number denoted by divisorsSum(n) is less than twice the value of the number n. And the difference between these two values is called the deficiency.
Mathematically, if below condition holds the number is said to be Deficient:
divisorsSum(n) < 2*n deficiency = (2*n) - divisorsSum(n)The first few Deficient Numbers are:
1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19 …..
Given a number n, our task is to find if this number is Deficient number or not.
A Simple solution is to iterate all the numbers from 1 to n and check if the number divides n and calculate the sum. Check if this sum is less than 2 * n or not.
Time Complexity of this approach: O ( n )
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)
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 case of (10, 10). In such case we will take only one of them in calculation of sum.
While checking divisors we will have to be careful if there are two equal divisors as in case of (10, 10). In such case we will take only one of them in calculation of sum.
Time Complexity: O( sqrt( n ))
// Function to calculate sum of divisors
int
divisorsSum(
int
n)
{
int
sum = 0;
// Initialize sum of prime factors
// Note that this loop runs till square root of n
for
(
int
i=1; i<=
sqrt
(n)+1; 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 Deficient Number
bool
isDeficient(
int
n)
{
// Check if sum(n) < 2 * n
return
(divisorsSum(n) < (2 * n));
}