---
title: "Fibonacci game"
output:
   bookdown::html_book
  #bookdown::pdf_book
header_includes:
  - \usepackage{amsmath}
  - \usepackage{amssymb}
  - \usepackage{amsthm}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# The magic trick

To perform this little magic trick, all you need is paper and a pen... To prepare, make sure to write the following statements on two separate pieces of paper beforehand:
$$\frac{l_1+\dots+l_{10}}{l_7}=11$$
and 
$$\frac{l_{10}}{l_9}=1.61...$$

Start by handing these folded pieces of paper to two audience members, ensuring they cannot read the contents just yet.

Here are the instructions for the audience:

> You now need something to write with and a sheet of paper containing ten blank lines (numbered vertically from one to ten). Choose two positive numbers and write them on the first two lines. Then, fill in each subsequent line by calculating the sum of the two lines immediately preceding it. Thus, on the third line, you will write the sum of the numbers on lines one and two; on the fourth line, the sum of the numbers on lines two and three; and so on, until you have filled the tenth line (the sum of the numbers on lines eight and nine). Finally, calculate:   
- the quotient of the result on line ten divided by the result on line nine  
- the sum of the results obtained on all lines divided by the result on line 7.

Once every participant has finished, have the two audience members read out what you wrote on the pieces of paper. But how were you able to determine the result in advance?  





# When magic uses math...

Let us now reveal the secrets behind this magic trick using mathematics.

The sum of lines 1 to 10 divided by the number written on line 7 always equals 11.

Let $X$ and $Y$ be the numbers chosen by your friend. They appear on lines 1 and 2, respectively. Since the number on line 3 is the sum of the previous two, it is $X + Y$. Similarly, we find the expression for the number on line 4: $(X + Y) + Y = X + 2Y$. Continuing this process for all 10 lines yields:

1) $X$   
2) $Y$  
3) $X +Y$  
4) $X + 2Y$  
5) $2X + 3Y$  
6) $3X + 5Y$  
7) $5X + 8Y$  
8) $8X + 13Y$  
9) $13X + 21Y$  
10) $21X + 34Y$.  


We can then calculate the sum of the 10 lines: $55X + 88Y$. We see that
$$55X + 88Y = 11 \times 5X + 11 \times 8Y = 11(5X + 8Y),$$
which ensures that the sum of lines 1 through 10, divided by the number on line 7
($5X + 8Y$), always equals 11, regardless of the values of $X$ and $Y$. 

## The number on line 10 divided by the number on line 7 begins with 1.61

To prove the second property underlying the magic trick, let us first demonstrate the following fact:

\begin{lemma}
If $A$, $B$, $C$, and $D$ are four positive real numbers such that $C>0$, $D > 0$, and $\frac{A}{C} \leq \frac{B}{D}$, then
$$\frac{A}{C} \leq \frac{A + B}{C + D} \leq \frac{B}{D}.$$
\end{lemma}


\begin{proof}
First, let us show that $\frac{A}{C} \leq \frac{A + B}{C + D}$. This result follows from the following decomposition:

\begin{eqnarray*}
\frac{A + B}{C + D} &=& \frac{A}{C}\frac{C}{C + D} + \frac{B}{C + D}\\
                    &=& \frac{A}{C}\left(1-\frac{D}{C + D}\right) + \frac{B}{C + D}\\
                    &=& \frac{A}{C} + \frac{BC - AD}{C(C + D)}.
\end{eqnarray*}

It then suffices to note that the term $\frac{BC - AD}{C(C + D)}$ is positive, since $A$, $B$, $C$, and $D$ are positive and $$\frac{A}{C} \leq \frac{B}{D} \Rightarrow BC - AD \geq 0.$$

The inequality $\frac{A + B}{C + D} \leq \frac{B}{D}$ is demonstrated in a very similar way. Indeed, by swapping the roles of $A$ (and $C$) with those of $B$ (and $D$), we obtain $\frac{A + B}{C + D}= \frac{B}{D} + \frac{AD - BC}{D(D+C)}$, and the term $\frac{AD-BC}{D(D+C)}$ is negative for the same reasons cited above to prove that the term $\frac{BC - AD}{C(C + D)}$ is positive. \end{proof}

The property we have just demonstrated allows us to show that the quotient of line 10 divided by line 9 can be bounded as follows:

$$1.615385 \approx \frac{21}{13} \leq \frac{21X + 34Y }{13X + 21Y} \leq  \frac{34}{21} \approx 1.619048,
$$
by applying it with $A=21X$, $B=34Y$, $C=13X$, and $D=21Y$.

And that is where the second property we used comes from.

Now you know the secrets behind this magic trick. It’s your turn to amaze your friends
using mathematical tricks!


# Going further

