Pascal's colorful triangle
Let's consider the Pascal's triangle - a triangle made of numbers, bordered by the number 1 on the top as well as its left and right walls. In between are other numbers, with the largest one being in the center bottom part of the triangle, symmetrically surrounded by other smaller numbers. Any given number in the triangle is the sum of the two numbers directly above it. For example,
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
...and so on
This concept has implications in mathematics. Some of which that I'm familiar with are:
- The numbers closely relate to binomial coefficients. The entry in the nth row and the kth column is got by n(C)k which when solved for gives the number of ways to choose k elements from a set of n elements. So, it's applied in probability.
- Deriving from the first point, it is a quick and efficient way of calculating combinations and can be used in dynamic programming algorithms to compute and store binomial coefficients.
- The numbers are closely related to the fibonacci sequence as well.
Fascinating stuff! But what if the numbers are replaced with colors?
I crafted this little project where I use PyGame to render a Pascal triangle as a collection of RGB colored tiles.
After around ~2 hours of studying the PyGame docs, coding, experimenting with different color schemes and tile sizes, some more beautiful outcomes came up on the screen. They're uploaded on the github repo*.
The first three tiles are made white. If they're given a color, the outcome will mostly have mixtures of those colors in the subsequent rows. The triangle starts with low RGB values on the tip, left and right edges and then they add up to form brighter and brighter colors on the bottom.
If you've come this far, might as well kill some more time and clone the repo. See what you can come up with!. All you need is PyGame and some ideas to tinker with the code.
So, math is an art form. Where there's math, there's opportunity for art.
With just a little bit of lazy coding, we are so gifted with the power of applying this art form and getting lost in admiration.
Cheers!
*Code for this project was written in November 2022