recursion. what is recursion? smaller version sometimes, the best way to solve a problem is by...

70
Recursion

Upload: lesley-oliver

Post on 13-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursion

Page 2: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

What is recursion?

Sometimes, the best way to solve a problem is by solving a smaller smaller versionversion of the exact same problem first

Recursion is a technique that solves a problem by solving a smaller problemsmaller problem of the same type

Page 3: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursion

More than programming technique: a way of describing, defining, or

specifying things. a way of designing solutions to

problems (divide and conquer).

Page 4: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Basic Recursion

1. Base cases: Always have at least one case that

can be solved without using recursion.

2. Make progress: Any recursive call must make

progress toward a base case.

Page 5: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Mathematical ExamplesFibonacci Sequence:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …Fibonacci Function:Fib(0) = 1 // base caseFib(1) = 1 // base case Fib(n) = Fib(n-1) + Fib(n-2) // recursive call, n>1

Unlike most recursive algorithms: two base cases, not just one two recursive calls, not just one

Page 6: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Euclid's AlgorithmIn about 300 BC, Euclid wrote an algorithm to calculate the greatest common divisor (GCD) of two numbers x and y where (x < y). This can be stated as:

1. Divide y by x with remainder r. 2. Replace y by x, and x with r. 3. Repeat step 1 until r is zero.

When this algorithm terminates, y is the highest common factor.

Page 7: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

GCD(34017, 16966)

Euclid's algorithm works as follows: 34,017/16,966 produces a remainder 85 16,966/85 produces a remainder 51 85/51 produces a remainder 34 51/34 produces a remainder 17 34/17 produces a remainder 0

The highest common divisor of 34,017 and 16,966 is 17.

Page 8: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Key Elements of Euclid's Algorithm :Simple arithmetic operations

(calculating the remainder after division)

Comparison of a number against 0 (test)

Ability to repeatedly execute the same set of instructions (loop)

Any computer programming language has these basic elements.

Page 9: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Factorial Sequence Factorial Function

factorial(0) = 1 factorial(n) = n * factorial(n-1) [for

n>0]

Compute factorial(3). factorial(3)

= 3 * factorial(2)= 3 * ( 2 * factorial(1) )= 3 * ( 2 * ( 1 * factorial(0) )= 3 * ( 2 * ( 1 * 1 ) )) = 6

Page 10: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Coding the factorial function

Recursive Implementation

int factorial(int n){ if (n==0) // base case return 1; else return n * factorial(n-1);}

Page 11: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursive Call Stack

Page 12: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursion vs. Iteration

Recursion is based upon calling the same function over and over.

Iteration simply `jumps back' to the beginning of the loop.

A function call is often more expensive than a jump.

Page 13: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursion vs. Iteration

Iteration can be used in place of recursion An iterative algorithm uses a looping construct A recursive algorithm uses a branching

structureRecursive solutions are often less efficient,

in terms of both time and space, than iterative solutions

Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code

Page 14: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursion to Iteration ConversionMost recursive algorithms can be translated

by a fairly mechanical procedure into iterative algorithms.

Sometimes this is very straightforward most compilers detect a special form of recursion,

called tail recursion, and automatically translate into iteration automatically.

Sometimes, the translation is more involved May require introducing an explicit stack with

which to `fake' the effect of recursive calls.

Page 15: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Coding Factorial Function

Iterative implementation

int factorial(int n) { int fact = 1;  for(int count = 2; count <= n; count++) fact = fact * count;  return fact;}

Page 16: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Combinations: n choose k ()Given n things, how many different sets of size k can be

chosen?

n n-1 n-1 = + , 1 < k < n (recursive solution)k k k-1 n n! = , 1 < k < n (closed-form solution)k k!(n-k)!

with base cases:n n = n (k = 1), = 1 (k = n) 1 n

Pascal’s Triangle

Page 17: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

int combinations(int n, int k)

{

if(k == 1) // base case 1

return n;

else if (n == k) // base case 2

return 1;

else

return(combinations(n-1, k) + combinations(n-1, k-1));

}

Combinations: n choose k

Page 18: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Combinations:

Page 19: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Writing a Recursive Function

Determine the base case(s) (the one for which you know the answer)

Determine the general case(s) (the one where the problem is expressed as a smaller version of itself)

Verify the algorithm (use the "Three-Question-Method")

Page 20: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Three-Question Method • The Base-Case Question: Is there a non-recursive way out of the

function, and does the routine work correctly for this "base" case? 

• The Smaller-Caller Question: Does each recursive call to the function

involve a smaller case of the original problem, leading inescapably to the base case? 

• The General-Case Question: Assuming that the recursive call(s) work

correctly, does the whole function work correctly?

Page 21: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Euclid's Algorithm Recall:

Find greatest common divisor (GCD) of two integers x and y where (x < y):

1. y / x, save remainder r. 2. Replace y by x, and x with r. 3. Repeat until r is zero.

When this algorithm terminates, y is the highest common factor.

Page 22: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursive Binary Search Iterative Solution

Item retrieveItem(Item info[], Item item){ int midPoint; int first = 0; int last = length - 1; Boolean found = false; while( (first <= last) && !found) { midPoint = (first + last) / 2; if (item < info[midPoint]) last = midPoint - 1; else if(item > info[midPoint]) first = midPoint + 1; else { found = true; item = info[midPoint]; } }}

Page 23: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

What is the base case(s)? (1) If first > last, return false (2) If item==info[midPoint], return true

What is the general case?if item < info[midPoint] search the first halfif item > info[midPoint], search the second half

Recursive Binary Search

Page 24: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

boolean binarySearch(Item info[], Item item, int first, int last){ int midPoint; 

if(first > last) // base case 1 return false; else { midPoint = (first + last)/2; if(item < info[midPoint]) return BinarySearch(info, item, first, midPoint-1); else if (item == info[midPoint]) { // base case 2 item = info[midPoint]; return true; } else return binarySearch(info, item, midPoint+1, last); }}

Recursive Binary Search

Page 25: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Implementing RecursionWhat happens when a function gets called?

int a(int w){ return w+w;} 

int b(int x){ int z,y; ……………… // other statements

z = a(x) + y;

return z;}

Page 26: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

When a Function is CalledAn activation record is stored into a

stack (run-time or call stack)1) The computer has to stop executing function

b and starts executing function a2) Since it needs to come back to function b

