-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy patheuler-0041.cpp
126 lines (112 loc) · 3.36 KB
/
euler-0041.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// ////////////////////////////////////////////////////////
// # Title
// Pandigital prime
//
// # URL
// https://projecteuler.net/problem=41
// http://euler.stephan-brumme.com/41/
//
// # Problem
// We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once.
// For example, 2143 is a 4-digit pandigital and is also prime.
//
// What is the largest n-digit pandigital prime that exists?
//
// # Solved by
// Stephan Brumme
// February 2017
//
// # Algorithm
// The largest pandigital number is 987654321. In order to find out whether a number `x <= 987654321` is prime, I precompute all primes up to `sqrt{987654321} approx 31426`.
// These "small" prime numbers will be kept in ''smallPrimes''.
//
// My second step is to generate all pandigital numbers: I create all permutations of the string ''"123456789"'' and perform a simple primality test (using ''smallPrimes'').
// Due to Hackerrank's variable number of digits, not only the 9-pandigitals numbers but also the 8-, 7-, 6-, ..., 2-pandigital numbers are checked, too.
//
// The set ''panPrimes'' will contain all 2-, ..., 9-pandigital primes after those two precomputation steps.
// Each test case look ups the closest bigger pandigital prime (''upper_bound'') and goes one step backwards.
//
// # Note
// When looking at the results I only saw 4- and 7-pandigital primes.
// Modifying my loop in step 2 accordingly would provide a 10x speed-up.
#include <set>
#include <iostream>
#include <algorithm>
int main()
{
// precomputation step 1:
// find all primes below sqrt(987654321)
std::set<unsigned int> smallPrimes;
smallPrimes.insert(2);
for (unsigned int i = 3; i*i <= 987654321; i += 2)
{
bool isPrime = true;
for (auto p : smallPrimes)
{
// abort, no divisors possible
if (p*p > i)
break;
// divisor found ?
if (i % p == 0)
{
isPrime = false;
break;
}
}
// found a prime number
if (isPrime)
smallPrimes.insert(i);
}
// precomputation step 2:
// generate all permutations of the strings "12", "123", "1234", ..., "123456789"
// and test whether they are prime
std::set<unsigned int> panPrimes;
for (unsigned int digits = 2; digits <= 9; digits++)
{
std::string strNumber = "123456789";
// reduce number of digits
strNumber.erase(digits);
do
{
unsigned int number = std::stoi(strNumber);
// test whether pandigital number is prime
bool isPrime = true;
for (auto p : smallPrimes)
{
// abort, no divisors possible
if (p*p > number)
break;
// divisor found ?
if (number % p == 0)
{
isPrime = false;
break;
}
}
// found a pandigital prime ?
if (isPrime)
panPrimes.insert(number);
} while (std::next_permutation(strNumber.begin(), strNumber.end()));
}
// process input
unsigned int tests;
std::cin >> tests;
while (tests--)
{
unsigned int limit;
std::cin >> limit;
// find next larger pandigital prime number
auto i = panPrimes.upper_bound(limit);
// smaller than the smallest pandigital prime ?
if (i == panPrimes.begin())
{
std::cout << "-1" << std::endl;
continue;
}
// upper_bound() goes one step too far
i--;
// and print it
std::cout << *i << std::endl;
}
return 0;
}