Recursion - York University
Transcript of Recursion - York University
![Page 1: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/1.jpg)
Recursion
notes Chapter 8
1
![Page 2: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/2.jpg)
Tower of Hanoi
move the stack of n disks from A to C
can move one disk at a time from the top of one stack onto another stack
cannot move a larger disk onto a smaller disk
2
A B C
![Page 3: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/3.jpg)
Tower of Hanoi legend says that the world will end when a 64 disk
version of the puzzle is solved
several appearances in pop culture
Doctor Who
Rise of the Planet of the Apes
Survior: South Pacific
3
![Page 4: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/4.jpg)
Tower of Hanoi n = 1
move disk from A to C
4
A B C
![Page 5: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/5.jpg)
Tower of Hanoi n = 1
5
A B C
![Page 6: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/6.jpg)
Tower of Hanoi n = 2
move disk from A to B
6
A B C
![Page 7: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/7.jpg)
Tower of Hanoi n = 2
move disk from A to C
7
A B C
![Page 8: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/8.jpg)
Tower of Hanoi n = 2
move disk from B to C
8
A B C
![Page 9: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/9.jpg)
Tower of Hanoi n = 2
9
A B C
![Page 10: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/10.jpg)
Tower of Hanoi n = 3
move disk from A to C
10
A B C
![Page 11: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/11.jpg)
Tower of Hanoi n = 3
move disk from A to B
11
A B C
![Page 12: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/12.jpg)
Tower of Hanoi n = 3
move disk from C to B
12
A B C
![Page 13: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/13.jpg)
Tower of Hanoi n = 3
move disk from A to C
13
A B C
![Page 14: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/14.jpg)
Tower of Hanoi n = 3
move disk from B to A
14
A B C
![Page 15: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/15.jpg)
Tower of Hanoi n = 3
move disk from B to C
15
A B C
![Page 16: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/16.jpg)
Tower of Hanoi n = 3
move disk from A to C
16
A B C
![Page 17: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/17.jpg)
Tower of Hanoi n = 3
17
A B C
![Page 18: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/18.jpg)
Tower of Hanoi write a loop-based method to solve the Tower of Hanoi
problem
discuss amongst yourselves now...
18
![Page 19: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/19.jpg)
Tower of Hanoi imagine that you had the following method (see next
slide)
how would you use the method to solve the Tower of Hanoi problem?
discuss amongst yourselves now...
19
![Page 20: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/20.jpg)
Tower of Hanoi/**
* Prints the sequence of moves required to move n disks from the
* starting pole (from) to the goal pole (to) using a third pole
* (using).
*
* @param n
* the number of disks to move
* @param from
* the starting pole
* @param to
* the goal pole
* @param using
* a third pole
* @pre. n is greater than 0
*/
public static void move(int n, String from, String to, String using)
20
![Page 21: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/21.jpg)
Tower of Hanoi n = 4
you eventually end up at (see next slide)...
21
A B C
![Page 22: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/22.jpg)
Tower of Hanoi n = 4
move disk from A to C
22
A B C
![Page 23: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/23.jpg)
Tower of Hanoi n = 4
move (n – 1) disks from B to C using A
23
A B C
![Page 24: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/24.jpg)
Tower of Hanoi n = 4
24
A B C
![Page 25: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/25.jpg)
Tower of Hanoi notice that to solve the n = 4 size problem, you have to
solve a variation of the n = 3 size problem twice and a variation of the n = 1 size problem once
we can use the move method to solve these 3 sub-problems
25
![Page 26: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/26.jpg)
Tower of Hanoi the basic solution can be described as follows:
1. move (n – 1) disks from A to B
2. move 1 disk from A to C
3. move (n – 1) disks from B to C
furthermore:
if exactly n == 1 disk is moved, print out the starting pole and the goal pole for the move
26
![Page 27: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/27.jpg)
Tower of Hanoi
public static void move(int n, String from, String to, String using) {
if (n == 1) {
System.out.println("move disk from " + from + " to " + to);
}
else {
move(n - 1, from, using, to);
move(1, from, to, using);
move(n - 1, using, to, from);
}
}
27
![Page 28: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/28.jpg)
Printing n of Something suppose you want to implement a method that prints
out n copies of a string
public static void printIt(String s, int n) {
for(int i = 0; i < n; i++) {
System.out.print(s);
}
}
28
![Page 29: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/29.jpg)
A Different Solution alternatively we can use the following algorithm:
1. if n == 0 done, otherwise
I. print the string once
II. print the string (n – 1) more times
public static void printItToo(String s, int n) {
if (n == 0) {
return;
}
else {
System.out.print(s);
printItToo(s, n - 1); // method invokes itself
}
}
29
![Page 30: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/30.jpg)
Recursion a method that calls itself is called a recursive method
a recursive method solves a problem by repeatedly reducing the problem so that a base case can be reached
printItToo("*", 5)
*printItToo ("*", 4)
**printItToo ("*", 3)
***printItToo ("*", 2)
****printItToo ("*", 1)
*****printItToo ("*", 0) base case
*****
30
Notice that the number of timesthe string is printed decreasesafter each recursive call to printIt
Notice that the base case iseventually reached.
![Page 31: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/31.jpg)
Infinite Recursion if the base case(s) is missing, or never reached, a
recursive method will run forever (or until the computer runs out of resources)
public static void printItForever(String s, int n) {
// missing base case; infinite recursion
System.out.print(s);
printItForever(s, n - 1);
}
printItForever("*", 1)
* printItForever("*", 0)
** printItForever("*", -1)
*** printItForever("*", -2) ...........
31
![Page 32: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/32.jpg)
Climbing a Flight of n Stairs not Java
/**
* method to climb n stairs
*/
climb(n) :
if n == 0
done
else
step up 1 stair
climb(n – 1);
end
32
![Page 33: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/33.jpg)
Rabbits
33
Month 0: 1 pair 0 additional pairs
Month 1: first pairmakes another pair
1 additional pair
Month 2: each pairmakes another pair;oldest pair dies
1 additional pair
Month 3: each pairmakes another pair;oldest pair dies
2 additional pairs
![Page 34: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/34.jpg)
Fibonacci Numbers the sequence of additional pairs
0, 1, 1, 2, 3, 5, 8, 13, ...
are called Fibonacci numbers
base cases
F(0) = 0
F(1) = 1
recursive definition
F(n) = F(n – 1) + F(n – 2)
34
![Page 35: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/35.jpg)
Recursive Methods & Return Values a recursive method can return a value
example: compute the nth Fibonacci number
public static int fibonacci(int n) {
if (n == 0) {
return 0;
}
else if (n == 1) {
return 1;
}
else {
int f = fibonacci(n - 1) + fibonacci(n - 2);
return f;
}
}
35
![Page 36: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/36.jpg)
Recursive Methods & Return Values write a recursive method that multiplies two positive
integer values (i.e., both values are strictly greater than zero)
observation: 𝑚 × 𝑛 means add 𝑚 𝑛's together
in other words, you can view multiplication as recursive addition
36
![Page 37: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/37.jpg)
Recursive Methods & Return Values
not Java:
/*** Computes m * n
*/
multiply(m, n) :
if m == 1
return n
else
return n + multiply(m - 1, n)
37
![Page 38: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/38.jpg)
public static int multiply(int m, int n) {
if (m == 1) {
return n;
}
return n + multiply(m - 1, n);
}
38
![Page 39: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/39.jpg)
Recursive Methods & Return Values example: write a recursive method countZeros that
counts the number of zeros in an integer number n
10305060700002L has 8 zeros
trick: examine the following sequence of numbers
1. 10305060700002
2. 1030506070000
3. 103050607000
4. 10305060700
5. 103050607
6. 1030506 ...
39
![Page 40: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/40.jpg)
Recursive Methods & Return Values
not Java:
/*** Counts the number of zeros in an integer n
*/
countZeros(n) :
if the last digit in n is a zero
return 1 + countZeros(n / 10)
else
return countZeros(n / 10)
40
![Page 41: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/41.jpg)
don't forget to establish the base case(s)
when should the recursion stop? when you reach a single digit (not zero digits; you never reach zero digits!)
base case #1 : n == 0
return 1
base case #2 : n != 0 && n < 10
return 0
41
![Page 42: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/42.jpg)
public static int countZeros(long n) {
if(n == 0L) { // base case 1
return 1;
}
else if(n < 10L) { // base case 2
return 0;
}
boolean lastDigitIsZero = (n % 10L == 0);
final long m = n / 10L;
if(lastDigitIsZero) {
return 1 + countZeros(m);
}
else {
return countZeros(m);
}
}
42
![Page 43: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/43.jpg)
countZeros Call StackcallZeros( 800410L )
43
callZeros( 800410L )
callZeros( 80041L )
callZeros( 8004L )
callZeros( 800L )
callZeros( 80L )
callZeros( 8L )
1 + 0 + 0 + 1 + 1 + 0
0 + 0 + 1 + 1 + 0
0 + 1 + 1 + 0
1 + 1 + 0
1 + 0
0
= 3
last in first out
![Page 44: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/44.jpg)
Fibonacci Call Tree
44
F(5)
F(4)
F(3)
F(2)
F(1)1
F(0)0
F(1)1
F(2)
F(1)1
F(0)0
F(3)
F(2)
F(1)1
F(0)0
F(1)1
![Page 45: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/45.jpg)
Compute Powers of 10 write a recursive method that computes 10n for any
integer value n
recall:
100 = 1
10n = 10 * 10n-1
10-n = 1 / 10n
45
![Page 46: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/46.jpg)
public static double powerOf10(int n) {
if (n == 0) {
// base case
return 1.0;
}
else if (n > 0) {
// recursive call for positive n
return 10.0 * powerOf10(n - 1);
}
else {
// recursive call for negative n
return 1.0 / powerOf10(-n);
}
}
46
![Page 47: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/47.jpg)
Fibonacci Numbers the sequence of additional pairs
0, 1, 1, 2, 3, 5, 8, 13, ...
are called Fibonacci numbers
base cases
F(0) = 0
F(1) = 1
recursive definition
F(n) = F(n – 1) + F(n – 2)
47
![Page 48: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/48.jpg)
Recursive Methods & Return Values a recursive method can return a value
example: compute the nth Fibonacci number
public static int fibonacci(int n) {
if (n == 0) {
return 0;
}
else if (n == 1) {
return 1;
}
else {
int f = fibonacci(n - 1) + fibonacci(n - 2);
return f;
}
}
48
![Page 49: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/49.jpg)
Fibonacci Call Tree
49
F(5)
F(4)
F(3)
F(2)
F(1)1
F(0)0
F(1)1
F(2)
F(1)1
F(0)0
F(3)
F(2)
F(1)1
F(0)0
F(1)1
![Page 50: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/50.jpg)
A Better Recursive Fibonaccipublic class Fibonacci {
private static Map<Integer, Long> values = new HashMap<Integer, Long>();
static {
Fibonacci.values.put(0, (long) 0);
Fibonacci.values.put(1, (long) 1);
}
public static long getValue(int n) {
Long value = Fibonacci.values.get(n);
if (value != null) {
return value;
}
value = Fibonacci.getValue(n - 1) + Fibonacci.getValue(n - 2);
Fibonacci.values.put(n, value);
return value;
}
}
50
![Page 51: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/51.jpg)
Better Fibonacci Call Tree
51
F(5)
F(4)
F(3)
F(2)
F(1)1
F(0)0
F(1)1
F(2)1
F(3)2
values in blue are already storedin the map
![Page 52: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/52.jpg)
A Better Recursive Fibonacci because the map is static subsequent calls to
Fibonacci.getValue(int) can use the values already computed and stored in the map
52
![Page 53: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/53.jpg)
Better Fibonacci Call Tree assuming the client has already invoked
Fibonacci.getValue(5)
53
F(6)
F(4)3
F(5)5
values in blue are already storedin the map
![Page 54: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/54.jpg)
Compute Powers of 10 write a recursive method that computes 10n for any
integer value n
recall:
10n = 1 / 10-n if n < 0
100 = 1
10n = 10 * 10n-1
54
![Page 55: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/55.jpg)
55
public static double powerOf10(int n) {
if (n < 0) {
return 1.0 / powerOf10(-n);
}
else if (n == 0) {
return 1.0;
}
return n * powerOf10(n - 1);
}
![Page 56: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/56.jpg)
A Better Powers of 10 recall:
10n = 1 / 10-n if n < 0
100 = 1
10n = 10 * 10n-1 if n is odd
10n = 10n/2 * 10n/2 if n is even
56
![Page 57: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/57.jpg)
57
public static double powerOf10(int n) {
if (n < 0) {
return 1.0 / powerOf10(-n);
}
else if (n == 0) {
return 1.0;
}
else if (n % 2 == 1) {
return 10 * powerOf10(n - 1);
}
double value = powerOf10(n / 2);
return value * value;
}
![Page 58: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/58.jpg)
What happens during recursion
58
![Page 59: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/59.jpg)
What Happens During Recursion a simplified model of what happens during a recursive
method invocation is the following:
whenever a method is invoked that method runs in a newblock of memory
when a method recursively invokes itself, a new block of memory is allocated for the newly invoked method to run in
consider a slightly modified version of the powerOf10method
59
![Page 60: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/60.jpg)
60
public static double powerOf10(int n) {
double result;
if (n < 0) {
result = 1.0 / powerOf10(-n);
}
else if (n == 0) {
result = 1.0;
}
else {
result = 10 * powerOf10(n - 1);
}
return result;
}
![Page 61: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/61.jpg)
61
double x = Recursion.powerOf10(3);
100 main method
x powerOf10(3)
![Page 62: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/62.jpg)
62
600 powerOf10 method
n 3
result100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
• methods occupy space in a region of memory called the call stack • information regarding the state of the method is stored in a stack frame• the stack frame includes information such as the method parameters, local
variables of the method, where the return value of the method should be copied to, where control should flow to after the method completes, ...
• stack memory can be allocated and deallocated very quickly, but this speed is obtained by restricting the total amount of stack memory
• if you try to solve a large problem using recursion you can exceed the available amount of stack memory which causes your program to crash
a stack frame
![Page 63: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/63.jpg)
63
600 powerOf10 method
n 3
result 10 * powerOf10(2)100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 64: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/64.jpg)
64
600 powerOf10 method
n 3
result 10 * powerOf10(2)
750 powerOf10 method
n 2
result
100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 65: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/65.jpg)
65
600 powerOf10 method
n 3
result 10 * powerOf10(2)
750 powerOf10 method
n 2
result 10 * powerOf10(1)
100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 66: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/66.jpg)
66
600 powerOf10 method
n 3
result 10 * powerOf10(2)
750 powerOf10 method
n 2
result 10 * powerOf10(1)
800 powerOf10 method
n 1
result 10 * powerOf10(0)
100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 67: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/67.jpg)
67
600 powerOf10 method
n 3
result 10 * powerOf10(2)
750 powerOf10 method
n 2
result 10 * powerOf10(1)
800 powerOf10 method
n 1
result 10 * powerOf10(0)
950 powerOf10 method
n 0
result
100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 68: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/68.jpg)
68
600 powerOf10 method
n 3
result 10 * powerOf10(2)
750 powerOf10 method
n 2
result 10 * powerOf10(1)
800 powerOf10 method
n 1
result 10 * powerOf10(0)
950 powerOf10 method
n 0
result 1
100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 69: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/69.jpg)
69
600 powerOf10 method
n 3
result 10 * powerOf10(2)
750 powerOf10 method
n 2
result 10 * powerOf10(1)
800 powerOf10 method
n 1
result 10 * 1
950 powerOf10 method
n 0
result 1
100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 70: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/70.jpg)
70
600 powerOf10 method
n 3
result 10 * powerOf10(2)
750 powerOf10 method
n 2
result 10 * powerOf10(1)
800 powerOf10 method
n 1
result 10
100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 71: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/71.jpg)
71
600 powerOf10 method
n 3
result 10 * powerOf10(2)
750 powerOf10 method
n 2
result 10 * 10
800 powerOf10 method
n 1
result 10
100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 72: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/72.jpg)
72
600 powerOf10 method
n 3
result 10 * powerOf10(2)
750 powerOf10 method
n 2
result 100
100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 73: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/73.jpg)
73
600 powerOf10 method
n 3
result 10 * 100
750 powerOf10 method
n 2
result 100
100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 74: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/74.jpg)
74
600 powerOf10 method
n 3
result 1000100 main method
x powerOf10(3)
double x = Recursion.powerOf10(3);
![Page 75: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/75.jpg)
75
600 powerOf10 method
n 3
result 1000100 main method
x 1000
double x = Recursion.powerOf10(3);
![Page 76: Recursion - York University](https://reader030.fdocuments.us/reader030/viewer/2022012422/61771a14a8d3e41721677b44/html5/thumbnails/76.jpg)
76
100 main method
x 1000
double x = Recursion.powerOf10(3);