later, it needs to store everything about function b that is going to need (x, y, z, and the place to start executing upon return)

3) Then, x from a is bounded to w from b4) Control is transferred to function a

Page 27: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

After function a is executed, the activation record is popped out of the run-time stack

All the old values of the parameters and variables in function b are restored and the return value of function a replaces a(x) in the assignment statement

When a Function is Called

Page 28: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

When Recursive Function Called

Except the fact that the calling and called functions have the same name, there is really no difference between recursive and non-recursive calls

int f(int x){ int y; 

if(x==0) return 1; else { y = 2 * f(x-1); return y+1; }}

Page 29: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

=f(3)

=f(2)

=f(1)

2*f(2)

2*f(1)

2*f(1)

=f(0)

Page 30: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursive InsertItem (Sorted List)

location

location

location

location

Page 31: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

What is the base case(s)?1) If the list is empty, insert item into the

empty list2) If item < location.info, insert item as the

first node in the current listWhat is the general case?

Insert(location.next, item)

Recursive InsertItem (Sorted List)

Page 32: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

void insert(Node<Item> node, Item item){ if(node == NULL) || (item < node.getItem()) { // base cases NodeType<Item> temp = node; node = new NodeType<Item>; location.setElement(item); location.setNext(temp); } else insert(node.getNext(), item); // general case} 

void insertItem(Item newItem){

insert(head, newItem);}

Recursive InsertItem (Sorted List)

Page 33: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

- No "predLoc" pointer is needed for insertion

location

Page 34: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

When to Use Recursion When the depth of recursive calls is

relatively "shallow"The recursive version does about the

same amount of work as the non-recursive version

The recursive version is shorter and simpler than the non-recursive solution

Page 35: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Benefits of Recursion

1. Recursive functions are clearer, simpler, shorter, and easier to understand than their non-recursive counterparts.

2. The program directly reflects the abstract solution strategy (algorithm).

3. From a practical software engineering point of view, greatly enhances the cost of maintaining the software.

Page 36: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Disadvantages of Recursion

1. Makes it easier to write simple and elegant programs, but it also makes it easier to write inefficient ones.

2. Use recursion to ensure correctness, not efficiency. My simple, elegant recursive algorithms are inherently inefficient.

Page 37: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first
Page 38: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Space: Every invocation of a function call requires:

space for parameters and local variables space for return address

Thus, a recursive algorithm needs space proportional to the number of nested calls to the same function.

Recursion Overhead

Page 39: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursion Overhead

Time: The operations involved in calling a function

allocating, and later releasing, local memory copying values into the local memory for the

parameters branching to/returning from the function

All contribute to the time overhead.

Page 40: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Cumulative Affects

If a function has very large local memory requirements, it would be very costly to program it recursively.

Even if there is very little overhead in a single function call, recursive functions often call themselves many times, which can magnify a small individual overhead into a very large cumulative overhead.

Page 41: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Cumulative Affects - Factorial

int factorial(int n){ if (n == 0) {return 1;} else {return n * factorial(n-1);}}Little overhead

only one word of local memory for the parameter n.

However, when we try to compute factorial(20) 21 words of memory are allocated.

Page 42: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Cumulative Affects - Factorial (cont’d)factorial(20) -- allocate 1 word of memory, call factorial(19) -- allocate 1 word of memory, call factorial(18) -- allocate 1 word of memory, . . . call factorial(2) -- allocate 1 word of memory, call factorial(1) -- allocate 1 word of memory, call factorial(0) -- allocate 1 word of

memory,At this point, 21 words of memory and 21 activation records

have been allocated.

Page 43: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursive Functions

int f(int x){ int y; if(x==0) return 1; else {

y = 2 * f(x-1); return y+1; }}

Page 44: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Iteration as a special case of recursion

Iteration is a special case of recursion: void do_loop () { do { ... } while (e); }

is equivalent to: void do_loop () { ... ; if (e) do_loop(); }

A compiler can recognize instances of this form of recursion and turn them into loops or simple jumps.

Page 45: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursive DeleteItem (Sorted List)

location

location

Page 46: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

What is the size factor?The number of elements in the list

What is the base case(s)? If item == location->info, delete node pointed by location

What is the general case? Delete(location->next, item)

Recursive DeleteItem (sorted list)

Page 47: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

template <class ItemType>void Delete(NodeType<ItemType>* &location, ItemType item){ if(item == location->info)) { NodeType<ItemType>* tempPtr = location; location = location->next; delete tempPtr; } else Delete(location->next, item);} template <class ItemType>void SortedType<ItemType>::DeleteItem(ItemType item){ Delete(listData, item);}

Recursive DeleteItem (sorted list)

(cont.)

Page 48: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursion can be very inefficient is some cases

+=

=

=

=

=

=

+ +

+ + + +

++ + + + +

+

+

+

+

+ + +

+ + + + + + +

+ +

+

++++++++3

3

C o m b (3 , 1 )

2

C o m b (2 , 1 )

1

C o m b (2 , 2 )

C o m b (3 , 2 )

C o m b (4 ,2 )

2

C o m b (2 , 1 )

1

C o m b (2 , 2 )

C o m b (3 , 2 )

1

1

C o m b (3 , 3 )

C o m b (4 , 3 )

C o m b (5 , 3 )

2

C o m b (2 , 1 )

1

C o m b (2 , 2 )

C o m b (3 , 2 )

1

1

C o m b (3 , 3 )

C o m b (4 , 3 )

1

1

1

C o m b (4 , 4 )

C o m b (5 , 4 )

C o m b (6 ,4 )

15

Page 49: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

We have seen one form of circularity already in our classes, with a For loop.Int x;For (x=0; x<=10; x++) { cout<<x; }

Page 50: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Problems solving used loops

In a for loop, we have a set loop structure which controls the length of the repetition. Many problems solved using loops may be solved using a recursion. In recursion, problems are defined in terms of smaller versions of themselves.

Page 51: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Power function

There are recursive definitions for many mathematical problems:The function Power (used to raise the number y to the xth power). Assume x is a non-negative integer:

Y^x = 1 if x is 0; otherwise, Y*Y^(x-1)

Page 52: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Power Function

2^3 = 2*2^2 = 2 * 4 = 8 2^2 = 2*2^1 = 2 * 2 = 4

2^1 = 2*2^0 = 2 * 1 = 2 2^0 = 1

Page 53: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Factorial Function

The factorial function has a natural recursive definition:n!= 1, if n = 0 or if n = 1; otherwise, n! = n * (n - 1)!

Page 54: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

For example: 5! = 5*4! = 5*24 4! = 4*3! = 4*6 3! = 3*2! = 3*2 2! = 2*1! = 2*1 1! = 1

Page 55: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Excessive Recursion

When a program runs too deep: When a simple loop runs more efficiently:Fibonacci sequence:

Page 56: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Ackermann’s Function

“…one of the fastest growing non-primitive recursive functions. Faster growing than any primitive recursive function.”It grows from 0 to 2^65546 in a few breaths.

Page 57: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Basis for Ackermann’s

If A(0, n)=n+1, by definitionIf A(m, 0)=A(m-1, 1)else, A(m, n)=A(m-1, A(m, n-1))……until A(0, A(1, A(…m-2, n-1)))…back to definition

Page 58: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Example

A(2, 2)=A(1, A(2, 1))=A(0, A(1, A(2, 1)))…=A(1, A(2, 1))+1=A(0, A(1, A(2, 0)))+1…=A(1, A(1, 1))+2=A(0, A(1, A(1, 0)))…=A(1, A(0, 1))+3=A(0, A(0, 0))+5=7!!!

Page 59: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Shortcuts:

If A(0, n)=n+1If A(1, n)=n+2If A(2, n)=2n+3If A(3, n)=2^n+3If A(4, n)=2^(n+3)*2If A(5, n)=TOO MUCH!!!!!

Page 60: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

A(4, 1)=13A(4, 2)=65533A(4, 3)=2^65536-3A(4, 4)=2^(2^(65536)-3)

Page 61: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

“ Ackermann’s function is a form of meta-multiplication.”Dr. Forb.a+b=adding the operand a*b=adding the operand “a” to itself b timesa^b=multiplying the operand “a” by itself b timesa@b=a^b, b times…a@@b=a@b, b times

Page 62: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Recursion

Recursion can be seen as building objects from objects that have set definitions. Recursion can also be seen in the opposite direction as objects that are defined from smaller and smaller parts. “Recursion is a different concept of circularity.”(Dr. Britt, Computing Concepts Magazine, March 97, pg.78)

Page 63: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Suppose that we have a series of functions for finding the power of a number x.

pow0(x) = 1

pow1(x) = x = x * pow0(x)

pow2(x) = x * x = x * pow1(x) pow3(x) = x * x * x = x * pow2(x)

We can turn this into something more usable by creating a variable for the power and making the pattern explicit: pow(0,x) = 1

pow(n,x) = x * pow(n-1,x)

Finding the powers of numbers

Page 64: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

For instance:

2**3 = 2 * 2**2 = 2 * 4 = 8

2**2 = 2 * 2**1 = 2 * 2 = 4

2**1 = 2 * 2**0 = 2 * 1 = 2

2**0 = 1

Page 65: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Almost all programminglanguages allow recursive functions calls.

That is they allow a function to call itself. And some languages allow recursive

definitions of data structures. This means we can directly implement the recursive definitions and recursive algorithms that we have just been discussing.

For example, the definition of the factorial function

factorial(0) = 1

factorial(n) = n * factorial(n-1) [ for n > 0

].can be written in C without

the slightest change: int factorial(int n){ if (n == 0) return 1 ; else return n *

factorial(n-1) ;}

Page 66: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Basic RecursionBasic Recursion

What we see is that if we have a base case, and if our recursive calls make progress toward reaching the base case, then eventually we terminate. We thus have our first two fundamental rules of recursion:

Page 67: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Euclid's Algorithm

Euclid's Algorithm determines the greatest common divisor of two natural numbers a, b. That is, the largest natural number d such that d | a and d | b.

GCD(33,21)=3 33 = 1*21 + 12 21 = 1*12 + 9 12 = 1*9 + 3 9 = 3*3

Page 68: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

The main thing to note here is that the variables that will hold the intermediate results, S1 and S2, have been declared as globalvariables

. This is a mistake. Although the function looks just fine, its correctness crucially depends on having local variables for

storing all the intermediate results. As shown, it will not correctly compute the fibonacci function for n=4 or larger. However, if we move the declaration of s1 and s2 inside the function, it works perfectly.

This sort of bug is very hard to find, and bugs like this are almost certain to arise whenever you use global variables to storeintermediate results of a recursive function.

Page 69: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

{Non-recursive version of Power function}

FUNCTION PowerNR (Base : real; Exponent : integer) : real;

{Preconditions: Exponent >= 0}

{Accepts Base and exponent values}

{Returns Base to the Exponent power}

VAR Count : integer; {Counts number of times BAse is multiplied}

Product : real;

{Holds the answer as it is being calculated}

BEGIN Product := 1; FOR Count := 1 TO

Exponent DO Product :=

Product * Base; PowerNR := ProductEND; {PowerNR}

Page 70: Recursion. What is recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

Consider the following program for

computing the Fibonacci Sequence

int fibonacci (int n){ if (n == 0) {return 1}; else if (n == 1) {return 1}; else {return fibonacci(n-1) +

fibonacci(n-2)};}