\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

\subsection{Numbers, Powers and Logarithms}

\begin{myexercise}{1}{25}
	What is the smallest positive rational number?
	
	\begin{myanswer}
		The smallest positive rational number is 0.
	\end{myanswer}
\end{myexercise}


\begin{myexercise}{2}{25}
	Is $1 + 0.23999999999...$ a decimal expansion?
	
	\begin{myanswer}
		0.23999999999... is not a decimal number as it has an infinite decimal expansion.

		The decimal expansion of $1 + 0.23999999999...$ is in the form
		\[
		1 \cdot 10^0 + 2 \cdot 10^{-1} + 3 \cdot 10^{-2} + 9 \cdot 10^{-3} + \sum_{i=1}^{\infty} 9 \cdot 10^{-3-i}	
		\]

		\textit{Nota bene}: This surely does not answer the question...
	\end{myanswer}
\end{myexercise}


\begin{myexercise}{3}{25}
What is $(-3)^{-3}$?

\begin{myanswer}
	\[
	(-3)^{-3} = \frac{1}{(-3)^3} = \frac{1}{(-3)(-3)(-3)} = -\frac{1}{27}
	\]
\end{myanswer} 

\end{myexercise}

\begin{myexercise}{4}{25}
	What is $(0.125)^{-2/3}$?

	\begin{equation}
		b^{p/q} = \sqrt[q]{b^p}
	\end{equation}

	\begin{myanswer}
		\begin{align*}			
			0.125^{-2/3} &= \left(\frac{1}{8}\right)^{-2/3} \\
			&= 8^{2/3} \\
			&= \sqrt[3]{8^2} \\
			&= \sqrt[3]{64} \\
			&= 4
		\end{align*}
	\end{myanswer}
\end{myexercise}

\begin{myexercise}{5}{25}

Defining real numbers in terms of binary expansion.	

\begin{myanswer}
	\[
	x = \sum_{i=0}^{\infty} a_i \cdot 2^{-i}	
	\] 
\end{myanswer}
\end{myexercise}

\begin{myexercise}{6}{25}
	Let $x = m + 0.d_1d_2\dots$ and $y = n + 0.e_1e_2\dots$.
	Give a rule for determining whether $x = y$, $x < y$, $x > y$ based on the decimal representation.

	\begin{myanswer}
		\begin{algorithm}[H]
			\begin{algorithmic}[1]
				\Procedure {LowerGreaterEqual} {$X$, $Y$}
				\Comment{$X$ and $Y$ are arrays of decimal of numbers $x$ and $y$ respectively.}
				\State $i \gets 0$
				\While {$i < X.length$ and $i < Y.length$}
					\If{$X[i] == Y[i]$}
						\State $i \gets i+1$
					\ElsIf {$X[i] > Y[i]$}
						\State \Return \texttt{GREATER}
					\Else
						\State \Return \texttt{LOWER}
					\EndIf
				\EndWhile
				\If{$X.length > Y.length$}
				\State \Return \texttt{GREATER}
				\ElsIf{$X.length < Y.length$} 
				\State \Return \texttt{LOWER}
				\Else 
				\State \Return \texttt{EQUALS}
				\EndIf
				\EndProcedure
			\end{algorithmic}
			\caption{Greater, Lower or Equal Decimal representation}
		\end{algorithm}
	\end{myanswer}
\end{myexercise}

\begin{myexercise}{7}{25}
	Given that $x$ and $y$ are integers. \textit{Prove} the laws of exponents, from definition given by Eq. \ref{eq:rule-of-exponents-2} from Eq. \ref{eq:rule-of-exponents}.

	\begin{eqnarray}
		b^0 = 1, & b^n = b^{n-1} b \quad \text{ if $n > 0$}, & b^n = b^{n+1} / b \quad \text{ if $n < 0$}. 
		\label{eq:rule-of-exponents}
	\end{eqnarray}

	\begin{eqnarray}
		b^{x+y} = b^x b^y, & (b^x)^y = b^{xy},
		\label{eq:rule-of-exponents-2}
	\end{eqnarray}

	\begin{myanswer}
		We will prove this in $\Reals$
		Let us recall the following formula:
		\begin{equation}
			b^x = e^{x \ln b}
		\end{equation}
		and
		\begin{equation}
			\exp(x + y) = \exp(x) \exp(y)
		\end{equation}

		Thus it comes:
		\begin{align*}
			b^{x + y} &= e^{(x + y) \ln b}  \\
			&= e^{x \ln b + y \ln b} \\
			&= e^{x \ln b} e^{y \ln b} \\
			&= b^x b^y
		\end{align*}

		Similarly for $(a^x)^y$:
		\begin{align*}
			(a^x)^y &= e^{y \ln (a^x)} \\
			&= e^{xy \ln a} \\
			&= a^{xy}
		\end{align*}
	\end{myanswer}
\end{myexercise}