Mathematics allows us to take this even further. Suppose we continue
filling in our sheet until we have $n$ lines. We can then show that, as $n$ becomes large, the quotient of line $n$ divided by line $n-1$ approaches the golden ratio—regardless of the values ​​of $X$ and $Y$: $$\phi=(1 + \sqrt{5})/2 \approx 1.618034.$$

This number is so named because of the large number of mathematical problems in which it appears (notably, it is one of the roots of the polynomial $x^2-x-1$) as well as the many other fields where it is found: architecture, biology, etc.

## The Fibonacci sequence

Let us begin by defining the Fibonacci sequence.

\begin{definition}
The Fibonacci sequence is the sequence $(u_n)_{n\in\mathbb{N}}$ defined by the following recurrence relation:
$$\begin{cases}
  u_0=0\\
  u_1=1\\
  \forall n\geq 2,\, u_n=u_{n-1}+u_{n-2}
 
\end{cases}
$$
\end{definition}




The recurrence relation $u_n=u_{n-1}+u_{n-2}$ allows us to find an explicit expression for $u_n$ via the following proposition.

\begin{proposition}
There exist two real constants $a_1$ and $a_2$ such that $$\forall n\in\mathbb{N},\, u_n=a_1x_1^n+a_2x_2^n,$$ where $x_1$ and $x_2$ are the roots of the polynomial $x^2-x-1$.  
\end{proposition}

What are the values ​​of $x_1$ and $x_2$? They are the solutions to $x^2-x-1=0$. We have $$x^2-x-1=(x-\frac{1}{2})^2-\frac{1}{4}-1=(x-\frac{1}{2})^2-\frac{5}{4}=(x-\frac{1}{2})^2-(\frac{\sqrt{5}}{2})^2=(x-\frac{1}{2}-\frac{\sqrt{5}}{2})(x-\frac{1}{2}+\frac{\sqrt{5}}{2})$$  

We thus conclude that the roots of $x^2-x-1$ are $x_1=\frac{1}{2}+\frac{\sqrt{5}}{2}=\frac{1+\sqrt{5}}{2}$ and $x_2=\frac{1}{2}-\frac{\sqrt{5}}{2}=\frac{1-\sqrt{5}}{2}$. We note that $x_1$ is none other than the golden ratio $\phi$. In the following, we will denote $\phi:=x_1$ and $\phi_c:=x_2$.

It is easily observed that the initial conditions $u_0=0$ and $u_1=1$ imply
$$\begin{cases} a_1+a_2=0\\
                a_1\phi+a_2\phi_c=1
  \end{cases}              
  \Leftrightarrow
  \begin{cases} a_2=-a_1\\
                a_1\frac{2\sqrt{5}}{2}=1
  \end{cases} 
  \Leftrightarrow
  \begin{cases} a_1=\frac{1}{\sqrt{5}}\\
                a_2=-\frac{1}{\sqrt{5}}
  \end{cases} 
$$

We then have
\begin{equation}\label{u_n}
\forall n\in\mathbb{N},\,u_n=\frac{1}{{\sqrt{5}}}\left(\phi^n-\phi_c^n\right).
\end{equation}

## Connection to our problem

It is easily shown that, for any $X$ and $Y$, and for $n\geq 1$, the result $r_n$ obtained at line $n$ satisfies $$ (\mathcal{H}_n): r_n=u_{n-1}X+u_{n}Y.$$

\begin{proof}   

{\bf Base case:}\\   
For $n=1$, we saw that $r_1=Y$ and $u_0X+u_1Y=0\times X+ 1\times Y=Y$.\\  
For $n=2$, we saw that $r_2=X+Y$ and $u_1X+u_2Y=1\times X+ 1\times Y=X+Y$.\\
Thus, $(\mathcal{H}_1)$ and $(\mathcal{H}_2)$ hold.  

{\bf Inductive step:}\\
Assume that for all $1\leq p\leq n$, we have $(\mathcal{H}_p): r_p=u_{p-1}X+u_{p}Y$. Let us prove $(\mathcal{H}_{n+1}): r_{n+1}=u_{n}X+u_{n+1}Y $.

By definition, we have:
$$r_{n+1}=r_n+r_{n-1}\stackrel{(\mathcal{H}_n) \text{ and } (\mathcal{H}_{n-1})}{=}u_{n-1}X+u_{n}Y+u_{n-2}X+u_{n-1}Y=(u_{n-1}+u_{n-2})X+(u_{n}+u_{n-1})Y=u_{n}X+u_{n+1}Y.$$

The property $(\mathcal{H}_{n})$ is therefore true for all $n\geq 1$.

\end{proof}


Based on what we have just proved and Lemma 1.1, we obtain, for any $X$ and $Y$:

