Posts

Showing posts with the label mathematics

Pocket maths: folding halves into thirds

Image
Pt En I have folded a piece of paper in half hundreds of times in my life. And probably so did you. Folding a piece of paper in half is fairly easy: just bend the piece of paper until the corners meet, and then crease. That is it. And with this method one can also fold a piece of paper in $4$, in $8$, etc. We just have to successively divide the sections of the paper in half. But what if we wanted to fold a piece of paper into thirds, as in the picture above? Some people are good at doing that, but they don't really measure anything: they just do it approximately by looking at the paper and folding where it seems about right. I guess it goes without saying, but mathematicians don't like things to be "about right", they want them right... and even though I wasn't a mathematician, when I was a child I thought that maybe there was a way for me to successively fold different parts of the paper in half, until one of the creases would be the crease at...

Pocket maths: how to compute averages in your head

Pt En Being able to do basic arithmetic calculations in your head is a great skill. Not because it is sexy but because it is useful in your daily life: it can help you check the change you are given when shopping, it can help you know if you will have enough money to pay for your groceries, it can help you estimate how much things cost after the discounts, etc... This often reduces to being able to sum and subtract decently; sometimes you need to make a couple of small multiplications, but that is it. More likely than not, you don't need to compute averages every day. But sometimes you just want the scoring average of your team for the past few games, or the average price per person of a given meal, or the average time you spent stuck in traffic this past week... And averages may appear nastier than simply adding or subtracting, because averages also require you to perform a division: in fact, you have to add all the numbers you want and then divide the total by ho...

Introduction to the Hill Cipher

Image
Pt En The Hill cipher is a very simple cipher that works by using modular arithmetic and matrices. In a nutshell, your key is a matrix in some $\mathbb{Z}_m$ and you encrypt messages by breaking them up into pieces and then multiplying the pieces by the key matrix. That's it. I will be giving a workshop on this subject in a near future, and so I decided to write a Python notebook with a brief explanation of how the Hill cipher works, as well as providing an implementation of said cipher. The notebook can be downloaded and read here . I will be glad if you leave any suggestions/comments in the section below! A cifra de Hill é uma cifra simples que faz uso de noções de aritmética modular e de álgebra linear (mais concretamente, matrizes). Em duas frases, a cifra de Hill tem como chave uma matriz num dado $\mathbb{Z}_m$ e o modo como encripta mensagens é partindo-a em bocados com o mesmo tamanho e depois multiplicando a matriz chave por esses bocados. Num...

Building an 8-bit addition calculator with circuits

Image
Pt En I have a childhood friend that had very interesting toys... He used to play around with circuits, multimeters, LED lights, small batteries, etc. I loved going to his house and watch him play with all that. That is probably why I find circuits (in their basic form) very interesting: the current flowing and the logic gates operating on the circuits and whatnot. Because of that, in this post I will show you how to build an 8-bit addition calculator just with circuits! For that I will be using logic.ly , a circuits simulator that I can run in my browser. The best way to go about this is by starting with small components and then using a nice feature of logic.ly, which enables one to create "integrated circuits": it takes one circuit we created and transforms it into a single piece. Of course that before anything else one must know how to add two numbers in binary. It is essentially the same as in with decimal numbers, except that $1 + 1 = 10$ now. To c...

Pocket maths: good rational approximations

