← Back to challenges

How Many Rectangles?

JavaScriptHardalgorithmsmathnumbers

Instructions

Let there be a square matrix, where each square is a rectangle, and a combination of more squares are also rectangles. To find the number of rectangles, Pete sat down and started counting... but that's highly inefficient.

Create a function that takes the order of the matrix as input and returns the number of rectangles in them.

Examples

rectangles(1) ➞ 1

rectangles(2) ➞ 9

rectangles(3) ➞ 36

Notes

  • The input will always be an integer.
  • Number of rectangles are given by: ((n(n+1))/2)^2
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Calculate the Median