- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem021.cpp
More file actions
Latest commit
34 lines (31 loc) · 680 Bytes
/
Copy pathproblem021.cpp
File metadata and controls
34 lines (31 loc) · 680 Bytes
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
#include<iostream>
#include<vector>
std::vector<int> aliquotSums(int upper)
{
std::vector<int> s(upper + 1, 1);
s[1] = 0;
int end = upper / 2;
for (int i = 2; i <= end; ++i) {
for (int j = 2 * i; j <= upper; j += i) {
s[j] += i;
}
}
return s;
}
intsumOfAmicableNumbers(int upper)
{
std::vector<int> s = aliquotSums(upper);
int amis = 0;
for (int i = 2; i <= upper; ++i) {
int sum = s[i];
if (sum > i && sum <= upper && s[sum] == i) {
amis += sum + i;
}
}
return amis;
}
intmain()
{
std::cout << "Answer: " << sumOfAmicableNumbers(9999) << '\n';
return0;
}