-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject Euler #21.cpp
49 lines (45 loc) · 949 Bytes
/
Project Euler #21.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
#include <iostream>
#include <set>
unsigned int getSum(unsigned int x)
{
unsigned int divisorSum = 1;
for (unsigned int divisor = 2; divisor * divisor <= x; divisor++)
if (x % divisor == 0)
{
divisorSum += divisor;
auto otherDivisor = x / divisor;
if (otherDivisor != divisor)
divisorSum += otherDivisor;
}
return divisorSum;
}
int main()
{
std::set<unsigned int> amicables;
const unsigned int MaxAmicable = 100000;
for (unsigned int i = 2; i <= MaxAmicable; i++)
{
auto sibling = getSum(i);
if (i == getSum(sibling) && i != sibling)
{
amicables.insert(i);
amicables.insert(sibling);
}
}
unsigned int tests;
std::cin >> tests;
while (tests--)
{
unsigned int x;
std::cin >> x;
unsigned int sum = 0;
for (auto i : amicables)
{
if (i > x)
break;
sum += i;
}
std::cout << sum << std::endl;
}
return 0;
}