|
@@ -2,125 +2,72 @@ package io.zipcoder.microlabs.mastering_loops;
|
2
|
2
|
|
3
|
3
|
public class NumberUtilities {
|
4
|
4
|
public static String getEvenNumbers(int start, int stop) {
|
5
|
|
- int i = 0;
|
6
|
|
- int j = start;
|
7
|
|
- StringBuilder builder = new StringBuilder();
|
8
|
|
-
|
9
|
|
- while(j < stop) {
|
10
|
|
- if(j % 2 == 0)
|
11
|
|
- {
|
12
|
|
- builder.append(j);
|
13
|
|
- j+=2;
|
14
|
|
- i++;
|
15
|
|
- }
|
16
|
|
- else
|
17
|
|
- {
|
18
|
|
- j++;
|
19
|
|
- }
|
20
|
|
- }
|
21
|
|
-
|
22
|
|
- return builder.toString();
|
|
5
|
+ return getNumberSequence(start, stop, true);
|
23
|
6
|
}
|
24
|
7
|
|
25
|
|
-
|
26
|
8
|
public static String getOddNumbers(int start, int stop) {
|
27
|
|
- int i = 0;
|
28
|
|
- int j = start;
|
29
|
|
- StringBuilder builder = new StringBuilder();
|
30
|
|
-
|
31
|
|
- while(j < stop)
|
32
|
|
- {
|
33
|
|
- if(j % 2 != 0)
|
34
|
|
- {
|
35
|
|
- builder.append(j);
|
36
|
|
- j+=2;
|
37
|
|
- i++;
|
38
|
|
- }
|
39
|
|
- else
|
40
|
|
- {
|
41
|
|
- j++;
|
42
|
|
- }
|
43
|
|
- }
|
44
|
|
-
|
45
|
|
- return builder.toString();
|
|
9
|
+ return getNumberSequence(start, stop, false);
|
46
|
10
|
}
|
47
|
11
|
|
48
|
12
|
public static String getSquareNumbers(int start, int stop) {
|
49
|
|
- int i = start;
|
50
|
|
- StringBuilder builder = new StringBuilder();
|
51
|
|
-
|
52
|
|
- while(i < stop)
|
53
|
|
- {
|
54
|
|
- builder.append(i * i);
|
55
|
|
- i++;
|
56
|
|
- }
|
57
|
|
-
|
58
|
|
- return builder.toString();
|
|
13
|
+ return getSquareNumbers(start, stop, 1);
|
59
|
14
|
}
|
60
|
15
|
|
61
|
|
-
|
62
|
16
|
public static String getSquareNumbers(int start, int stop, int step) {
|
63
|
|
- int i = start;
|
64
|
|
- StringBuilder builder = new StringBuilder();
|
65
|
|
-
|
66
|
|
- while(i < stop)
|
67
|
|
- {
|
68
|
|
- builder.append(i * i);
|
69
|
|
- i += step;
|
70
|
|
- }
|
71
|
|
-
|
72
|
|
- return builder.toString();
|
|
17
|
+ return getExponentiations(start, stop, step, 2);
|
73
|
18
|
}
|
74
|
19
|
|
75
|
20
|
public static String getRange(int stop) {
|
76
|
|
- int i = 0;
|
77
|
|
- StringBuilder builder = new StringBuilder();
|
78
|
|
-
|
79
|
|
- while(i < stop)
|
80
|
|
- {
|
81
|
|
- builder.append(i);
|
82
|
|
- i++;
|
83
|
|
- }
|
84
|
|
-
|
85
|
|
- return builder.toString();
|
|
21
|
+ return getRange(0, stop, 1);
|
86
|
22
|
}
|
87
|
23
|
|
88
|
24
|
public static String getRange(int start, int stop) {
|
|
25
|
+ return getRange(start, stop, 1);
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ public static String getRange(int start, int stop, int step) {
|
89
|
29
|
int i = start;
|
90
|
30
|
StringBuilder builder = new StringBuilder();
|
91
|
31
|
|
92
|
32
|
while(i < stop)
|
93
|
33
|
{
|
94
|
34
|
builder.append(i);
|
95
|
|
- i++;
|
|
35
|
+ i += step;
|
96
|
36
|
}
|
97
|
37
|
|
98
|
38
|
return builder.toString();
|
99
|
39
|
}
|
100
|
40
|
|
101
|
|
-
|
102
|
|
- public static String getRange(int start, int stop, int step) {
|
|
41
|
+ public static String getExponentiations(int start, int stop, int step, int exponent) {
|
103
|
42
|
int i = start;
|
104
|
43
|
StringBuilder builder = new StringBuilder();
|
105
|
44
|
|
106
|
45
|
while(i < stop)
|
107
|
46
|
{
|
108
|
|
- builder.append(i);
|
109
|
|
- i+=step;
|
|
47
|
+ builder.append((int)Math.pow(i, exponent));
|
|
48
|
+ i += step;
|
110
|
49
|
}
|
111
|
50
|
|
112
|
51
|
return builder.toString();
|
113
|
52
|
}
|
114
|
53
|
|
115
|
|
-
|
116
|
|
- public static String getExponentiations(int start, int stop, int step, int exponent) {
|
117
|
|
- int i = start;
|
|
54
|
+ private static String getNumberSequence(int start, int stop, boolean isEven) {
|
|
55
|
+ int i = 0;
|
|
56
|
+ int j = start;
|
118
|
57
|
StringBuilder builder = new StringBuilder();
|
119
|
58
|
|
120
|
|
- while(i < stop)
|
|
59
|
+ while(j < stop)
|
121
|
60
|
{
|
122
|
|
- builder.append((int)Math.pow(i, exponent));
|
123
|
|
- i += step;
|
|
61
|
+ if((isEven && j % 2 == 0) || (!isEven && j % 2 != 0))
|
|
62
|
+ {
|
|
63
|
+ builder.append(j);
|
|
64
|
+ j+=2;
|
|
65
|
+ i++;
|
|
66
|
+ }
|
|
67
|
+ else
|
|
68
|
+ {
|
|
69
|
+ j++;
|
|
70
|
+ }
|
124
|
71
|
}
|
125
|
72
|
|
126
|
73
|
return builder.toString();
|