Saturday, July 28, 2012

How many squares on a chess board?

Hello friends!

I'm a bored nerd that loves really elementary maths. M4TH$ B1TC4ES! Yeah that's right. I used the letter four for two different letters. Wait, holy shit four is a number. Now I've got you second guessing yourself!

Anyways. I was bored tonight, lurking through facebook pictures of friends when I found this gem. 


The goal with this puppy is to count the squares in this picture. How many do you count? There are few different ways to do this. If you're a normal human being, you just.. .fucking count them. Easy peasy. Start with the single squares, then double it up for 2x2s, 3x3s, 4x4s for all larger boxes. They add up pretty quickly so it's only really practical do spend any amount of time on the smaller  puzzles. If you get to grids that 5x5,6x6, are those still countable? What about a chess board? How many squares are on that? It was a pretty easy problem to solve for squares in Calc (the OpenOffice version of Excel).

The logic is as follows:

The size of the square limits the size of the smaller squares (obvious, I know but bare with me). Therefor the squares in a grid with nxn length go from 1x1, 2x2, 3x3 ... nxn. And there is always only 1 nxn square in an nxn grid. Let's look at the chess board example. There is 1 8x8 square, and there is room for 2 7x7 squares horizontally and 2 7x7 squares vertically:




Or if we write it out:
Number of squares side-to-side: (8-7+1)
Number of squares up-and-down: (8-7+1)
Therefor the total number of 7x7 squares in an 8x8 grid is: (8-7+1)x(8-7+1) 

OR

(Length of side - size of square of interest + 1)^2

Since we desire all of the squares it is necessary to repeat this for each combination of square.

1x1: (8-1+1)^2 = 64 = 8^2
2x2: (8-2+1)^2 = 49 = 7^2
3x3: (8-3+1)^2 = 36 = 6^2
4x4: (8-4+1)^2 = 25 = 5^2
5x5: (8-5+1)^2 = 16 = 4^2
6x6: (8-6+1)^2 = 9 = 3^2
7x7: (8-7+1)^2 = 4 = 2^2
8x8: (8-8+1)^2 = 1 = 1^2

As it turns out it's just a simply sum of squares problem. So for any size square you can just use the equation for the sum (Sn) of squares, for n squares.

Sn = n(n+1)(2n+1)/6
See? Easy peasy, lemon squeezy. You can check out my Calc worksheet (don't worry it's excel compatible) here. So what about rectangles? Identical logic. Using our trusty formula for a square:

(Length of side - size of square of interest + 1)^2

And making it rectangley:

(Length of side - size of square of interest + 1) x (Length of other side - size of square of interest + 1)

Let's look at a 5x3 rectangle:

The largest square this can contain is 3x3, so it can really hold some 3x3s, 2x2s, and 1x1s. So looking at our formula we'll have:

3x3: (5-3+1)(3-3+1)
2x2: (5-2+1)(3-2+1)
1x1: (5-1+1)(3-1+1)
OR
In an NxM rectangle (where N is the longer side)


which reduces to



And voila. Notice that if N=M we get our original square formula? I'm not sure how to runs loops in OpenOffice Calc (I know, shut up, I don't have Excel) so I made the loop in my trusty TI-83 with the following code:

:Prompt N, M                N, for long side or rectangle, M for short.
:0-->S                           S, running total, or sum
:For(I,0,M-1,1)              I, the counter, starting at 0.
:(N-(M-I)+1)*(I+1)+S-->S
:End
:Disp S

It's been delicious,

-Rois