\begin{equation}\label{ineg}
\forall n\geq 2,\, \min\left(\frac{u_{n-1}}{u_{n-2}},\frac{u_{n}}{u_{n-1}}\right)\leq \frac{r_n}{r_{n-1}}= \frac{u_{n-1}X+u_{n}Y}{u_{n-2}X+u_{n-1}Y}\leq \max\left(\frac{u_{n-1}}{u_{n-2}},\frac{u_{n}}{u_{n-1}}\right)
\end{equation}



Finally, let us prove that the quotients $\frac{r_n}{r_{n-1}}$ converge to the golden ratio. Let's start by proving that $\lim_{n\rightarrow+\infty} \frac{u_n}{u_{n-1}}=\phi.$

\begin{proof} 
Using (\ref{u_n}) we obtain $$\frac{u_n}{u_{n-1}}=\frac{    \frac{1}{\sqrt{5}}\left(\phi^n-\phi_c^n\right)}{\frac{1}{\sqrt{5}}\left(\phi^{n-1}-\phi_c^{n-1}\right)}=\frac{\left(\phi^n-\phi_c^n\right)}{\left(\phi^{n-1}-\phi_c^{n-1}\right)}=\frac{\phi^n\left(1-\left(\frac{\phi_c}{\phi}\right)^n\right)}{\phi^{n-1}\left(1-\left(\frac{\phi_c}{\phi}\right)^{n-1}\right)}=\phi\frac{\left(1-\left(\frac{\phi_c}{\phi}\right)^n\right)}{\left(1-\left(\frac{\phi_c}{\phi}\right)^{n-1}\right)}.$$ 
The limit is then easily obtained by noting that $|\frac{\phi_c}{\phi}|=|\frac{1-\sqrt{5}}{1+\sqrt{5}}|=|-1+\frac{2}{1+\sqrt{5}}|<1$.  


\end{proof}

Finally from (\ref{ineg}) and $\lim_{n\rightarrow+\infty} \frac{u_n}{u_{n-1}}=\phi$ we easily prove by the police theorem that $\lim_{n\rightarrow+\infty} \frac{r_n}{r_{n-1}}=\phi$.

## How many lines to know k numbers?

A closer look at what enabled the proof of convergence reveals that the convergence is exponential and, consequently, very rapid... Let us calculate, for example, the min and max bounds in (\ref{ineg})

```{r,include=FALSE}
library(plotly)

Fibonacci_game=function(x,y,K)
{
  num=rep(NA,K)
  r=rep(NA,K)
  min=rep(NA,K)
  max=rep(NA,K)
  num[1:2]=c(x,y)
  r[2]=y/x
  for (j in 3:K) 
  {
    num[j]=sum(num[j-(1:2)])
    r[j]=num[j]/num[j-1]
    min[j]=min(r[j],r[j-1])
    max[j]=max(r[j],r[j-1])
  }
  if (K>=10)
  {
  ratio=num[10]/num[9]
  total=sum(num[1:10])
  estim=num[7]*11
  }
  r1=data.frame(z=(3:(K)),max=max[3:(K)],min=min[3:(K)],phi=rep((1+sqrt(5))/2,K-2))
  plot_ly(r1,x=~z,y=~max, type = 'scatter', mode = 'lines') %>%
    add_lines(x=~z,y=~min) %>%
    add_lines(x=~z,y=~phi)
  
  return(list('num'=num,'ratio'=ratio, 'total'=total,'estim'=estim,'r'=r,'min'=min,'max'=max ))
}

K=100
RE=Fibonacci_game(0,1,K)
r1=data.frame(z=(1:(K)),max=RE$max[1:(K)],min=RE$min[1:(K)],phi=rep((1+sqrt(5))/2,K))

Ligne=rep(NA,8)
names(Ligne)=paste(1:8,' first correct digits',sep='')
for (j in 0:7)
{
  eta=10^{-j}
  Ligne[j+1]=(which(((floor(r1$phi/eta)*eta-floor(r1$min/eta)*eta)==0) & ((floor(r1$phi/eta)*eta-floor(r1$max/eta)*eta)==0) ))[1]
}
Ligne
```



```{r,echo=FALSE,comment=''}

CV=data.frame(n=1:30,min=RE$min[1:30],max=RE$max[1:30],Gap=RE$max[1:30]-RE$min[1:30])
row.names(CV)=NULL
print(CV,digits=13)
```
Convergence is quite rapid; we can therefore determine how many lines are needed to know the first k digits of the result.

| Correct digits | 1. | 1.6 |  1.61 | 1.618 | 1.6180 | 1.61803 | 1.618033 |1.6180339 |
|:-|-:|-:|-:|-:|-:|-:|-:|-:|
| Lines required | 6  | 7  |  10 |  15 |  15 |  17 |  22 | 22  |


For the online application, click the following link (https://laurent-delsol-idp.shinyapps.io/Fibonacci_Game/).