-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler_4.cpp
More file actions
58 lines (47 loc) · 1.34 KB
/
euler_4.cpp
File metadata and controls
58 lines (47 loc) · 1.34 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
51
52
53
54
55
56
57
58
#include "euler_4.h"
#include <iostream>
#include <cassert>
std::vector<int> extract_digits(unsigned val) {
std::vector<int> digits;
int pow_10 = 10;
int prev_pow_10 = 1;
do {
int place_val = (val % pow_10) / prev_pow_10;
digits.push_back(place_val);
val -= place_val * prev_pow_10;
prev_pow_10 = pow_10;
pow_10 *= 10;
} while (val);
return digits;
}
bool is_palindrome(const std::vector<int> &v) {
assert(!v.empty());
unsigned i = 0;
while (i < (v.size() / 2)) {
if (v.at(i) != v.at(v.size() - i - 1)) {
return false;
}
i++;
}
return true;
}
unsigned find_largest_palindrome_product(int digits) {
assert(digits > 0);
// find the largest number we need to try
unsigned upper_bound = 1;
for (int i = 0; i < digits; i++) {
upper_bound *= 10;
}
unsigned largest_palindrome = 0;
for (unsigned i = 0; i < upper_bound; i++) {
// since multiplication is commutative,
// we only need to try half of the combinations
for (unsigned j = 0; j < i; j++) {
unsigned product = i * j;
if (product > largest_palindrome && is_palindrome(extract_digits(product))) {
largest_palindrome = product;
}
}
}
return largest_palindrome;
}