\chapter{Basic concepts}

\section{Algorithms}

\section{Mathematical Preliminaries}

\subsection{Mathematical induction}

\begin{myexercise}{4}{18}

    Let $F_n$ be the $n$-th Fibonacci number, defined by the recursion relation
    \[
    \begin{cases}
        F_0 = 1, \\
        F_1 = 1, \\
        F_n = F_{n-1} + F_{n-2}, \quad n \geq 2.
    \end{cases}    
    \]

    Let $\phi = \frac{1+\sqrt{5}}{2}$ be the golden ratio. 

    We have
    \begin{equation}
        F_n \leq \phi^{n-1}
        \label{eq:fibo_leq_golden_ratio}
    \end{equation}
    $\forall n \in \mathbb{N}$.

    Prove that, in addition to Eq. \ref{eq:fibo_leq_golden_ratio}, $F_n \geq \phi^{n-2}$.

    \begin{myanswer}

        Let $P(n)$ be the assertion that $F_n \geq \phi^{n-2}$.

        We proceed by recursion on $n$.        
        \begin{equation}
            1 + \phi = \phi^2 
            \label{eq:golden_ratio_squared}
        \end{equation}

        \emph{Initiation} For $n = 0$: $\phi^{0-2} = \phi^{-2} = \frac{1}{\phi^2}$. As $\phi^2 > 1$ . $\frac{1}{\phi^2} < 1$, so $P(0)$ is true.

        For $n = 1$: 
        $\phi^{1-2} = \phi^{-1} = \frac{1}{\phi}$. As $\phi > 1$, $\frac{1}{\phi} < 1$, so $P(1)$ is true.

        \emph{Heredity}
        Let $P(n)$ and $P(n-1)$ be true. We have to prove $P(n+1)$.
        \begin{align*}
            P(n) \wedge P(n-1) & \Leftrightarrow F_n \geq \phi^{n-2} \wedge F_{n-1} \geq \phi^{n-1-2}\\
            & \Leftrightarrow F_{n} + F_{n-1} \geq \phi^{n-2} + \phi^{n-3} \\
            & \Leftrightarrow F_n + F_{n-1} \geq \phi^{n-3} (1 + \phi)  \\
            & \Leftrightarrow F_n + F_{n-1} \geq \phi^{n-3} \phi^2  \qquad \text{using Eq. \ref{eq:golden_ratio_squared}} \\
            & \Leftrightarrow F_n + F_{n-1} \geq \phi^{n-1} \\
            & \Leftrightarrow F_{n+1} \geq \phi^{n+1-2} \\
            & \Leftrightarrow P(n+1)
        \end{align*}

        \emph{Conclusion} $P(0)$ and $P(1)$ are true. $P(n) \wedge P(n-1)$ true implies $P(n+1)$ true. So $P(n)$ is true for all $n \in \mathbb{N}$.

    \end{myanswer}
\end{myexercise}



\begin{myexercise}{5}{19}
A prime number is an integer $> 1$ that has no exact divisors other than $1$ and itself.

Using this definition and mathematical induction, prove that every integer $> 1$ can be written as a product of prime numbers or is a prime itself.

