We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent cc2737e commit 75046dcCopy full SHA for 75046dc
StreamSolutions/SumOfOddNumbers.java
@@ -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