Problem 104: Pandigital Fibonacci ends

It turns out that F_541, which contains 113 digits, is the first Fibonacci number for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9, but not necessarily in order).
And F_2749, which contains 575 digits, is the first Fibonacci number for which the first nine digits are 1-9 pandigital.

Given that F_k is the first Fibonacci number for which the first nine digits AND the last nine digits are 1-9 pandigital, find k.

My Algorithm

My solution is based on a stripped down version of my BigNum class and stores 9 digits per cell, that's why it's called BillionNum (because MaxDigit = 1000000000 → 10^9).
It only supports operator+=.

isPandigital(x, digits) returns true if x is 1..digits-pandigital, e.g. isPandigital(312, 3) == true because 312 is 3-pandigital.
The original problem assumes digits = 9.x 's digits are chopped off step-by-step and a bitmask tracks which digits we have already seen.
Zero is not part of any pandigital number, not even implicit leading zeros.

The main routines analyzes the lower digits first.
Only when they are pandigital then the highest digits are checked, too, because that's bit slower:
I take all digits from the highest cell of my BillionNum. If there are too few digits, all digits in its neighboring cell are included.
We might have too many digits now, therefore I remove the lowest digit until the number of digits is correct.
If these digits are pandigital, then we are done.

Finding the next Fibonacci number involves operator+= of BillionNum. The default algorithm is:F_{next} = F_a + F_bF_a = F_bF_b = F_{next}
→ quite a few memory allocations, and many object constructor/destruction will take place behind the curtain.
A simple trick to improve performance is to use these equations instead, which get rid of these "memory/object bookkeeping" effects:F_a += F_b (add in-place)F_a <=> F_b (swap contents of both object, which is technically just swapping two pointers)

The main performance boost comes from my next trick:

we check the lowest digit whether they are pandigital

we check the highest digit whether they are pandigital

but we don't care what the other digits are

→ whenever a number exceeds a certain size, I delete digits from the middle
Currently I delete if there are more than 10 cells, which represent 9 x 10 = 90 digits.
There is no special reason why I chose 10, it was the first number that I tried and it worked immediately.
The solution is found 100x faster now ...

Changelog

Hackerrank

Difficulty

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

Hackerrank describes this problem as easy.

Note:Hackerrank has strict execution time limits (typically 2 seconds for C++ code) and often a much wider input range than the original problem.In my opinion, Hackerrank's modified problems are usually a lot harder to solve. As a rule thumb: brute-force is rarely an option.

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.

The 310 solved problems (that's level 12) had an average difficulty of 32.6&percnt; at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of &approx;60000 in August 2017)
at Hackerrank's Project Euler+.

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

Copyright

I hope you enjoy my code and learn something - or give me feedback how I can improve my solutions.All of my solutions can be used for any purpose and I am in no way liable for any damages caused.You can even remove my name and claim it's yours. But then you shall burn in hell.

The problems and most of the problems' images were created by Project Euler.Thanks for all their endless effort !!!

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 !