\begin{myanswer}
Let $P(n)$ be the assertion that $n$ can be written as a product of prime numbers or is a prime.
\begin{proof}
    
    Let $N > 1$ be the smallest integer such that $P(N)$ is false.
    
    As $\forall N' < N$, $N' > 1$, $P(N')$ is true.
    We have either of the following assertions:
    
    \begin{itemize}
        \item $N$ is a prime number;
        \item there exists $m$ and $n$ below $N$ such that, $m \times n = N$.
        As $m$ and $n$ are below $N$, they satisfies $P$, so they are either primes or a product of primes. So $mn = N$ is also a product of primes.
    \end{itemize}
    We have a contradiction, and $P(N)$ must be true.
    
\end{proof}
\end{myanswer}
\end{myexercise}

\begin{myexercise}{7}{19} 
Formulate and prove by induction a rule for the sums $1^2$, $2^2 - 1^2$, $3^2 - 2^2 + 1^2$, $4^2 - 3^2 + 2^2 - 1^2$, $5^2 - 4^2 + 3^2 - 2^2 + 1^2$, etc. 

\begin{myanswer}

$1^2 = 1$

$2^2 - 1^2 = 3$

$3^2 - 2^2 + 1^2 = 6$

$4^2 - 3^2 + 2^2 - 1^2 = 10$

$5^2 - 4^2 + 3^2 - 2^2 + 1^2 = 15$


We can rewrite the computed sequence with
\[
T_n = \sum_{i=0}^n (-1)^i (n-i)^2    
\]

With little help from formal computation, we get
\begin{equation}
T_n = \frac{1}{2} n (n + 1)
\label{eq:ex7_half_factor_n_n+1}    
\end{equation}

\begin{proof}
    Let us prove the formula. 

    Let $P(n)$ be the proposition that Eq. \ref{eq:ex7_half_factor_n_n+1} is correct for $T_n$.

    By recursion on $n$

    \emph{Initiation}

    For $n = 1$, $1^2 = 1$ and $\frac{1}{2} 1 \times (1+1) = 1$, so $P(n)$ is true.

    \emph{Heredity}

    Suppose $P(n)$ true, let us prove that $P(n+1)$ is also true.
    \begin{align*}
        P(n) & \Leftrightarrow T_n = \frac{1}{2} n(n+1) \\
        & \Leftrightarrow \sum_{i=0}^n (-1)^i (n-i)^2 = \frac{1}{2} n(n+1)
    \end{align*}
    \unfinished
\end{proof}

\end{myanswer}
\end{myexercise}

\begin{myexercise}{8}{19}
    (a) Prove the following theorem of Nicomachus by induction:

    $1^3 = 1$, $2^3 = 3 + 5$, $3^3 = 7 + 9 + 11$, $4^3 = 13 + 15 + 17 + 19$, etc.

    (b) Use this result to prove the remarkable formula $1^3 + 2^3 +  \dots + n^3 = (1 + 2 + \dots + n)^2$

    \begin{myanswer}

    (a) The theorem states the following: 

    For all $n \in \mathbb{N}$ we have
    \[
        n^3 = \sum_{i = 1}^n | n (n - 1) - 1 + 2i|
    \]

    \begin{proof}
        
        We proceed by recursion on $n$.
        
        Let $P(n)$ be the proposition ``$n^3 = \sum_{i = 1}^n | n (n - 1) - 1 + 2i|$''.
        
        \emph{Initiation} For $n = 1$, $1^3 = 1^2$, so $P(1)$ is true.
        
        \emph{Heredity} Suppose $P(n)$ true, let us prove that $P(n+1)$ is also true.
        \begin{align*}
            P(n) \Leftrightarrow n^3 &= \sum_{i = 1}^n n (n - 1) - 1 + 2i \\
            % &\Leftrightarrow (n+1)^3 &= \sum_{i = 1}^n  n^2 - n - 1 + 2i \\
        \end{align*}
    \end{proof}
    \unfinished

    (b)

    \begin{theorem}[Formula of Nicomachus]
        The sum of the cubes of the first $n$ natural numbers is equal to the square of the sum of the first $n$ natural numbers.
    \end{theorem}
    \begin{equation}
        \sum_{k=1}^n k^3 = \left(\sum_{k=1}^n k \right)^2
    \end{equation}

    \begin{proof}
        Let $P(n)$ be the proposition ``$\sum_{k=1}^n k^3 = \left(\sum_{k=1}^n k \right)^2$''.
        
        $P(n) \Longleftrightarrow \sum_{k=1}^n k^3 = \left(\frac{n(n+1)}{2}\right)^2$
        because $\sum_{i=1}^n i = \frac{n(n+1)}{2}$.

        We want to prove that: $\sum_{k=1}^{n+1} k^3 = \left(\frac{(n+1)(n+2)}{2}\right)^2$

        \begin{align*}
            \sum_{k=1}^{n+1} k^3 &= \frac{n^2 (n+1)^2}{2^2} + (n+1)^3 \\
            &= (n+1)^2 \left(\frac{n^2}{4} + n + 1\right) \\
            &= (n+1)^2 \cdot \frac{n^2 + 4n + 4}{4} \\
            &= \frac{(n+1)^2(n+2)^2}{2^2} \\
            & \Updownarrow \\
            & P(n+1)
        \end{align*}
        
        \emph{Conclusion}
        
        $P(1)$ is true because $1^3 = 1$ and $\left(\frac{1(1+1)}{2}\right)^2 = 1$.
        
        $P(n)$ true implies $P(n+1)$ true, so $P(n)$ is true for all $n \in \mathbb{N}_+$.
    \end{proof}
    \end{myanswer}
\end{myexercise}

\begin{myexercise}{9}{19}
    Prove by induction that if $0 < a < 1$ then $(1 - a)^n \geq 1 - na$.
    \begin{myanswer}
        We proceed by recursion on $n$.

        Let $P(n)$ be the proposition ``$(1 - a)^n \geq 1 - na$''.

        \emph{Initiation} For $n = 0$,

        $(1 - a)^n = 1 = 1 - na$, so $P(0)$ is true.

        \emph{Heredity} Suppose $P(n)$ true, let us prove that $P(n+1)$ is also true.

        We want to prove that $(1 - a)^{n+1} \geq 1 - (n+1)a$.

        \begin{align*}
            P(n) \Leftrightarrow (1-a)^n &\geq 1-na \\
            (1-a)^n (1-a) & \geq (1 - na) (1-a) \qquad \text{$1-a > 0 $} \\
            (1-a)^{n+1} & \geq  1 - na - a + na \\ 
            & \geq 1 - (n+1)a - a \\
            & \geq 1 - (n+1)a \qquad \text{because $a > 0$}
            & \Rightarrow P(n+1)
        \end{align*}

        \emph{Conclusion}
        $P(1)$ is true. 

        $P(n)$ true implies $P(n+1)$ true, so $P(n)$ is true for all $n \in \mathbb{N}_+$.
    \end{myanswer}
\end{myexercise}

% \begin{myexercise}{10}{19}
%     Prove by induction that if $n \geq 10$ then $2^n > n^3$.

%     \begin{myanswer}
%         We proceed by recursion on $n$.

%         Let $P(n)$ be the proposition ``$2^n > n^3$''.

%         \emph{Initiation} For $n = 10$,

%         $2^{10} = 1024 > 1000 = 10^3$, so $P(10)$ is true.

%         \emph{Heredity} Suppose $P(n)$ true, let us prove that $P(n+1)$ is also true.

%         We want to prove that $2^{n+1} > (n+1)^3$.

%         \begin{align*}
%             P(n) \Leftrightarrow 2^n &> n^3 \\
%             2^n \times 2 &> n^3 \times 2 \qquad \text{because $2 > 0$} \\
%             2^{n+1} &> n^3 + n^3 \\
%         \end{align*}

%         $n^3 = n^2 \times n$

%         \emph{Conclusion}
%         $P(10)$ is true. 

%         $P(n)$ true implies $P(n+1)$ true, so $P(n)$ is true for all integer $n \geq 10$.
%     \end{myanswer}
% \end{myexercise}
% Dirty false proof... To be continued

