-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp37.cpp
More file actions
78 lines (71 loc) · 1.83 KB
/
Copy pathp37.cpp
File metadata and controls
78 lines (71 loc) · 1.83 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
70
71
72
73
74
75
76
77
78
/**
* The number 3797 has an interesting property. Being prime itself, it is
* possible to continuously remove digits from left to right, and remain
* prime at each stage: 3797, 797, 97, and 7. Similarly we can work from
* right to left: 3797, 379, 37, and 3.
*
* Find the sum of the only eleven primes that are both truncatable from
* left to right and right to left.
*
* NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
*
* ANSWER: 748317.
*/
#include <iostream>
#include <numeric>
#include <vector>
#include "euler/prime_test.hpp"
#include "euler.h"
BEGIN_PROBLEM(37, solve_problem_37)
PROBLEM_TITLE("Truncatable primes")
PROBLEM_ANSWER("748317")
PROBLEM_DIFFICULTY(1)
PROBLEM_FUN_LEVEL(2)
PROBLEM_TIME_COMPLEXITY("?")
PROBLEM_SPACE_COMPLEXITY("?")
END_PROBLEM()
static std::vector<int> truncatable_primes;
// Check whether a prime number n is still a prime when removing
// one digit from the left iteratively.
static bool is_truncatable_from_left(int n)
{
int m = 1000000000;
for (int prev = n; (n %= m) > 0; m /= 10, prev = n)
{
if (n != prev)
{
if (!euler::is_prime(n))
{
return false;
}
}
}
return true;
}
// Try append digits to a prime number n to make it still a prime
// when truncated iteratively both ways.
static void try_append_digit(int n)
{
static const int digits[] = { 1, 3, 7, 9 };
for (int d: digits)
{
int k = n * 10 + d;
if (euler::is_prime(k))
{
if (is_truncatable_from_left(k))
{
truncatable_primes.push_back(k);
}
try_append_digit(k);
}
}
}
static void solve_problem_37()
{
try_append_digit(2);
try_append_digit(3);
try_append_digit(5);
try_append_digit(7);
int sum = std::accumulate(truncatable_primes.cbegin(), truncatable_primes.cend(), 0);
std::cout << sum << std::endl;
}