Skip to content

Commit 75046dc

Browse files
authored
Create SumOfOddNumbers.java
1 parent cc2737e commit 75046dc

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Given the triangle of consecutive odd numbers:
3+
4+
1
5+
3 5
6+
7 9 11
7+
13 15 17 19
8+
21 23 25 27 29
9+
...
10+
Calculate the sum of the numbers in the nth row of this triangle (starting at index 1) e.g.: (Input --> Output)
11+
12+
1 --> 1
13+
2 --> 3 + 5 = 8
14+
*/
15+
16+
import java.util.stream.IntStream;
17+
18+
class RowSumOddNumbers {
19+
public static long rowSumOddNumbers(int rowNumber) {
20+
int startingNumber = (rowNumber - 1) * rowNumber + 1;
21+
return IntStream.range(startingNumber, startingNumber + rowNumber * 2)
22+
.filter(number -> number % 2 != 0)
23+
.sum();
24+
}
25+
26+
}

0 commit comments

Comments
 (0)