Created
January 9, 2019 17:17
-
-
Save paveleremin/cc5191e9066099b0522bf326f6f98121 to your computer and use it in GitHub Desktop.
Google Solar Doomsday
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
function sum(arr) { | |
return arr.reduce((sum, n) => sum + n, 0); | |
} | |
function answer(initArea) { | |
var list = []; | |
var area = initArea; | |
var i = 1; | |
while (sum(list) < initArea) { | |
if ((i + 1) ** 2 >= area) { | |
list.push(i ** 2); | |
area -= i ** 2 | |
i = 1 | |
} | |
else { | |
i += 1 | |
} | |
} | |
return list; | |
} | |
console.log(answer(12), [9, 1, 1, 1]) | |
console.log(answer(15324), [15129, 169, 25, 1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment