Posts

Random Walks to solve Diophantine Equations

Image
Random walks (check my older post here ) and Diophantine equations are two simple mathematical beasts. After a college seminar, I tried putting them together to make something neat, and came up with this: just pick a Diophantine equation, simulate a random walk, and try to see if the random walk went over any solutions! In this report I briefly go over how I came up with this, and show the code I wrote/some results. The Matlab code is all in here (I also have some Python code about random walks in general here , check my post !). In the image above, the blue line represents a path taken by the random walk and in orange/red the nodes tell us how far we are from a solution (the smaller the node, the closer we are). Note that this notion of proximity isn't given by actually computing the distance to a known solution, but just by looking at how similar the two sides of the equation are. The image below shows the evolution of three independent random walks trying to find an example ...

Two-dimensional random walk simulations

Image
Think of a drunk man that continuously tumbles left and right, back and forth, with no final destination. That can be thought of as a random walk on the plane... Now imagine that the drunk man has a shorter leg and tumbles more to one of the sides: that is a biased random walk. Now imagine the drunk man can teleport to a nearby location. That is (kind of) a Lèvy flight ! All those are quite interesting to observe in motion and I implemented them in Python. (the code can be found here ). From those, only the tilted walks and the one with the trail are such that the particle's movement wraps around the borders (leaving the green area makes the particle appear in the opposite side, just like the snake from the game Snake). The image above is a screenshot of the execution of the random walk that leaves a coloured trail. I also implemented an animation where the screen gets progressively filled with circles of different colours, as if being splattered with paint. Not sure why... But it...

Egyptian multiplication with Haskell

Very recently a saw a Youtube video from a friend, MathGurl , in which she explained the ancient Egyptian multiplication method. At the same time, and for no particular reason, I remembered Haskell, so I decided to implement the method. It was not a big feat of programming, but I did enjoy relearning the basics of Haskell I once knew. You can find the file with the implementation here. (The implementation only works for non-negative integers) The method is quite simple and works because of the binary expansion of a number. Basically, if you want to calculate a * b , either b is even or odd. If b is even, just cut b in half and duplicate a to compute (2a)(b/2) . If b is odd, then ab = a + (2a)*((b-1)/2) . Another way of thinking about this is by writing b in the form b = 2^(k_1) + 2^(k_2) + ... + 2^(k_n) and then having ab = a( 2^(k_1) + 2^(k_2) + ... + 2^(k_n)) = a2^(k_1) + a2^(k_2) + ... + a2^(k_n) . My formulation is just the recursive way of writing it. If I get the courage t...

Fractals and the Filled Julia set

Image
The Filled Julia set is another fractal that can be built with the complex numbers, just like the Mandelbrot set. To build a Filled Julia set, we pick a complex number c and then repeatedly apply f(z) = z^2 + c to every point of the plane. If that iterative application of f produces a sequence that goes to infinity, the point does not belong to the Filled Julia set. If it does not go to infinity, then the point belongs to the Filled Julia set and is coloured black. Using Python and pygame I developed a small script that allows the user to click a point on the complex plane and create its Filled Julia set. The code, which is very simple, can be found here . A Windows executable can be downloaded from here . Pressing the left arrow shows the previous Filled Julia sets you created, while the right arrow takes you to the more recent ones. Pressing the spacebar erases the current fractal and shows the initial black axis. [Pt] O 'Filled Julia set' é outro fractal que pode ser def...

On recursive functions and Kleene's approach

In Computation Theory it is of interest to study properties about functions, and what functions satisfy said properties. We then consider the set of all functions that satisfy those properties. One of those sets is the set of recursive functions R as defined by Kleene. To define R, Kleene gives a series of different primitive functions that are known to be in R, and then defines some operations that preserve functions in R. For the purposes of what I will be sharing next, I will just enumerate said primitive functions and constructions, so that the reader is aware of what will be used (notice that what comes below is almost identical to what you can see here ). The primitive functions are: The constant functions of arity 0, one for each natural number; The zero function of arity 1, that always returns 0; The successor function of arity 1, that sends x to x+1 ; The projection{a,b} of arity a, that returns the b-th argument unchanged. For example, projection{3,2}(a,b,c) = b . After tha...

Creating a programming language from scratch!

Image
Python is the programming language I am more comfortable with, by far; at an early stage of my learning I heard that Python was built on top of C. Whatever that meant. Then I started thinking of it in this way: programming languages are used to build programs. Someone used C to create another program, that can be used to create even more programs! That seemed reasonable. At some point in time I got interested in compilers and interpreters and all those weird things, and I came across this wonderful blog post. I immediately started following the "Let's Build a Simple Interpreter" series to write my own Pascal interpreter with Python, which is the aim of that series. At that time there were only 5 or 6 posts, and I got to the point where I had done everything he told us to do and couldn't wait for the other posts, as I noticed the frequency with which the author posted in that series was rather low. At that point, my project's aim diverged of that of the blog and I...

Minesweeper remake

Image
Minesweeper has to be one of the most well-known games ever. You start with a fully covered grid and your aim is to flag all mines that are hidden. Upon pressing a cell, if it didn't have a mine, you will be told how many mines there are adjacent to that cell you uncovered. For this game, cells that share only a corner are also considered adjacent: any cell that is not on the border of the frame has 8 adjacent cells; one for each side and one for each corner. The controls are similar to those of the original game. Left-click to uncover a cell and right-click to (un)flag it. You win if you flag all the mines and uncover all the other cells. You lose if you uncover a mine. The code was written in Python3 and made use of pygame. The source code and the artwork can be found here ; a windows executable can be found here . [Pt] O minesweeper é um jogo muito icónico dos dias de hoje, com instruções fáceis, mas por vezes não tão fácil de jogar quanto isso! O código fonte e os gráficos nece...