-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler23.cpp
83 lines (72 loc) · 1.43 KB
/
Euler23.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <math.h>
#include <unistd.h>
#include <fstream>
#include <unordered_set>
using namespace std;
vector<int> factors(int n){
vector<int> ret;
for(unsigned int i = 1; i < n/2+1; i++){
if(n%i == 0){
ret.push_back(i);
}
}
return ret;
}
bool two_numbers_add_up_to_target(int target, const vector<int>& nums, const vector<bool>& truth){
int temp = target;
int i = 0;
while(nums[i] < target){
temp = target - nums[i];
if(truth[temp]){
return true;
}
i+=1;
}
return false;
}
int sum(vector<int> s){
int ret = 0;
for(unsigned int i = 0; i < s.size(); i++){
ret += s[i];
}
return ret;
}
void print1D(vector<int> v){
for(unsigned int j = 0; j < v.size(); j++){
if(j != v.size() -1 ){
cout<<v[j]<<", ";
}
else{
cout<<v[j]<<endl;
}
}
}
bool abundant(int i){
vector<int> f = factors(i);
int s = sum(f);
if(s > i){
return true;
}
return false;
}
int main(){
vector<bool> abundant_truth(28123, false);
vector<int> abundant_numbers;
for(unsigned int i = 12; i <= 28123; i++){
if(abundant(i)){
abundant_truth[i] = true;
abundant_numbers.push_back(i);
}
}
cout<<"Done loading "<<abundant_numbers.size()<<" abundant numbers"<<endl;
int total_sum = 0;
for(unsigned int i = 0; i <= 28123; i++){
if(!two_numbers_add_up_to_target(i, abundant_numbers, abundant_truth)){
total_sum += i;
}
}
cout<<total_sum<<endl;
}