Find what you're looking for

Saturday, 20 July 2013

Triangles and Squares.

Deviating momentarily from my Colours series for a bit of a maths/nerdy post here.

A while ago, I was wondering about how to calculate how many squares of any size were in a square grid. Take, for example, this one:



That's obviously a 4 by 4 square grid. So you would need to find all the 1x1 squares (which is obviously 16), but also all the 2x2, 3x3, up to 4x4 (just 1).
I actually figured this out when I was sitting on the toilet, as you do. Because of the bathroom tiles. (We have square white tiles in the bathroom.) I figured out that I could do it by working from the outside in - first counting all the spaces, then the grid points, and repeating that until I got into the middle - each time I had done it, disregarding the outer ring.
So, for the grid above, there are 16 spaces; then 9 intersecting grid points; then 4 spaces, removing the outside ring; then 1 grid point, again removing the next ring.
I quickly realised that this pattern (16 + 9 + 4 + 1 = 30) was a sum of squares, up to the length/width of the square itself.
I knew that that could be expressed as x^2 + (x-1)^2 + (x-2)^2 + ........ + 2^2 + 1^2. But it was tricky trying to get it down any further. Thankfully, after a bit of hunting around, I was able to find this formula: (x(x+1)(2x+1))/6, which works perfectly. And, it means you don't even need to see the grid - you just need to know that it's a perfect square, and what the length of it is in the unit squares.

Then, however, I wanted to move on to triangle "grids", as such. Which proved to be a lot harder. Take, for instance, this one:

I was able to add up the numbers in a similar way as I did the squares - except, it had three different things to count; top-pointing, bottom-pointing and then the intersecting points (so this one, for example, would be 16 + 6 + 3 + 1 = 26) - but the numbers weren't in a similar series to before. The square seemed to always be there at the beginning (the triangle above has a side length of four, for example; you only count the top-pointing triangles), but then it was a different series that I didn't quite recognise. It was, however, consistent. So I looked at it a bit closer. This is the series: 0, 1, 3, 6, 10, 15, 21.... I shortly realised that the difference was simply increasing by one each time. However, what each next triangle had wasn't just the next number in the series - it was all the previous ones as well. So the fourth triangle wasn't just the fourth square (16) plus the fourth in this series (6), but the first four in it (0, 1, 3, 6). Which was a bit harder to try and figure out a way to write down. So I tried adding them together, to see if that created a new series. I got: 0, 1, 4, 10, 20, 35, 56.... Which was better. So now, I could express the sum of the triangles as: x^2 + x(N), N being that series. But I wanted to do better than that. So I looked the series up, and found that they were tetrahedral numbers. And could be generated with the formula: (n(n+1)(n+2))/6, which is quite similar to the previous one I had for the sum of squares. However, this generator started the series at 1. I needed to start at 0. Which meant that n = x - 1, which I substituted in to the equation:
= ((x-1)((x-1)+1)((x-1)+2))/6
= ((x-1)(x)(x+1))/6
= ((x^2-x)(x+1))/6
= (x^3 + x^2 - x^2 - x)/6
= (x^3 - x)/6
...Which actually works. And is a surprisingly simple solution.

So, the equation for the calculation of all the squares of any size in a square grid with the number of unit squares along the side being given as x is equal to:
(x(x + 1)(2x + 1)) / 6

And the equation for the calculation of all the triangles of any size in a triangle "grid" (if you have a better word, let me know) with the number of unit triangles along the side being given as x is equal to:
x^2 + ((x^3 - x)/6)

I'm also planning to figure out how to do this equation with sections of the square or triangle missing - so they are rectangles or trapeziums. And you are given the ratio of one side to the other. Seems like there should be a way to do that.

No comments:

Post a Comment

Please, tell me what you think. I'm not psychic, and I want to know :)