Skip to content

Instantly share code, notes, and snippets.

@paveleremin
Created January 9, 2019 17:17
Show Gist options
  • Save paveleremin/cc5191e9066099b0522bf326f6f98121 to your computer and use it in GitHub Desktop.
Save paveleremin/cc5191e9066099b0522bf326f6f98121 to your computer and use it in GitHub Desktop.
Google Solar Doomsday
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