-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.cc
62 lines (57 loc) · 1.09 KB
/
23.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
int divisorSum (int i);
bool isAbundant (int in);
vector <int> abundant;
bool allNums[28124];
int main(int argc, char const *argv[])
{
if(argc == 2){
int input = stoi(argv[1]);
//Calc Abundant numbers
for (int i = 1; i < input;i++){
isAbundant(i);
}
int sum = 0;
//find all numbers that can be formed by 2 abundant
for (int i = 0; i < abundant.size();i++){
for (int j = i; j < abundant.size(); j++){
if(abundant[i]+ abundant[j] < input+1){
allNums[abundant[i]+abundant[j]] = true;
}else{
break;
}
}
}
for (int i = 1; i < input+1;i++){
if(!allNums[i])
sum += i;
}
cout << sum << "\n";
}
return 0;
}
bool isAbundant(int in){
if(divisorSum(in) > in){
abundant.push_back(in);
return true;
}
//Not abundant number
return false;
}
//Find sum of all divisors less than i
int divisorSum (int i){
//Find sum of divisors
int sum = 1;
int sq = sqrt(i);
if(i == sq*sq){
sum -= sq;
}
for(int d = 2; d <= sq;d++){
if(i % d == 0)
sum += d + i/d;
}
return sum;
}