<< problem 307 - Chip Defects Integer Ladders - problem 309 >>

Problem 308: An amazing Prime-generating Automaton

A program written in the programming language Fractran consists of a list of fractions.

The internal state of the Fractran Virtual Machine is a positive integer, which is initially set to a seed value.
Each iteration of a Fractran program multiplies the state integer by the first fraction in the list which will leave it an integer.

For example, one of the Fractran programs that John Horton Conway wrote for prime-generation consists of the following 14 fractions:

dfrac{17}{91}, dfrac{78}{85}, dfrac{19}{51}, dfrac{23}{38}, dfrac{29}{33}, dfrac{77}{29}, dfrac{95}{23}, dfrac{77}{19}, dfrac{1}{17}, dfrac{11}{13}, dfrac{13}{11}, dfrac{15}{2}, dfrac{1}{7}, dfrac{55}{1}

Starting with the seed integer 2, successive iterations of the program produce the sequence:
15, 825, 725, 1925, 2275, 425, ..., 68, 4, 30, ..., 136, 8, 60, ..., 544, 32, 240, ...

The powers of 2 that appear in this sequence are 2^2, 2^3, 2^5, ...
It can be shown that all the powers of 2 in this sequence have prime exponents and that all the primes appear as exponents of powers of 2, in proper order!

If someone uses the above Fractran program to solve Project Euler Problem 7 (find the 10001st prime), how many iterations would be needed until the program produces 2^{10001st \space prime} ?

My Algorithm

The crucial discovery is that all fractions can be factorized such that the largest prime factor is 29 and no prime appears as a square (or cube).
The sequence becomes:

dfrac{17}{7 * 13}, dfrac{2 * 3 * 13}{5 * 17}, dfrac{19}{3 * 17}, dfrac{23}{2 * 19}, dfrac{29}{3 * 11}, dfrac{7 * 11}{29}, dfrac{5 * 19}{23}, dfrac{7 * 11}{19}, dfrac{1}{17}, dfrac{11}{13}, dfrac{13}{11}, dfrac{3 * 5}{2}, dfrac{1}{7}, dfrac{5 * 11}{1}

The 10001st prime needs 10001 bits to be represented - way beyond the 64 bits of unsigned long long.
However, I can factorize the current number as well and it's initial value is 2 = 2^1.
I could include all the other (non-existing) prime factors, too, so that
2 = 2^1 * 3^0 * 5^0 * 7^0 * 11^0 * 13^0 * 17^0 * 19^0 * 23^0 * 29^0

Each fraction increments or decrements some exponents. No exponent must become negative because then it wouldn't be an integer anymore.
Whenever all exponents except the exponent of 2 are zero, then I found another prime and it's the exponent of 2.

I wrote a brute-force function enumerate() that finds the 100th prime in a few seconds (matching the values of OEIS A007547).
But more important, I included a "visualization" of each step which revealed:

• the exponents of 2, 3, 5, 7 change a lot, often they are incremented or decremented
• the exponents of 11, 13, 17, 19, 23, 29 never exceed 1
• actually at most one of the exponents of 11, 13, 17, 19, 23, 29 is 1 and all the others are zero
These are the 19 steps for the first prime (I replaced zeros by an underline to emphasize the patterns):

2357111317192329
step 0:1_________
step 1:_11_______
step 2:_12_1_____
step 3:__2______1
step 4:__211_____
step 5:__21_1____
step 6:__2___1___
step 7:111__1____
step 8:111_1_____
step 9:1_1______1
step 10:1_111_____
step 11:1_11_1____
step 12:1_1___1___
step 13:21___1____
step 14:21__1_____
step 15:2________1
step 16:2__11_____
step 17:2__1_1____
step 18:2_____1___
step 19:2_________

My function search() is build on these properties. It's a state machine which has 7 states S_, S11, S13, S17, S19, S23 and S29
(where the number after S indicates the exponent which is currently 1 or S_ if all are zero, which is the default state, too).

The 14 fractions can be interpreted as:

dfrac{17}{91} = dfrac{17}{7 * 13} → when in state S13 then try to decrement the exponent of 7 and continue in state S17

dfrac{78}{85} = dfrac{2 * 3 * 13}{5 * 17} → when in state S17 then try to decrement the exponent of 5, increment the exponents of 2 and 3 and continue in state S13

