Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Blowing a Gasket - Classic Recursive Example - Slides | CS 112, Exams of Computer Science

Material Type: Exam; Class: Computation for the Sciences; Subject: Computer Science; University: Wellesley College; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 08/16/2009

koofers-user-wig
koofers-user-wig 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS112 Scientific Computation
Department of Computer Science
Wellesley College
Blowing a gasket
Classic recursive examples
Recursion 20-2
Recursion
Recursion solves a problem by
solving a smaller instance of
the same problem
Think divide, conquer, and glue,
where all of the subproblems
have the same “shape” as the
original problem
Recursion 20-3
Factorial
4! = 4 x (3 x 2 x 1)
=4 x 3!
2! = 2 x (1)
=2 x 1!
1! = 1
3! = 3 x (2 x 1)
=3 x 2!
recursive
cases
base
case
function result = fact (n)
if (n <= 1) % base case
result = 1;
else % recursive step
result = n * fact(n-1);
end
Recursion 20-4
The recursive leap of faith
To solve a problem recursively:
If it is simple enough
then solve it immediately
Otherwise
express solution in terms of
smaller, similar problem(s)
Initially this will take some
faith on your part
pf3

Partial preview of the text

Download Blowing a Gasket - Classic Recursive Example - Slides | CS 112 and more Exams Computer Science in PDF only on Docsity!

CS112 Scientific Computation

Department of Computer Science Wellesley College

Blowing a gasket

Classic recursive examples

Recursion 20-

Recursion

Recursion solves a problem by

solving a smaller instance of

the same problem

Think divide, conquer, and glue,

where all of the subproblems

have the same “shape” as the

original problem

Recursion 20-

Factorial

4! = 4 x (3 x 2 x 1)

= 4 x 3!

2! = 2 x (1)

= 2 x 1!

3! = 3 x (2 x 1)

= 3 x 2!

recursive

cases

base

case

function result = fact (n) if (n <= 1) % base case

result = 1; else % recursive step

result = n * fact(n-1); end

Recursion 20-

The recursive leap of faith

To solve a problem recursively:

If it is simple enough then solve it immediately Otherwise express solution in terms of smaller, similar problem(s)

Initially this will take some faith on your part

Recursion 20-

Fibonacci numbers, or multiplying rabbits?

Fibonacci numbers first appear around 500 B.C. in writings of a Sanscrit grammarian named Pingala who studied rhythm in language

Leonardo Fibonacci studied these numbers around 1200 in the context of multiplying rabbits

  • In the first month, there’s one newborn pair
  • Newborn pairs become fertile in their second month
  • Each month, every fertile pair begets a new pair
  • Rabbits never die

Recursion 20-

Fibonacci function

function result = fibonacci (n) if (n <= 2) % base case result = 1; else % recursive step result = fibonacci(n-1) + fibonacci(n-2); end

fib(5)

fib(4) fib(3)

Recursion 20-

Towers of Hanoi

Recursion 20-

A natural recursive solution

function towersOfHanoi (n, source, destination, spare) if (n == 1) % base case disp(['move disk 1 from ' source ' to ' destination]); else % recursive step towersOfHanoi(n-1, source, spare, destination); disp(['move disk ' num2str(n) ' from ' source ' to ' destination]); towersOfHanoi(n-1, spare, destination, source); end