Pt En An obvious way of creating rational approximations for irrational numbers is by truncating its decimal expansion. For example, $3$, $3.1$ and $3.14$ are all rational approximations of $\pi $; as fractions, those approximations would be written $3$, $\frac{31}{10}$ and $\frac{314}{100} $. Notice how $\frac{314}{100}$ has $100$ as the denominator and yet only produces an approximation correct up to two decimal places. Claim: by using continued fractions one can obtain better rational approximations for irrational numbers. Method: if $x $ is an irrational number, instead of truncating its decimal expansion, we can truncate its continued fraction. Taking $\pi $ as an example, we have $$\pi = 3 + \frac1{7 + \frac1{15 + \cdots}} $$ and by taking $$\pi \approx 3 + \frac17 = \frac{22}{7} $$ we get the approximation $\pi \approx 3.14285\cdots$: it is correct up to two decimal places just as $\frac{314}{100} $, but $7$ is a much smaller denominator than $100$. (And al...

Twitter proof: the roots go hand in hand

Pt En In this twitter proof we will have a look at a rather curious, yet simple, property of real polynomials. Claim: if $p(x) = \sum_{i=0}^n a_ix^i $ is a polynomial with real coefficients, then for all complex numbers $z $, $$p(z) = 0 \iff p(\bar{z}) = 0$$ which means that the complex roots of $p(x) $ come in conjugate pairs. Twitter proof: it suffices to show that $p(z) = 0 \implies p(\bar{z}) = 0$. Assume that $p(z) = 0$ and recall that $a_i = \bar{a_i} $: $$\begin{align} p(\bar{z}) &= \sum_{i=0}^n a_i\bar{z}^i \\ &= \sum_{i=0}^n \overline{a_iz^i} \\ &= \overline{\sum_{i=0}^n a_iz^i} = \overline{p(z)} = 0 \end {align} $$ Neste post vamos dar uma olhadela a uma propriedade curiosa, mas simples, dos polinómios com coeficientes reais. Proposição: se $p(x) = \sum_{i=0}^n a_ix^i $ é um polinómio com coeficientes reais, então para qualquer número complexo $z $ vem $$p(z) = 0 \iff p(\bar{z}) = 0$$ o que significa que as raízes complexas de $p(x) $ vêm...

Twitter proof: interpolating polynomials

Pt En In this post I will show the existence of a family of polynomials that are very useful for interpolation. For that I will use what are known as Lagrange polynomials. Claim: given $n+1$ pairs $(x_i, y_i) $ with $0\leq i \leq n $ and with $x_i \neq x_j $ whenever $i\neq j $, there exists a polynomial $p(x) $ of degree at most $n $ such that $$p(x_i) = y_i,\ i = 0, \cdots, n $$ Twitter proof: consider the polynomial $$l_i(x) = \prod_{j\neq i} \frac{x - x_j}{x_i - x_j} $$ with $l_i(x_i) = 1$ and $l_i(x_j) = 0$ whenever $j \neq i$. Define $p(x) $ to be $$p(x) = \sum_{i=0}^{n} y_i l_i(x) $$ $p(x) $ has degree at most $n $ because so do the $l_i(x) $ and $p(x_k) = \sum_i y_i l_i(x_k) = y_k $. In a future post I will show the uniqueness of the polynomial satisfying the constraints in the claim. Neste post vou mostrar a existência de uma família interessante de polinómios, muito útil em interpolação. Para isso vou usar uns polinómios chamados polinómios de Lagrange...

Twitter proof: can't touch this (exponential)

Pt En In this twitter proof we will see that no polynomial grows faster than the exponential function. Claim: the ratio $\frac{x^p}{e^x}$ tends to $0$ as $x $ tends to infinity. Twitter proof: recall that by the Taylor expansion of $e^x $ we have $$e^x = \sum_{i=0}^\infty \frac{x^i}{i!}$$ and for $x > 0$ we have $$\frac{x^p}{e^x} \leq \frac{x^p}{\sum_{i=0}^{p+1} a_ix^i} \to 0 $$ where $a_{p+1} \neq 0$ thus proving our claim. The way I like to look at this is "if the exponential has a bit of every polynomial inside, then it will grow faster than any fixed polynomial $p(x)$" (because, in particular, the exponential "has a bit" of all other polynomials that have degree higher than that of $p(x) $. Neste post vamos ver que a função exponencial cresce mais depressa que qualquer função polinomial. Proposição: o rácio $\frac{x^p}{e^x}$ tende para $0$ quando $x $ tende para infinito. Prova num tweet: sabemos pela expansão de Taylor de $e...

Twitter proof: irrational high-order roots of 2

Pt En For this twitter proof we will be using a piece of mathematics straight from the 17th century. Claim: the number $\sqrt[n]{2} $ is irrational for $n \geq 3$. Twitter proof: suppose that $n \geq 3$ and $\sqrt[n]{2}$ is rational, i.e. $\sqrt[n]{2} = \frac{a}{b}$ for some integers $a, b $. Then taking the $n $-th power of both sides we get $2 = \frac{a^n}{b^n} \iff b^n + b^n = a^n $, contradicting the well-known Fermat's Last Theorem . Para esta prova num tweet vamos usar um pedaço de matemática do século 17. Proposição: o número $\sqrt[n]{2} $ é irracional para $n \geq 3$. Prova num tweet: suponhamos que $n\geq 3$ e que $\sqrt[n]{2} $ é racional, i.e. $\sqrt[n]{2} = \frac{a}{b} $ para alguns inteiros $a, b $. Se for esse o caso, elevando os dois lados da igualdade a $n $, obtemos $2 = \frac{a^n}{b^n} \iff b^n + b^n = a^n $, contrariando o famoso Último Teorema de Fermat . &nbsp&nbsp- RGS join the mathspp mailing list

Twitter proof: the sum of inverses diverges

Pt En In this post I will share with you my favourite proof that the series of the inverses diverges: $\sum_{i=1}^\infty \frac1i = \infty $. Claim : the series $\sum_i \frac1i$ diverges. Twitter proof : consider the series $$ \begin{align} &\frac12 + \frac12 + \frac12 + \cdots = \\ &\frac12 + 2 \times\frac14 + 4\times \frac18 + \cdots = \\ &\frac12 + \frac14 + \frac14 + \frac18 + \cdots \leq \\ &\frac12 + \frac13 + \frac14 + \frac15 + \cdots \end{align}$$ that clearly diverges because it is a series of a constant nonzero term. By the comparison test, the series of the inverses also diverges. Comment with your favourite way to prove this fact!! Neste post quero partilhar com todos a minha prova preferida de que a série dos inversos dos naturais diverge: $\sum_{i=1}^\infty \frac1i = \infty $. Proposição : a série $\sum_i \frac1i$ diverge. Prova num tweet : considere-se a série$$ \begin{align} &\frac12 + \frac12 + \frac12 + \cdots = \\ &...

Twitter proof: the Tower of Hanoi

Image
Pt En In this post we prove what the minimum number of moves to solve the problem of the Tower of Hanoi is! Claim: let $T(n)$ denote the number of moves it takes to solve the Tower of Hanoi with $n$ disks; then $T(n) = 2^{n}-1$. Twitter proof: note that to solve the problem with $n$ disks, we first have to move the top $n-1$ disks to one of the two poles, move the bottom disk (the bigger one) to the remaining pole, and then move the top $n-1$ disks to the top of the bigger disk. Each time we move the top $n-1$ disks to another pole we must take, at least, $T(n-1)$ moves (by definition of $T$) hence we clearly have $T(n) = 2T(n-1) + 1$. Just notice that $B(n) = 2^n - 1$ satisfies the recurrence relation and that $T(0) = B(0) = 0$. If you are having trouble understanding what I mean by to solve the problem with $n$ disks, we first have to move the top $n-1$ disks to one of the two poles, move the bottom disk (the bigger one) to the remaining pole, and then move t...

Markov Decision Processes 03: optimal policy variation

Image
Pt En This post is going to follow up what was discussed here , and I will show you how changing the value of the discount factor may affect what the optimal policy is. For that, consider the MDP defined below: This MDP is inspired in the example I have been using; I added an extra state and stripped the MDP of the majority of the transitions so we can focus on what is essential here. From the MDP above there is a clear contender for the title of optimal policy: $$\begin{align} &\pi(H) = E\\ &\pi(T) = D\\ &\pi(TU) = D\end{align}$$ Assume that when we enter the MDP there is a $50\%$ chance we start Hungry and a $50\%$ chance we start Thirsty. If we use the policy $\pi$ then the expected reward is $$E[R | \pi] = 0.5E[R_T | \pi] + 0.5E[R_H | \pi]$$ where $E[R_s | \pi]$ is the expected reward we get by following policy $\pi$ starting from state $s$. From my previous post we know that $E[R_T | \pi] = E[R_H | \pi] = \frac{1}{1-\gamma}$ and hence $$E[R | \pi] ...

Markov Decision Processes 02: how the discount factor works

Image
Pt En In this previous post I defined a Markov Decision Process and explained all of its components; now, we will be exploring what the discount factor $\gamma$ really is and how it influences the MDP. Let us start with the complete example of last post: In this MDP the states are Hungry and Thirsty (which we will represent with $H$ and $T$) and the actions are Eat and Drink (which we will represent with $E$ and $D$). The transition probabilities are specified by the numbers on top of the arrows. In the previous post we put forward that the best policy for this MDP was defined as $$\begin{cases} \pi(H) = E\\ \pi(T) = D\end{cases}$$ but I didn't really prove that. I will do that in a second, but first what are all the other possible policies? Well, recall that the policy $\pi$ is the "best strategy" to be followed, and $\pi$ is formally seen as a function from the states to the actions, i.e. $\pi: S \to A$. Because of that, we must know what $\pi(H)...