dfrac{19}{51} = dfrac{19}{3 * 17} → when in state S17 then try to decrement the exponent of 3 and continue in state S19

... and so on. The current state is in the denominator and the follow-up state in the numerator.
Any prime factors 2, 3, 5, 7 found in the numerator are incremented but if found in the denominator then they are decremented.

That state machine is substantially faster than the previously mentioned enumerate() function.
It needs about 10 seconds to find the first 500 prime numbers but then becomes slower and slower (because the number of steps seems to grow exponentially).

However, I saw a few patterns in the output of enumerate() that allow me to "merge" states:
depending on the current state s_i and the exponents of 2, 3, 5, 7, the follow-up state s_{i+1} might return to the previous state s_i (while modifying the exponents of 2, 3, 5, 7).
Look for the #ifdef OPTIMIZE pragmas to locate my optimizations.

The first optimization can be found in state S11:
• if the exponent of 3 is positive (three > 0), then it's decremented and followed by state S29
• state S29 increments the exponent of 7 and returns to state S11
→ that's a simple loop which simplifies to seven += three; three = 0;
→ the total number of steps would be 2 * three (visit S11 and S29 once per iteration)
→ this optimization saves about 1/3 of all steps (143,165,562 iterations to count the 213,945,763 steps for the first 100 primes)

The second optimization can be found in state S13:
• if the exponents of 5 and 7 are both positive (seven > 0 && five > 0), then both are decremented and followed by state S17
• state S17 increments the exponents of 2 and 3, decrements the exponent of 5 and returns to state S13
→ that's another loop which stops whenever five or seven becomes zero
→ there are std::min(five, seven) iterations of that loop and each requires 2 steps
→ saves about 1/3 (143,263,171 iterations to process the 213,945,763 steps for the first 100 primes)
→ saves about 2/3 together with the first optimization (72,482,969 iterations to count the 213,945,763 steps for the first 100 primes)

I was surprised that these two optimization are sufficient to solve the problem (10001th prime):
the result is a number with 16 digits and found in about a minute but according to my logging the program processed 513,273,040,838,264 iterations.
That's impossible - the code runs on a modest Core i7 CPU which is clocked somewhere between 3 and 4 GHz (depending on overall load).
Even the best-case scenario would mean that roughly 250 * 10^9 CPU cycles were "burnt" while processing 513 * 10^12 iterations - that's 2000 iterations per CPU cycle !!!

Clearly the GCC compiler found some optimization that I missed. And after a few minutes I stumbled across it (and it's so simple I wonder why I didn't spot it earlier).
The third optimization was hidden in state S19 and its structure is similar to the first optimization:
• if the exponent of 2 is positive (two > 0), then it's decremented and followed by state S23
• state S23 increments the exponent of 5 and returns to state S19
→ that's a simple loop which simplifies to five += two; two = 0;
→ the total number of steps would be 2 * two (visit S19 and S23 once per iteration)
→ saves about 1/3 (143,313,579 iterations to process the 213,945,763 steps for the first 100 primes)

