The Fibonacci sequence is defined by the recurrence relation:
$F_n = F_{n – 1} + F_{n – 2}$, where $F_1 = 1$ and $F_2 = 1$.
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$.
To solve this problem, we can generate the Fibonacci sequence iteratively, checking for the given pandigital conditions as we go. We’ll start by defining a function to check if a number is pandigital.
“`python
def is_pandigital(n):
digits = set(str(n))
return len(digits) == 9 and ‘0’ not in digits
“`
Now, we’ll create a loop to generate the Fibonacci sequence until we find a number that satisfies both conditions:
“`python
F1 = F2 = 1
k = 2
while True:
Fn = F1 + F2
k += 1
if is_pandigital(Fn % 1000000000) and is_pandigital(int(str(Fn)[:9])):
break
F1, F2 = F2, Fn
“`
Finally, we print the value of `k`, which represents the index of the first Fibonacci number that satisfies the given pandigital conditions:
“`python
print(k)
“`
Putting it all together, the Python code to solve this problem is:
“`python
def is_pandigital(n):
digits = set(str(n))
return len(digits) == 9 and ‘0’ not in digits
F1 = F2 = 1
k = 2
while True:
Fn = F1 + F2
k += 1
if is_pandigital(Fn % 1000000000) and is_pandigital(int(str(Fn)[:9])):
break
F1, F2 = F2, Fn
print(k)
“`
When you run this code, it will output the value of `k`, which represents the index of the first Fibonacci number where both the last nine digits and the first nine digits are pandigital.
More Answers:
Optimum PolynomialTriangle Containment
Special Subset Sums: Optimum