-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp36.cpp
More file actions
69 lines (65 loc) · 1.53 KB
/
Copy pathp36.cpp
File metadata and controls
69 lines (65 loc) · 1.53 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
59
60
61
62
63
64
65
66
67
68
69
#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream>
#include "euler/digits.hpp"
#include "euler.h"
BEGIN_PROBLEM(36, solve_problem_36)
PROBLEM_TITLE("Find palindromic numbers in both base 10 and base 2")
PROBLEM_ANSWER("872187")
PROBLEM_DIFFICULTY(1)
PROBLEM_FUN_LEVEL(1)
PROBLEM_TIME_COMPLEXITY("sqrt(N) log(N)")
PROBLEM_SPACE_COMPLEXITY("log(N)")
END_PROBLEM()
static void solve_problem_36()
{
int64_t sum = 0;
#if 0
for (int n = 1; n < 1000000; n++)
{
if (is_palindromic(n,10) && is_palindromic(n,2))
{
if (verbose)
std::cout << n << std::endl;
sum += n;
}
}
#else
// Note: the algorithm can be improved by skipping all even numbers.
// However, this is not implemented below.
std::array<int,10> digits;
for (int a = 1; a < 1000; a++)
{
auto p0 = digits.begin();
auto p1 = std::copy(euler::digits(a).begin(), euler::digits(a).end(), p0);
// Mirror abc => abcba
{
auto p2 = std::reverse_copy(p0, p1 - 1, p1);
int n = euler::from_digits<int>(p0, p2);
if (euler::is_palindromic<2>(n))
{
if (verbose())
{
std::cout << n << std::endl;
}
sum += n;
}
}
// Mirror abc => abccba
{
auto p2 = std::reverse_copy(p0, p1, p1);
int n = euler::from_digits<int>(p0, p2);
if (euler::is_palindromic<2>(n))
{
if (verbose())
{
std::cout << n << std::endl;
}
sum += n;
}
}
}
#endif
std::cout << sum << std::endl;
}