-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoreRecursion.java
More file actions
165 lines (99 loc) · 3.34 KB
/
Copy pathMoreRecursion.java
File metadata and controls
165 lines (99 loc) · 3.34 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
public class MoreRecursion {
/**
* Any problem solved with iteration can be solved using recursion.
*/
/*
EXAMPLE 1
Define a method that prints out hello 10 times.
*/
// solution using a loop
public static void printHello10TimesIteration() {
for (int i = 1; i <= 10; i += 1) {
System.out.println("Hello");
}
}
// solution using recursion
public static void printHello10TimesRecursion(int i) {
if (i == 10) return; // base case/stop condition
System.out.println("Hello");
printHello10TimesRecursion(i + 1);
}
/*
PROBLEM 1
Define a method that prints out numbers from 1 up to the passed input.
*/
// using a loop
public static void printNumbersFrom1UpToInputIteration(int input) {
for (int i = 1; i <= input; i += 1) {
System.out.println(i);
}
}
// using recursion
// TODO: use the solution above to write the method below using recursion
public static void printNumbersFrom1UpToInputRecursion(int input, int start) {
if (input == start) {
return;
}
printNumbersFrom1UpToInputRecursion(input, start + 1);
}
/**
* Any problem solved with recursion can be solved with iteration.
*/
/*
EXAMPLE 2
Create a method that will add numbers from 1 up to the passed in input.
*/
// using recursion
public static int addNumbersFrom1ToInputRecursion(int input, int total) {
if (input == 1) return total;
return addNumbersFrom1ToInputRecursion(input - 1, total + input);
}
// using iteration
public static int addNumbersFrom1ToInputIteration(int input) {
int total = 0;
for (int i = 1; i <= input; i += 1) {
total += i;
}
return total;
}
/*
PROBlEM 2
Create a method that will return the highest number up to a given limit achieved by doubling a given number.
*/
// using recursion
public static int doubleNumberUpToRecursion(int limit, int start) {
if (start * 2 > limit) {
return start;
} else {
return doubleNumberUpToRecursion(limit, start * 2);
}
}
// using iteration
// TODO: use the solution above to write the method below using iteration
public static int doubleNumberUpToLimitIteration(int limit, int start) {
while (start * 2 <= limit) {
start *= 2;
}
return start;
}
// use this main method to test out your code
public static void main(String[] args) {
doubleNumberUpToLimitIteration(20, 2);
}
// POSSIBLE SOLUTIONS BELOW THIS LINE!!! Avoid looking at a possible solution until you've created your own solution.
// Possible Solution for Problem 1
// public static void printNumbersFrom1UpToInputRecursion(int input, int start) {
// System.out.println(start);
// if (input == start) {
// return;
// }
// printNumbersFrom1UpToInputRecursion(input, start + 1);
// }
// Possible Solution for Problem 2
// public static int doubleNumberUpToLimitIteration(int limit, int start) {
// while (start * 2 <= limit) {
// start *= 2;
// }
// return start;
// }
}