Skip to content

Instantly share code, notes, and snippets.

@andyperlitch
Created October 30, 2015 07:34
Show Gist options
  • Save andyperlitch/cf044e625f359428dbcb to your computer and use it in GitHub Desktop.
Save andyperlitch/cf044e625f359428dbcb to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
</style>
<script>
function findSquareset(n, divisor) {
if (!divisor) {
divisor = 1;
}
// Result array
var result = [];
// Let s be the square of the floor of the square root of n/divisor
var chunk = Math.pow(Math.floor(Math.sqrt(n/divisor)), 2);
// Add this to result {divisor} times
for (var i = divisor; i > 0; i--) {
result.push(chunk);
}
// Take remainder of n - divisor * chunk
var remainder = n - divisor * chunk;
if (remainder > 0) {
result = result.concat(findSquareset(remainder));
}
return result;
}
// Create Graph
var svg = d3.select('body').append('svg');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment