-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
challenge.cpp
35 lines (28 loc) · 885 Bytes
/
challenge.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
//
// challenge.cpp
// Challenges
//
// Created by Carlos Álvaro on 03/04/2021.
// Copyright © 2021 cdalvaro. All rights reserved.
//
#include <numeric>
#include "challenges/c0021/challenge.hpp"
#include "tools/math/factorization.hpp"
using namespace challenges;
using namespace tools;
Challenge21::Challenge21(const size_t &limit) : limit(limit) {
}
IChallenge::Solution_t Challenge21::solve() {
std::set<Type_t> amicable_numbers;
for (Type_t number = 1; number <= limit; ++number) {
if (amicable_numbers.find(number) != amicable_numbers.end()) {
continue;
}
if (auto pair = math::amicablePair(number)) {
amicable_numbers.insert(pair->first);
amicable_numbers.insert(pair->second);
}
}
Type_t sum = std::accumulate(amicable_numbers.begin(), amicable_numbers.end(), 0);
return sum;
}