-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchallenge.cpp
More file actions
50 lines (40 loc) · 1.31 KB
/
challenge.cpp
File metadata and controls
50 lines (40 loc) · 1.31 KB
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
//
// challenge.cpp
// Challenges
//
// Created by Carlos Álvaro on 11/06/2020.
// Copyright © 2020 cdalvaro. All rights reserved.
//
#include <numeric>
#include <vector>
#include "challenges/c0005/challenge.hpp"
#include "tools/math/factorial.hpp"
using namespace challenges;
Challenge5::Challenge5(const Type_t &last_number) : last_number(last_number) {
}
IChallenge::Solution_t Challenge5::solve() {
std::vector<Type_t> divisors(last_number - 1, 0);
std::iota(divisors.begin(), divisors.end(), 2);
auto smallest_number = tools::math::factorial(last_number);
bool new_number_found;
do {
new_number_found = false;
for (const auto &divisor : divisors) {
if (smallest_number % divisor == 0) {
Type_t new_smallest_number = smallest_number / divisor;
bool is_multiple = true;
for (const auto &divisor : divisors) {
if (new_smallest_number % divisor != 0) {
is_multiple = false;
break;
}
}
if (is_multiple) {
smallest_number = new_smallest_number;
new_number_found = true;
}
}
}
} while (new_number_found);
return smallest_number;
}