-
Notifications
You must be signed in to change notification settings - Fork 1
/
RowSumOddNumbers.java
69 lines (44 loc) · 1.3 KB
/
RowSumOddNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package challeges_from_codewars;
public class RowSumOddNumbers {
//Given the triangle of consecutive odd numbers:
// 1
// 3 5
// 7 9 11
//13 15 17 19
//21 23 25 27 29
//...
//Calculate the row sums of this triangle from the row index (starting at index
//rowSumOddNumbers(1); // 1
//rowSumOddNumbers(2); // 3 + 5 = 8
public static void main(String[] args) {
System.out.println(rowSumOddNumbers(1)); // 1
System.out.println(rowSumOddNumbers(2)); // 3 + 5 = 8
System.out.println(rowSumOddNumbers(3));
System.out.println(rowSumOddNumbers(4));
//System.out.println(rowSumOddNumbers2(1));
//System.out.println(rowSumOddNumbers2(2));
}
//first way to do it O(n) time complexity
public static int rowSumOddNumbers(int n) {
// formula n*(n-1) + 1
String format ="";
int counter = 0;
int result = 0;
for (int i = (n*(n-1))+1; counter < n; i++) {
if(i%2 == 1) {
counter ++;
result += i;
format = format + " " + Integer.toString(i);
}
}
System.out.println(format + " = sum "+ result);
return result;
}
//constant time complexity
public static int rowSumOddNumbers2(int n) {
return n* n* n;
}
public static int rowSumOddNumbers3(int n) {
return (int) Math.pow(n,3);
}
}