And now comes the incredible part:
if all three optimzations are enabled simultaneously then the total number of iterations needed to figure out the number of steps quickly drops to less than 1%:
1,850,784 iterations are sufficient to process the 213,945,763 steps for the first 100 primes (that's 0.865%, or put in another way, I saved 99.135%).
And 76 * 10^9 iterations to know that it takes 1.5 * 10^15 steps (i.e. saved 99.9950278%) to find the 10001th prime.
By the way: that's only 2 CPU cycles per iteration, so there's still "something going on" in the GCC optimizer ...

The execution time didn't drop when I added that third optimization (because GCC discovered it anyway)
but other C++ compilers with less smart optimizer should now be able to emit reasonably fast code, too.

Alternative Approaches

The Project Euler forum is full of other optimizations which aren't as simple as mine.
The fastest programs solve the problem in less than a second.

Note

Another tough lesson in how modern software can almost outsmart a human being ...

The source code doesn't need anything from my toolbox and is one of the longest with "original" code written specificly to solve only one problem.

Interactive test

You can submit your own input to my program and it will be instantly processed at my server:

Input data (separated by spaces or newlines):

This is equivalent to
echo 10 | ./308

Output:

Note: the original problem's input 10001 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. Or just jump to my GitHub repository.

       #include <iostream>
#include <iomanip>
#include <algorithm>

// slowly step through all iterations until enough primes are found; display the exponents in each step
unsigned long long enumerate(unsigned int numPrimes, bool displaySteps = true)
{
const auto NumFractions = 14;
const auto NumExponents = 10;
const char Fractions[NumFractions][NumExponents] =
{
// represent fractions as exponents of these primes:
//          2,  3,  5,  7, 11, 13, 17, 19, 23, 29
// 17/91 =             1/7   *1/13*17
{  0,  0,  0, -1,  0, -1, +1,  0,  0,  0 },
// 78/85 =  2 * 3 *1/5        *13*1/17
{ +1, +1, -1,  0,  0, +1, -1,  0,  0,  0 },
// 19/51 =     1/3               *1/17*19
{  0, -1,  0,  0,  0,  0, -1, +1,  0,  0 },
// 23/38 = 1/2                       *1/19*23
{ -1,  0,  0,  0,  0,  0,  0, -1, +1,  0 },
// 29/33 =     1/3       *1/11                *29
{  0, -1,  0,  0, -1,  0,  0,  0,  0, +1 },
// 77/29 =              7* 11                *1/29
{  0,  0,  0, +1, +1,  0,  0,  0,  0, -1 },
// 95/23 =          5                 *19*1/23
{  0,  0, +1,  0,  0,  0,  0, +1, -1,  0 },
// 77/19 =              7* 11        *1/19
{  0,  0,  0, +1, +1,  0,  0, -1,  0,  0 },
//  1/17 =                       *1/17
{  0,  0,  0,  0,  0,  0, -1,  0,  0,  0 },
// 11/13 =                 11*1/13
{  0,  0,  0,  0, +1, -1,  0,  0,  0,  0 },
// 13/11 =                1/11*13
{  0,  0,  0,  0, -1, +1,  0,  0,  0,  0 },
// 15/2  = 1/2* 3 * 5
{ -1, +1, +1,  0,  0,  0,  0,  0,  0,  0 },
//  1/7  =             1/7
{  0,  0,  0, -1,  0,  0,  0,  0,  0,  0 },
// 55/1  =          5     *11
{  0,  0, +1,  0, +1,  0,  0,  0,  0,  0 }
};

// seed = 2 = 2^1 => set first exponent to 1
int current[NumExponents] = { +1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

// count number of steps required to find that prime
unsigned long long steps = 0;

// number of primes encountered along the way
unsigned int numFound = 0;
while (numFound < numPrimes)
{
// print current step
if (displaySteps)
{
std::cout << "step " << std::setw(3) << steps << ": ";
for (auto x : current)
// replace zeros by dashes to ease "pattern detection"
std::cout << std::setw(2) << (x == 0 ? "-" : std::to_string(x)) << " ";
std::cout << std::endl;
}

// apply first rule that produces an integer
for (const auto& fraction : Fractions)
{
bool matchingFraction = true;
// no exponent should become negative after applying a rule
for (auto exponent = 0; exponent < NumExponents; exponent++)
matchingFraction &= (current[exponent] + fraction[exponent]) >= 0;

// yes, that rule rules !
if (matchingFraction)
{
for (auto exponent = 0; exponent < NumExponents; exponent++)
current[exponent] += fraction[exponent];
// no further rules
break;
}
}

steps++;

// one more prime ?
bool isPrime = true;
// all but the first exponent are zero
for (auto exponent = 1; exponent < NumExponents; exponent++)
isPrime &= current[exponent] == 0;
if (isPrime) // only 2s, ignore seed value (2^1)
{
numFound++;
if (displaySteps)
std::cout << "prime " << current[0] << " @ step " << steps << std::endl;
}
}

return steps;
}

// treat the FRACTRAN sequence as a state machine
unsigned long long search(unsigned int numPrimes)
{
// and manually optimize a few states (GCC discovers a few optimizations on its own)
#define OPTIMIZED

// at most one exponent a,b,c,d,e,f of 11^a, 13^b, 17^c, 19^d, 23^e, 29^f can be one
// => seven states
enum State { S_, S11, S13, S17, S19, S23, S29 };
State state = S_; // default state

// the exponents of 2,3,5,7 can be any non-negative number
auto two   = 1;
auto three = 0;
auto five  = 0;
auto seven = 0;

unsigned long long steps = 0;
unsigned long long iterations = 0; // to measure how efficient my state optimizations are
unsigned int    numFound = 0;
while (true) // exit-condition in state S_
{
iterations++;

switch (state)
{
case S_:
if (three == 0 && five == 0 && seven == 0 && steps > 0)
{
numFound++;
//std::cout << "prime " << two << "/" << numFound << " @ " << steps
//          << " (" << iterations << " iterations => " << 100.0 * iterations / steps << "%)" << std::endl;
if (numFound == numPrimes)
return steps;
}

// fraction L: 15/2 = 3*5 / 2
if (two > 0)
{
two--; three++; five++;
// could be optimized, too:
//steps += two; three += two; five += two; two = 0; continue:
break;
}

// fraction M: 1/7
if (seven > 0)
{
// could be optimized, too
//steps += seven; seven = 0; continue;
seven--;
break;
}

// fraction N: 55/1 = 5 * 11
five++;
state = S11;
break;

case S11:
// fraction E: 29/33 = 29 / (3*11)
if (three > 0)
{
#ifdef OPTIMIZED
// S11 <=> S29
// S29 performs seven++ and immediately returns to the current state S11
steps += 2 * three;
seven += three;
three  = 0;
continue; // to avoid step++ after the switch-block
#endif
three--;
state = S29;
break;
}

// fraction K: 13/11
state = S13;
break;

case S13:
// fraction A: 17/91 = 17 / (7*13)
if (seven > 0)
{
#ifdef OPTIMIZED
// S13 <=> S17:
// as long as five and seven are not zero both are decremented by one
// whereas two and three are incremented by one
if (five > 0)
{
auto smallest = std::min(five, seven);
steps += 2 * smallest;
two   += smallest;
three += smallest;
five  -= smallest;
seven -= smallest;
continue; // to avoid step++ after the switch-block
}
#endif
seven--;
state = S17;
break;
}

// fraction J: 11/13
state = S11;
break;

case S17:
// fraction B: 78/85 = 2*3*13 / (5*17)
if (five > 0)
{
five--; two++; three++;
state = S13;
break;
}
// fraction C: 19/51 = 19 / (3*17)
if (three > 0)
{
three--;
state = S19;
break;
}

// fraction I: 1/17
state = S_;
break;

case S19:
// fraction D: 23/38 = 23 / (2*19)
if (two > 0)
{
#ifdef OPTIMIZED
// S19 <=> S23 (apparently automatically detected by GCC at optimization level -O3, too)
// two is decremented until it's zero while five is incremented
steps += 2 * two;
five  += two;
two    = 0;
continue; // to avoid step++ after the switch-block
#endif
two--;
state = S23;
break;
}

// fraction H: 77/19 = 7*11 / 19
seven++;
state = S11;
break;

case S23:
// fraction G: 95/23 = 5*19 / 23
five++;
state = S19;
break;

case S29:
// fraction F: 77/29 = 7*11 / 29
seven++;
state = S11;
break;
}

// next iteration
steps++;
}
}

int main()
{
unsigned int numPrimes = 10001;
std::cin >> numPrimes;

// visualize steps
//enumerate(numPrimes, true);

// fast algorithm to solve the problem
std::cout << search(numPrimes) << std::endl;
return 0;
}


This solution contains 31 empty lines, 65 comments and 10 preprocessor commands.

Benchmark

The correct solution to the original Project Euler problem was found in 46.7 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

December 11, 2017 submitted solution

Difficulty

Project Euler ranks this problem at 65% (out of 100%).

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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
The 310 solved problems (that's level 12) had an average difficulty of 32.6% at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of ≈60000 in August 2017) at Hackerrank's Project Euler+.

My username at Project Euler is stephanbrumme while it's stbrumme at Hackerrank.

Look at my progress and performance pages to get more details.

 << problem 307 - Chip Defects Integer Ladders - problem 309 >>
more about me can be found on my homepage, especially in my coding blog.
some names mentioned on this site may be trademarks of their respective owners.
thanks to the KaTeX team for their great typesetting library !