Skip to content

Instantly share code, notes, and snippets.

@avraamisvi
Created December 1, 2018 13:47
Show Gist options
  • Save avraamisvi/a9cea52b941369c5ac902a0129dccfed to your computer and use it in GitHub Desktop.
Save avraamisvi/a9cea52b941369c5ac902a0129dccfed to your computer and use it in GitHub Desktop.
Calculate the row sums of this triangle from the row index (starting at index 1) e.g.:
package com.company;
class RowSumOddNumbers {
public static int rowSumOddNumbers(int n) {
return sum(lastElement(n)) - sum(lastElement(n - 1));
}
private static int lastElement(int n) {
return ((n * n) + (n - 1));
}
private static int sum(int n) {
int s = ((n + 1) / 2);
return s * s;
}
}
@presetslrdev
Copy link

return Math.pow(n,3);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment