Skip to content

Instantly share code, notes, and snippets.

@ORESoftware
Last active February 23, 2022 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ORESoftware/eb04fa4417f59456bd5ce61ec9372c03 to your computer and use it in GitHub Desktop.
Save ORESoftware/eb04fa4417f59456bd5ce61ec9372c03 to your computer and use it in GitHub Desktop.
doing simple dynamic programming for finding squares using previous computation
// begin
const findAllSquares = (n: number) :Array<[number,number]> => {
let squares : Array<[number,number]> = [[0,0], [1,1]];
let diff = 1;
for(let i = 2; i <= n; i++){
const prev = squares[squares.length-1][1];
diff = diff + 2;
squares.push([i, prev + diff]);
}
return squares;
}
console.log(
findAllSquares(15)
)
// end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment