## Project euler: Problem 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.

#include <iostream> #include <algorithm> #include <stdio.h> #include <vector> using namespace std; #define MAX 28123 vector<long> abundant; int allabundant(int num) { long sum = 0; long p = 1; for (long i = 2; i <= num; i++) { sum = 1; p = 2; while (p * p <= i) { if (i % p == 0) { sum += p; if (p != i / p) sum += (i / p); } p++; } if (sum > i) abundant.push_back(i); } } int main() { long long sum = 0; allabundant(MAX); bool boolabund[MAX] = {false}; cout<< "Total number of abundant numbers below "<<MAX<<": "<<abundant.size()<<endl; for(long i = 0; i < abundant.size(); i++) { for(long j = i; j < abundant.size(); j++) { long long temp = abundant[i] + abundant[j]; if (temp > MAX) break; boolabund[temp] = true; } } for (int i = 0; i <= MAX; i++) if (boolabund[i] == false) sum += i; cout<<"Answer: "<<sum<<endl; }

time ./a.exe

Total number of abundant numbers below 28123: 6965

Answer: 4207994

real 0m0.193s

user 0m0.124s

sys 0m0.046s