Last active
February 23, 2022 20:58
-
-
Save ORESoftware/eb04fa4417f59456bd5ce61ec9372c03 to your computer and use it in GitHub Desktop.
doing simple dynamic programming for finding squares using previous computation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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