Link: http://projecteuler.net/problem=23
An ideal number is a number for which the sum of its own divisors is exactly equal to the number. For example, the sum of the corresponding divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is an ideal number.
A number n is called insufficient if the sum of its own divisors is less than n, and this is called plentiful if this sum exceeds n.
Since 12 is the smallest bountiful number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two bountiful numbers 24. By mathematical analysis it can be shown that all integers greater than 28123 can be written as the sum of two bountiful numbers. However, this upper limit cannot yet be reduced by analysis, although it is known that the largest number, which cannot be expressed as the sum of two bountiful numbers, is less than this limit.
Find the sum of all positive integers that cannot be written as the sum of two bold numbers.
Answer to the problem: 4179871, my program shows 3797954.
First of all, I make a function to fill the rich [] array with all the plentiful numbers below 28124. This works fine, because I googled the plentiful numbers, and they exactly match my array.
-, 1-28123, , " ". hold [].
, , , [] [] hold [] 0. ( [ [ 0 n] + [0 n]] = 0)
hold [], 3797954
, , , . ?
#include <iostream>
#include <cmath>
using namespace std;
int hold[28124];
int abundant[7000];
bool abundance(int x){
int counter = 1;
for (int i = 2; i < sqrt(x); i++){
if (x % i == 0){
counter += i;
counter += x / i;
}
}
int y = sqrt(x);
if (x % y == 0){
counter += sqrt(x);
}
if (counter > x){
return true;
} else {
return false;
}
}
int main()
{
int counter = 0;
for (int i = 0; i < 28124; i++){
hold[i] = i;
}
for (int j = 10; j < 28124; j++){
if (abundance(j) == true){
abundant[counter] = j;
counter++;
}
}
for (int m = 0; m < counter; m++){
for (int n = 0; n < counter; n++){
if (abundant[m]+abundant[n] < 28124){
hold[abundant[m]+abundant[n]] = 0;
}
}
}
int counter2 = 0;
for (int x = 0; x < 28124; x++){
counter2 += hold[x];
}
cout << counter2 << endl;
}