-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem 23.cpp
62 lines (52 loc) · 1.19 KB
/
Problem 23.cpp
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
#include<iostream>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<fstream>
using namespace std;
vector<long> abundants;
bool sumOfabund(long n);
unsigned long long sumDivisors(long n);
int main(){
ofstream output ("Problem23Debug.txt");
for(long i = 2; i <= 28123; i++){
if(sumDivisors(i) > i){
abundants.push_back(i);
//output << endl << i << " " << sumDivisors(i);
}
}
unsigned long long sumOfNonAbundants = 0;
for(long j = 1; j< 29000; j++){
if(!sumOfabund(j)){
sumOfNonAbundants += j;
}
if(j % 1000 == 0){
cout <<endl << j;
}
}
cout << endl << sumOfNonAbundants;
return 0;
}
unsigned long long sumDivisors(long n){
unsigned long long returnVal = 1;
for (long i = 2; i < sqrt(n); i++){
if (n % i == 0){
returnVal += i;
returnVal += (n / i);
}
}
if(sqrt(n) == ceil(sqrt(n))){
returnVal += sqrt(n);
}
return returnVal;
}
bool sumOfabund(long n){
for(long i =0; i< abundants.size() && abundants[i] < n; i++){
for(long j =0; j< abundants.size() && abundants[j] < n; j++){
if (abundants[i] + abundants[j] == n){
return true;
}
}
}
return false;
}