Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Last active December 27, 2015 18:05
Show Gist options
  • Save mmloveaa/6e487a5de7c1d5178179 to your computer and use it in GitHub Desktop.
Save mmloveaa/6e487a5de7c1d5178179 to your computer and use it in GitHub Desktop.
CubeSummation
// 12/27/2015
// Write a function cubeSum(n, m) that will calculate a sum of cubes
// of numbers in a given range, starting from the smaller
// (but not including it) to the larger (including).
// The first argument is not necessarily the larger number.
// Examples:
// Test.expect(cubeSum(5,0) == 225, "cubeSum(5,0) should be 225");
// Test.expect(cubeSum(2,3) == 27, "cubeSum(2,3) should be 27");
// My Solution:
function cubeSum(n, m){
var sum=0;
if(n>m){
for(var i=m+1; i<=n;i++){
sum+=i*i*i;
}
}
else{
for(var i=n+1; i<=m;i++){
sum+=i*i*i;
}
}
return sum;
}
// cubeSum(5,0)
// 1^3 + 2^3 + 3^3 + 4^3 + 5^3
// 1 + 8 + 27 + 64 + 125 = 225
cubeSum(2,3)
// 3^3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment