Posts

N-body simulation in 2D

October 16, 2022

A few days ago I implemented a n-body simulation in Python, which is used to generate the following animation: Vectorized n-body implementation # It uses a vectorized form of calculating the acceleration matrix, which is used to update the positions of the bodies. The velocity is integrated with leapfrog integration, which unlike Euler integration, is stable for oscillatory motion. It is pretty efficient. The acceleration matrix is calculated for 2D, but is easily extended to work in 3D as well. ...

Ordered fractions

August 10, 2022

In this post we will look at solving Project Euler’s problem #71, which is about ordered fractions. I have been puzzling with pen and paper for a while during this problem, and researching about fractions really paid off big time for this problem. The final solution is pretty elegant in my opinion. Let’s get into it! Problem statement # Consider the fraction $n / d$, where $n$ and $d$ are positive integers. ...

Counting rectangles

July 6, 2022

In this post we will work out a solution to the Project Euler problem #85 where we have to count rectangles. Problem statement By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles. Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the nearest solution. Naïve solution As usual, I always try to define a naïve implementation first, and build from there. ...

Path sum: two ways

June 10, 2022

In this post we will look at solving problem 81 on Project Euler. This problem is about finding a minimal path sum in a matrix. There are three variants of this problem, and this is the easiest one. Problem statement # In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only moving to the right and down, is indicated in bold red and is equal to 2427. ...

Plotting trees recursively

May 18, 2022

The idea for this visualization is something I have been thinking about for quite some time now. Although it is not this specific visualization per se, but the idea of visualizing a nested tree in an appealing way. This method is actually quite easy to implement, and HTML and CSS are doing all the heavy lifting of displaying something that actually looks nice. The illustration below is an example of what the final result will look like, and the rest of this post is about developing the visualization. ...

Sqrt(x)

May 12, 2022

I came across a fun exercise on LeetCode, where you have to calculate $f(x) = \sqrt{x}$, without using the standard library functions. Wikipedia has plenty of implementations to solve this problem efficiently, but I figured it would be a fun excercise to come up with a solution of my own. Binary search algorithm My approach was to determine an upper and a lower bound, and do a binary search to find the solution. ...

Gambler's Fallacy

March 27, 2022

Suppose we are throwing coins and bet if it is going to be head ($H$) or tails ($T$). After throwing the coin three times, it resulted in heads all of the throws. Do you think that the fourth throw will be heads, or will it be tails?  Most people would assume that it is not likely to throw heads three times in a row, and it is even more unlikely that it would be four times. ...

Totient maximum

March 26, 2022

In this post we will look at solving problem 69 on Project Euler. This problem is about using Euler’s totient function to find a given maximum. Without any further introduction, let’s dive into it. Problem statement # Euler’s Totient function, $\varphi(n)$, is used to determine the number of numbers less than $n$ which are relatively prime to $n$. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine, and relatively prime to nine, $\varphi(9) = 6$. ...

Monopoly odds

February 28, 2021

Introduction # A few years ago I have written a detailed analysis of the probabilities in Monopoly. However, because of handwaving many rules to make it easier to analyze, I wanted to redo it and use all the rules this time. The main motivation for doing this is to solve a question on Project Euler. While we are at it, I also wanted to take the time to answer a few different questions as well. ...

Dungeon generation with Newton's law of gravity

February 3, 2021

You might wonder what Newton's law of gravitation has to do with the generation of dungeons, but if we use this concept we can use it to generate a dungeon of rooms. Instead of using something like a BSP algorithm, my idea was to just throw a bunch of rooms on top of each other, and let them push each other away until there are no longer any collisions. The result looks like this. ...


© 2022 Lars Rotgers
All rights reserved