An n x n grid will have a total of n2 + (n - 1)2 + ... + 22 + 12 squares. The picture has one 4 x 4 square and two 2 x 2 squares. There are 42 + 32 + 22 + 12 = 16 + 9 + 4 + 1 = 30 squares in the 4 x 4, and there are 22 + 12 = 4 + 1 = 5 squares in each 2 x 2. Thus, there are a total of 30 + 2 * 5 = 40 squares.

An n x n grid will have a total of n2 + (n - 1)2 + ... + 22 + 12 squares. The picture has one 4 x 4 square and two 2 x 2 squares. There are 42 + 32 + 22 + 12 = 16 + 9 + 4 + 1 = 30 squares in the 4 x 4, and there are 22 + 12 = 4 + 1 = 5 squares in each 2 x 2. Thus, there are a total of 30 + 2 * 5 = 40 squares.

Do you mean n^2 instead of n2? Not sure what formula you're using. But I'm assuming those are meant to be powers instead of multipliers, right?

The formula for the larger squares that makes sense to me is f(x) = (x(x+1)(2x+1))/6 where x = 4. f(4) gives you (4 * 5 * 9)/6 = 30 total squares for the larger ones. Then I'd add the 10 total combinations of the smaller squares. I think we're basically doing the same thing to arrive at 40 though.