Facebook
From Jittery Marmoset, 3 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 76
  1. const getSquareFormations = (squareCount) =>
  2. {
  3.     const squareFormations = []
  4.  
  5.     for(let row = 1; row < (squareCount+1); row++)
  6.     {
  7.         for(let column = 1; column < (squareCount+1); column++)
  8.         {
  9.             if (row*column >= squareCount) //the formation can fit the squares
  10.             {
  11.                 if (row === squareCount && column>1) //all squares are in one column
  12.                 {
  13.                     continue
  14.                 } else if (column === squareCount && row>1) //all squares are in one row
  15.                 {
  16.                     continue
  17.                 }
  18.                 const formation = { rows: row, columns: column, size: row*column }
  19.                 squareFormations.push(formation)
  20.             }
  21.         }
  22.     }
  23.     return squareFormations
  24. }