Empty Square Sequence (Code Challenge)

  • Page Owner: Not Set
  • Last Reviewed: 2019-10-18

Happy 10th code challenge!

The Empty Square Sequence

In the image below, squares are either empty or filled with a circle.

Steps vs Empty Squarest

Square set

Create a function that takes a number step (which equals HALF the width of a square) and returns the amount of empty squares. The image shows the squares with step 1, 2 and 3. The return value is the number of cells not on a diagonal, which is 0 for the first square, 8 for the second, and 24 for the third.

Examples

emptySq(1) ➞ 0

emptySq(3) ➞ 24

emptySq(10) ➞ 360

Notes

Test input will always be a positive integer. The width of the square will always be even.

Tests

Test = (x, y) => x == y ? console.log("Pass") || true : console.log("Fail") || false;

emptySq = x=>x;

Test(emptySq(3), 24)
Test(emptySq(1), 0)
Test(emptySq(27), 2808)
Test(emptySq(3), 24)
Test(emptySq(0), 0)
Test(emptySq(10), 360)
Test(emptySq(19), 1368)
Test(emptySq(60), 14160)
Test(emptySq(7), 168)

Additional Posts

Here's mine in Rust:

! fn empty_square(w:i32) -> i32 { 4 * (w.pow(2) - w) }

The key insight for me was to not think of it as a square, but as four quadrants.