Pandigital Fibonacci Ends (104)
In Problem 104, we're asked to find the first Fibonacci number where the first and last 9 digits respectively are pandigital (containing the digits 1 to 9 exactly once).
The Fibonacci numbers grow exponentially, their number of digits grows linearly. Keeping track of the whole numbers seems hopeless.
For the lower 9 digits, we can just make use of the addition with modulus. We are only interested in
We only need to store the low part of the Fibonacci numbers and discard the high part. This lets us iterate easily through all the numbers, no matter their actual size.
Iterating through the low part means that we can check the pandigital property there. This already makes the list of candidates much shorter. For these candidates, we need to check the high part.
Using the logarithmic approximation to the Fibonacci numbers, we do the following:
- Compute
using the approximation. - Isolate the fractional part,
as we don't care about the absolute magnitude of the number, only the digits. - As
, we can make a nine digit number via . - We check whether the that nine digit number is pandigital.
The compute performance of the check for a fixed
Computing the full Fibonacci numbers using a big number library will take time linear to