Posts

Showing posts with the label algorithms

Pledging to do 100 days of Machine Learning and progress log!

Image
Pt En After watching this video from Siraj Raval, I decided to jump right on board of the #100daysofMLcode initiative! (even though I am something like 73 days late...) The goal here is to devote (at least) 1h every day, for the next 100 days, to studying ML or writing code! According to the rules posted by Siraj, I must: Make a public pledge for this, which this post is; Make a log of everything, which this post will also be; Whenever I see something related to this #100DaysofMLCode, be supportive! Progress log For the day $0$ I wrote this post and spent quite some time thinking about what I will do throughout. I am thinking of studying several topics about ML and then writing educative posts here, for the blog. For today I wrote this twitter proof , tackling a mathematical property of neural networks with linear activation functions. Started reading about Reinforcement Learning and Markov Decision Processes; already imagined a nice example I will be using when writing about th...

Random maze generation

Image
Pt En In this post I just want to share a simple algorithm that I used to create random mazes. The idea came from an e-mail I got, about a past competition, where one of the contestants did this exact thing: a program to generate random mazes. I saw the animation of the program working here and I deduced how to do it. All the code can be found on GitHub , as well as an executable of the program, the animations from the beginning and end of this post, an image of a bigger maze, and this other animation: where you can see a different style of maze; a less straight one. The maze starts in the top left red corner and ends wherever the other red square is, which need not be on the bottom right corner. The algorithm is simple: travel randomly inside the black area without ever hitting a white path; whenever no random move can be made, start going back until you find a place where the path can branch out again. While we are creating white paths, keep updating the final position to be ...

How to compute any square root by hand

Image
Pt En Num post anterior mostrei como podemos aproximar a raíz quadrada de um número através de um processo iterativo que começa com um palpite, seguido de vários ajustes. Neste post vou mostrar qual é o algoritmo mencionado pela Mathgurl no vídeo que ela fez em "parceria" comigo. O método que vou descrever pode ser usado com qualquer número real, seja quadrado perfeito ou não, seja inteiro ou não, racional ou não. Vou começar por apresentar um raciocínio que mostra como o algoritmo surge. Para quem não estiver interessado, pode saltar diretamente para a explicação final de como funciona . Para a exposição que se segue, se $a,b$ forem dígitos, então a notação $ab$ representa o número $10a + b$ em vez do número $a\times b$. Começamos por notar que, se quisermos descobrir $\sqrt{N}$ à mão e $\sqrt{N}$ for irracional, então vamos ter de nos contentar com uma aproximação com um número finito de casas decimais. Por outro lado, se $\sqrt{N} = a_0a_1\cdots a_n.b_0\cdots b_m$, e...

The naturals, the rationals and sets that fit inside themselves

Image
Pt En If I have two groups of kids in front of me, for example two different classes, how can I decide if the two groups have the same size? Of course I can count both groups, but I can also ask every kid from group $A$ to give his hand to a kid from group $B$, so they pair up. If, in the end, everyone is paired up, both groups have the same size. If some kid from group $A$ doesn't manage to give his hand to anybody, because every kid from group $B$ is taken, then the group $A$ had more kids... and if some kid from group $B$ doesn't get the hand from any kid from group $A$, then group $B$ had more kids! When we want to compare the sizes of two sets this is one of the things we can do! Instead of counting the two sets, we can try to pair them up. If we manage to do that, the sets have the same size! Sometimes pairing two sets is a hard task and instead we opt for a different thing: remember that if $a \leq b$ and $b \leq a$ then $a=b$; hence, if a set $A$ is not smaller than...

Problem #06 - stacks of beans

Image
(english version below ) O problema deste post foi-me colocado na primeira edição das Jornadas de Matemática da Faculdade de Ciências da Universidade do Porto, numa sessão de jogos matemáticos ! O problema é particularmente engraçado porque assenta num jogo que se pode jogar entre duas pessoas. Problema: suponha-se que sobre uma mesa estão dois montes de feijões, um com 19 feijões e outro com 20 feijões. A Ana e o João vão jogar um jogo com esses montes de feijões: cada jogada consiste em retirar $2N$ feijões de um monte e pôr  $N $ feijões no outro monte. Assim, na primeira jogada podemos, por exemplo, tirar 10 feijões do monte com 19, deixando-o com 9, e pôr 5 feijões no monte com 20, deixando-o com 25. A Ana vai ser a primeira a jogar e perde quem não puder fazer nenhuma jogada válida. Será que algum dos jogadores consegue garantir a sua vitória? Este jogo tem regras muito simples e é bastante engraçado, vale a pena jogá-lo com alguém só para entender realmente como funciona a d...

Water buckets and infinite tap water

Image
[ Link to code, you can try it in the end of the post] Suppose you have an infinite source of water and two buckets of capacities $5$L and $3$L. Through juggling around the water in the buckets, can you find a way to have one of the buckets hold exactly $1$L of water? A possible way of doing it would be: Fill the bucket of $3$L and then pour its water into the $5$L one; Fill the bucket of $3$L. At this point you have $3$L in each bucket; Pour the bucket of $3$L into the $5$L one. Since the bigger one already had $3$L in it, only $2$ will fit, meaning there will be a remaining litre in the $3$L bucket. I find this problem a very interesting one, and it is not hard to generalize it: given $N$ buckets of capacities $c_1, c_2, \cdots, c_N$, as well as a target value $T$ and an infinite source of water, is there a sequence of moves that puts exactly $T$ litres in one of the buckets? There are some cases for which one can immediately say that there is no such sequence. On one hand, if $T ...