<< problem 565 - Divisibility of sum of divisors | Counting hexagons - problem 577 >> |
Problem 571: Super Pandigital Numbers
(see projecteuler.net/problem=571)
A positive number is pandigital in base b if it contains all digits from 0 to b - 1 at least once when written in base b.
A n-super-pandigital number is a number that is simultaneously pandigital in all bases from 2 to n inclusively.
For example 978 = 1111010010_2 = 1100020_3 = 33102_4 = 12403_5 is the smallest 5-super-pandigital number.
Similarly, 1093265784 is the smallest 10-super-pandigital number.
The sum of the 10 smallest 10-super-pandigital numbers is 20319792309.
What is the sum of the 10 smallest 12-super-pandigital numbers?
My Algorithm
I made an assumption which turned out to be true:
there are at least ten 12-super-pandigital numbers with 12 digits.
Or in plain English: if I generate all permutations of the digits { 0,1,2,3,4,5,6,7,8,9,10,11 }
in lexicographical order
and check whether they are 12-super-pandigital then I will find the result.
The smallest valid 12-pandigital number is { 1,0,2,3,4,5,6,7,8,9,10,11 }
because leading zeros are forbidden.
Any 12-pandigital number fits nicely in a 64 bit integer: log_2 12^12 < 44 → I need at most 44 bits
The smallest such number is current = 1 * 12^11 + 0 * 12^10 + 2 * 12^9 + 3 * 12^8 + ... + 11 * 12^0 = 754777787027 (in decimal notation, base 10)
I rewrote the function isPandigital()
from problem 170 such that it accepts an arbitrary base greater than 1 and tolerates if a digit appears more than once.
It basically chops off the right-most digit and sets a bit in a bitmask used
.
If all 12 bits are set at the end, then the number is pandigital indeed.
Since all numbers are already pandigital in base 12 I only have to check bases 2,3,...,11.
It's much faster to check each number against a "high" base first because the "failure rate" is higher.
For example, all numbers except zero and 2^n-1 are pandigital in base 2, so pretty much all numbers are pandigital in base 2.
On the other side, a substantial amount of numbers fail the 11-pandigital test.
Consequently, my for
-loop runs backwards from 11 to 2 and aborts if a isPandigital
check fails.
Division and modulo operations are notoriously slow on modern CPUs - and my program basically does that non-stop.
Division and modulo operation can be quite fast if you divide by a power of two (and you have a smart compiler):
Division by 8 is just a right shift by 3 bits (because 2^3 = 8) and modulo 8 means taking the right-most three bits (number & (8-1)
).
Checking isPandigital(current, 8)
before anything else made my program more than twice as fast !
Note
The inner loop of isPandigital
skips the last iteration because then number
is a single digit and I can save one division and one modulo. This trick is about 5% faster.
I was pretty sure that the for
-loop which calls isPandigital(current, base)
can be optimized:
if isPandigital(current, 8)
was already tested outside the for
-loop then there is no use in tested against it inside the for
-loop.
To my surprise, to opposite is true and I have no idea why.
If a number passes the pandigital check for base=11,10,9,...,6 then it also passes the checks for base=5,4,3,2.
Interactive test
You can submit your own input to my program and it will be instantly processed at my server:
This is equivalent toecho "10 10" | ./571
Output:
Note: the original problem's input 12 10
cannot be entered
because just copying results is a soft skill reserved for idiots.
(this interactive test is still under development, computations will be aborted after one second)
My code
… was written in C++11 and can be compiled with G++, Clang++, Visual C++. You can download it, too.
#include <iostream>
#include <vector>
#include <algorithm>
const unsigned int MaxBase = 12;
// return true if decimal number is pandigital in a certain base
// note: a digit may found more than once unlike most other pandigital problems
// where each digit must be found exactly once
bool isPandigital(unsigned long long number, unsigned int base)
{
// bitmask where the n-th bit is set if the digit n was observed in number
unsigned int used = 0;
// all bits set => all digits used
const unsigned int All = (1 << base) - 1;
// process right-most digit and remove it
while (number >= base) // skip last iteration
{
auto digit = number % base;
used |= 1 << digit;
number /= base;
}
// simplified last iteration
used |= 1 << number;
return used == All;
}
int main()
{
unsigned int base = 12;
unsigned int numResults = 10;
std::cin >> base >> numResults;
// smallest 12-pandigital number
std::vector<unsigned char> twelve = { 1,0,2,3,4,5,6,7,8,9,10,11 };
// reduce for smaller bases
twelve.resize(base);
// look for the sum of the first 10 matches
unsigned int numFound = 0;
unsigned long long sum = 0;
do
{
// convert from base 12 to an integer (technically that base-12-to-2 ?)
unsigned long long current = 0;
for (auto digit : twelve)
{
current *= base;
current += digit;
}
// an optimizing compiler can reduce mod/div by fast bit operations
if (base >= 8 && !isPandigital(current, 8))
continue;
// no need to check base 12 because all generated number are pandigital in base 12 by definition
// I'm pretty sure there are some relationships:
// - if pandigital in base 8 then always in base 4, too
// - if pandigital in base 9 then always in base 3, too
// my debugging output verified that no number is rejected by the tests in base 2,3,4,5
bool isGood = true;
for (auto i = base - 1; i >= 2; i--)
if (!isPandigital(current, i)) // I tried to prepend "i != 8" but it was slower !
{
isGood = false;
break;
}
// passed all tests
if (isGood)
{
sum += current;
numFound++;
// done ?
if (numFound == numResults)
break;
}
} while (std::next_permutation(twelve.begin(), twelve.end()));
// print result
std::cout << sum << std::endl;
return 0;
}
This solution contains 13 empty lines, 20 comments and 3 preprocessor commands.
Benchmark
The correct solution to the original Project Euler problem was found in 8.9 seconds on an Intel® Core™ i7-2600K CPU @ 3.40GHz.
(compiled for x86_64 / Linux, GCC flags: -O3 -march=native -fno-exceptions -fno-rtti -std=gnu++11 -DORIGINAL
)
See here for a comparison of all solutions.
Note: interactive tests run on a weaker (=slower) computer. Some interactive tests are compiled without -DORIGINAL
.
Changelog
September 22, 2017 submitted solution
September 22, 2017 added comments
Difficulty
Project Euler ranks this problem at 25% (out of 100%).
Links
projecteuler.net/thread=571 - the best forum on the subject (note: you have to submit the correct solution first)
Code in various languages:
Python github.com/Meng-Gen/ProjectEuler/blob/master/571.py (written by Meng-Gen Tsai)
C++ github.com/evilmucedin/project-euler/blob/master/euler571/571.cpp (written by Den Raskovalov)
C++ github.com/roosephu/project-euler/blob/master/571.cpp (written by Yuping Luo)
Those links are just an unordered selection of source code I found with a semi-automatic search script on Google/Bing/GitHub/whatever.
You will probably stumble upon better solutions when searching on your own.
Maybe not all linked resources produce the correct result and/or exceed time/memory limits.
Heatmap
Please click on a problem's number to open my solution to that problem:
green | solutions solve the original Project Euler problem and have a perfect score of 100% at Hackerrank, too | |
yellow | solutions score less than 100% at Hackerrank (but still solve the original problem easily) | |
gray | problems are already solved but I haven't published my solution yet | |
blue | solutions are relevant for Project Euler only: there wasn't a Hackerrank version of it (at the time I solved it) or it differed too much | |
orange | problems are solved but exceed the time limit of one minute or the memory limit of 256 MByte | |
red | problems are not solved yet but I wrote a simulation to approximate the result or verified at least the given example - usually I sketched a few ideas, too | |
black | problems are solved but access to the solution is blocked for a few days until the next problem is published | |
[new] | the flashing problem is the one I solved most recently |
I stopped working on Project Euler problems around the time they released 617.
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 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 |
151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 |
176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 |
201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 |
226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 |
251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 |
276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 |
301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 |
326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 |
351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 |
376 | 377 | 378 | 3 |