worked some on a fun draft

master
Rachel Lambda Samuelsson 2023-03-05 15:45:55 +01:00
parent e1ce70303b
commit 356736df29
3 changed files with 71 additions and 18 deletions

View File

@ -0,0 +1,71 @@
---
layout: post
title: "A favorite proof of mine"
katex: True
---
There are a lot of proofs in mathematics. Many of them serve to verify what we know intuitively know to be true, some of them shed light on new methods, and some reveal new ways to view old ideas. There are proofs which leave us with a headache, some which leave us bored, and some which leave us with wonder and awe. In this blog post I will share a beautiful proof leading to a closed formula for the $n$-th Fibonacci number, taking us on a detour into functional programming and linear algebra.
<!--more-->
# The Fibonacci Numbers
The Fibonacci numbers are a sequence of numbers starting with two ones where each number is the sum of the last two. That is $0, 1, 1, 2, 3, 5, 8, 13 \dots$. If we wanted to be more precise we could define a sequence $\{f\}_{n=0}^{\infty}$ by the following recurisve definition:
$$
f_n = \begin{cases}
0\;\textrm{if}\;n=0 \\
1\;\textrm{if}\;n=1 \\
f_{n-2} + f_{n-1}\;\textrm{otherwise}
\end{cases}
$$
The Fibonacci numbers have become a bit of a poster child for recursive definitions, perhaps due to it's simplicity. You'll surely find it in the early chapters of most books teaching functional programming (a programming paradigm where recursive definitions are common).
Indeed, if we open [Chapter 5: Recursion](https://learnyouahaskell.github.io/recursion.html) of [LYAH](https://learnyouahaskell.github.io/) we are greeted with the following.
> Definitions in mathematics are often given recursively. For instance, the fibonacci sequence is defined recursively.
Likewise, in [Chapter 1.2.2 Tree Recursion](https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/6515/sicp.zip/full-text/book/book-Z-H-4.html#%_toc_%_sec_1.2.2) of [SICP](https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/6515/sicp.zip/index.html) we are yet again greeted by an old friend
> Another common pattern of computation is called tree recursion. As an example, consider computing the sequence of Fibonacci numbers
With this in mind, it might come as a suprise that there is a closed, non-recursive, formula for the $n$-th Fibonacci number. Perhaps more surprising is that we will discover this formula by using the ideas presented in the above chapter of SICP.
# Programatically calculating the $n$-th Fibonacci number
A naive way of calculating the $n$-th fibonacci number is to use the definition above. Check if $n = 0$, if $n = 1$, and otherwise calculating $f_{n-2}$ and $f_{n-1}$. However, unless $n$ is $0$ or $1$. Programatically this corresponds to the following Haskell code:
```
fib :: Integer -> Integer
fib 0 = 0
fib 1 = 1
fib n = fib (n-2) + fib (n-1)
```
However, there is an issue with this method, many fibonacci numbers will be calculated numerous times, as for each fibonacci number evaluated we split into two paths, evaluating the previous and twice previous fibonacci number. The reader which prefers visuals might appreciate Figure 1.5 from the SICP chapter.
How might we fix this then? A human calculating the $n$-th Fibonacci number might construct a list of Fibonacci numbers, calculating each fibonacci number only once. While it is possible to do this on the computer it is actually superflous to carry all previous numbers, as we only need the previous two in order to calculate the next one. We might think of this as a window, moving along the Fibonacci numbers, taking $n$ steps to arrive at $f_n$. In code we could represent this as follows:
```
-- steps -> f n-2 -> f n-1 -> f n
window :: Integer -> Integer -> Integer -> Integer
window 0 a b = a
window steps a b = window (steps-1) b (a+b)
fib :: Integer -> Integer
fib n = window n 0 1
```
In each step we move the 2-slot window by replacing the first slot in the window by what was previously in the second, and filling the new second slot of the window by the sum of the previous two slots. This is then repeated $n$ times, and then the first slot of the window is returned.
# Mathematically calculating the $n$-th Fibonacci number
What does this have to do with mathematics, and this beautiful proof which I have promised? We shall begin to translate this moving window into the language of mathematics, our window is a pair of numbers, so why not represent is as a vector. Furthermore, we may view sliding our window one step as a function $S$ from vectors to vectors. This poses an interesting question, is this function a linear transformation?
\begin{align}
test & test2 \\
test3 & test4
\end{align}
$$ S\left(\begin{bmatrix} a \\ b \end{bmatrix}\right) + S\left(\begin{bmatrix} c \\ d \end{bmatrix}\right) = \begin{bmatrix} b \\ a + b \end{bmatrix} + \begin{bmatrix} d \\ c + d \end{bmatrix} = \begin{bmatrix} b + d \\ a + b + c + d \end{bmatrix} $$
$$ S\left(\begin{bmatrix} a \\ b \end{bmatrix} + \begin{bmatrix} c \\ d \end{bmatrix}\right) = S\left(\begin{bmatrix} a + c \\ b + d \end{bmatrix}\right) = \begin{bmatrix} b + d \\ a + b + c + d \end{bmatrix} $$

View File

@ -1,16 +0,0 @@
---
layout: post
title: "Fibbonacci"
---
{% katexmm %}
When talking about recurisve sequences there is a poster child, the fibbonacci sequence. $F_{n} = F_{n-1} + F_{n-2}$, but did you know there is a closed, non-recursive, $O(1)$, formula for the fibbonacci numbers $F_n = \frac{\varphi^n-\psi^n}{\sqrt 5}$. Where $\varphi = bla$ and $\psi = bla$. This blog post will explore several ways to prove this identity, in order to not only verify the formula, but see how you could have discovered it, as well as uncovering a fun connection to a $O(n)$ algorithm and linear algebra.
{% endkatexmm %}
<!--more-->
{% katexmm %}
{% endkatexmm %}

View File

@ -7,6 +7,4 @@ UIP, martin hoffmann, groupoid model...
<!--more-->
{% katexmm %}
{% endkatexmm %}