Probelm 23 : Non-abundant sums
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
C++ source code
#include <iostream>
#include <vector>
#include <cstring>
#include <ctime>
using namespace std;
class PE0023
{
private:
static const int m_LIMIT = 28123;
vector<int> m_abundantNumbers;
void findAllAbundantNumbers();
bool checkAbundantNumber(int number);
int getSumOfWrittenAsTwoAbundantNumbers();
public:
PE0023() { findAllAbundantNumbers();}
int getSumOfCannotBeWrittenAsTwoAbundantNumbers();
};
void PE0023::findAllAbundantNumbers()
{
// As 12 is the smallest abundant number , and all integers
// greater than 28123 can be written as the sum of two abundant numbers
for(int n=12; n<=m_LIMIT; n++)
{
if (true == checkAbundantNumber(n))
{
m_abundantNumbers.push_back(n);
}
}
}
bool PE0023::checkAbundantNumber(int number)
{
int sumOfProperDivisors = 0;
for(int i=1; i<number; i++)
{
if (number%i == 0)
{
sumOfProperDivisors += i;
}
}
if (sumOfProperDivisors > number)
{
return true; // abundant
}
return false; // non-abundant
}
int PE0023::getSumOfWrittenAsTwoAbundantNumbers()
{
int sumAbundantNumbersArray[m_LIMIT+1];
memset(sumAbundantNumbersArray, 0, sizeof(sumAbundantNumbersArray));
// m_abundantNumbers[j] >= m_abundantNumbers[i]
for (unsigned int i=0; 2*m_abundantNumbers[i]<=m_LIMIT; i++)
{
for(unsigned int j=i; j<m_abundantNumbers.size(); j++)
{
int sum = m_abundantNumbers[i] + m_abundantNumbers[j];
if (sum <= m_LIMIT)
{
sumAbundantNumbersArray[sum]++;
}
}
}
int sum = 0;
for(int i=12; i<=m_LIMIT; i++)
{
if (sumAbundantNumbersArray[i] > 0)
{
sum += i;
}
}
return sum;
}
int PE0023::getSumOfCannotBeWrittenAsTwoAbundantNumbers()
{
int sum = getSumOfWrittenAsTwoAbundantNumbers();
// total = 1 + 2 + 3 +...+ m_LIMIT
int total = (1 + m_LIMIT) * m_LIMIT / 2;
return total - sum;
}
int main()
{
clock_t start = clock();
PE0023 pe0023;
int sum = pe0023.getSumOfCannotBeWrittenAsTwoAbundantNumbers();
cout << "The sum of all the positive integers which cannot be written ";
cout << "as the sum of two abundant numbers is " << sum << endl;
clock_t finish = clock();
double duration = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "C/C++ running time: " << duration << " seconds" << endl;
return 0;
}
Python source code
import numpy as np
import math
gLIMIT = 28123
def d(n):
sum = 1
root = math.sqrt(n)
for i in range(2, int(root)+1):
if n % i == 0:
sum += i + n/i
if root == int(root):
sum -= root #correct sum if n is a perfect square
return sum
def checkAbundantNumber(n):
if d(n) > n:
return True # abundant
else:
return False # non-abundant
def findAllAbundantNumbers():
"""
As 12 is the smallest abundant number , and all integers
greater than gLIMIT can be written as the sum of two abundant numbers
"""
abundantNumbers = [ n for n in range(12, gLIMIT+1) if d(n) > n ]
return abundantNumbers
def getSumOfWrittenAsTwoAbundantNumbers(abundantNumbers):
sumAbundantNumbersArray = np.zeros(gLIMIT+1)
# abundantNumbers[j] >= abundantNumbers[i]
i = 0
while abundantNumbers[i] <= gLIMIT/2:
for j in range(i, len(abundantNumbers)):
sum0 = abundantNumbers[i] + abundantNumbers[j]
if sum0 <= gLIMIT:
sumAbundantNumbersArray[sum0] += 1
i += 1
sumOfNumbers = sum([i for i in range(12, gLIMIT+1) if sumAbundantNumbersArray[i]>0])
return sumOfNumbers
def getSumOfCannotBeWrittenAsTwoAbundantNumbers(abundantNumbers):
"""
take indirect method to compte sum of cannot be written as two abundantNumbers
"""
sum1 = getSumOfWrittenAsTwoAbundantNumbers(abundantNumbers)
# total = 1 + 2 + 3 +...+ gLIMIT
total = (1 + gLIMIT) * gLIMIT / 2.0
return int(total) - sum1
def getSumOfCannotBeWrittenAsTwoAbundantNumbers2(abundantNumbers):
"""
take direct method to compute sum of cannot be written as two abundantNumbers
"""
L, sum2 = gLIMIT, 0
abn = set()
for n in range(1, L):
if d(n) > n:
abn.add(n)
if not any( (n-a in abn) for a in abn ):
sum2 += n
return sum2
def main():
abundantNumbers = findAllAbundantNumbers()
sum2 = getSumOfCannotBeWrittenAsTwoAbundantNumbers(abundantNumbers)
print("The sum of all the positive integers which cannot be")
print("written as the sum of two abundant numbers is",sum2)
sum2p = getSumOfCannotBeWrittenAsTwoAbundantNumbers2(abundantNumbers)
print("The sum of all the positive integers which cannot be")
print("written as the sum of two abundant numbers is",sum2p)
if __name__ == '__main__':
main()