Created
December 1, 2018 13:47
-
-
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.:
This file contains hidden or 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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
return Math.pow(n,3);