Here's an approach one can use to derive a formula.
The shapes always have a horizontal line across the middle. If you draw a rectangle that emcompasses both the top square and the horizontal line, there will always be a void of white squares within it large enough to be partially filled by the squares below the line.
Imagine that you fill that void above the line with the squares below the line. With the exception of n=1, your shape will be changed to a rectangle that still has some white squares in it. Let's look at a few.
n=2 n=3 n=4
. X . X X . . . X . . X X X . . . . . X . . . X X X X . . .
X X X X X X . X X X . X X X X X . . X X X . . X X X X X X X
. X . . . . X X X X X X X X X X . X X X X X . X X X X X X X
. X X X . . . . . . X X X X X X X X X X X X X X
. . X . . . . . . . . X X X X X . . . . . . . .
. . X X X . . . . . . . . .
. . . X . . . . . . . . . .
The new shape can be characterized with the formula: area = height * width - gap
If we chart that out to look for patterns, it looks like this:
n | height | width | gap
1 | 1 | 1 | 0
2 | 2 | 3 | 1
3 | 3 | 5 | 2
4 | 4 | 7 | 3
Both height and gap are counting by one, and width is skip-counting by 2. You can always characterize that linear trend as n*skipValue +/- constant
. In this case,
height=n
width=2n-1
gap=n-1
Plugging those terms back into our formula for the area of the gapped rectangles,
area = height * width - gap
becomes area = n * (2n - 1) - (n - 1)
shapeArea(1000)
already fails on my machine.