- 70 Views
- Uploaded on
- Presentation posted in: General

Programming Appreciation Camp

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Programming Appreciation Camp

Session 2: Recursion

Steven Halim

NUS School of Computing

- In the first session, we have learnt/done some problem solving using algorithm:
- Algorithms have three control structures:
- Sequence
- Selection (branching)
- Repetition (loop)

- And Recursion

[Programming Appreciation Camp November 2008]

- A “Definition” in an English-English dictionary
- Recursion
- If you still don't get it, See: “Recursion”.

- Recursion
- Informally:
- Repeat same thing until some conditions are met.
- Seems like repetition/iteration/loop
- But more than that!

[Programming Appreciation Camp November 2008]

- Droste Effecthttp://en.wikipedia.org/wiki/Droste_effect
- Mathematical Definition
- Example: even number
- 2 is an even number
- If n is an even number, then n+2 too

- Example: even number
- Russian Doll
http://en.wikipedia.org/wiki/Matryoshka_doll

- Recursive Acronymshttp://en.wikipedia.org/wiki/Recursive_acronym
- GNU: GNU’s Not Unix
- VISA: VISA International Service Association

[Programming Appreciation Camp November 2008]

- Sierpinski Triangle

- Koch Snowflake
- Recursive Tree
- More Fractals
- http://en.wikipedia.org/wiki/Fractal

[Programming Appreciation Camp November 2008]

- Given this 9x9 2-D map
- L is Land
- W is Water

- Q: “How many lakes in the map?”
- Adjacent ‘W’s (N, E, S, W) belong to one lake.

- LLLLLLLLL
- LLWWLLWLL
- LWWLLLLLL
- LWWWLWWLL
- LLLWWWLLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LL2LL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLL33LL4L
- LL5L3LLLL
- LLLLLLLLL

The answer for this scenario: 5 lakes!

[Programming Appreciation Camp November 2008]

- Hard to get answer using standard iteration!
numOfW 0for each cell(i,j) in the map // (0,0) to (9,9) if (cell(i,j) is character ‘W’) numOfW numOfW + 1printf numOfW // W = 18 in this case

- LLLLLLLLL
- LLWWLLWLL
- LWWLLLLLL
- LWWWLWWLL
- LLLWWWLLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

- LLLLLLLLL
- LL12LL3LL
- L45LLLLLL
- L678L9ALL
- LLLBCDLLL
- LLLLLLLLL
- LLLEFLLGL
- LLHLILLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LL2LL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLL33LL4L
- LL5L3LLLL
- LLLLLLLLL

A=10, B=11, …, I = 18

So, how to answer: 5 lakes?

Use recursion!

This problem will be revisitedat the latter part of this session!

[Programming Appreciation Camp November 2008]

- Recursion is a programming paradigm!
- Complements sequence, selection, repetition

- Divide: break up a probleminto sub-problems of the same type!
- Conquer: Solve the problem with a functionthat calls itself to solve each sub-problem
- Somewhat similar to repetition, but not 100%
- Terminating condition: one or more of these sub-problems are so simple that they can be solved directly without further calls to the function

[Programming Appreciation Camp November 2008]

- Many computer algorithms can be expressed naturally in recursive form
- Trying to express these algorithms iterativelycan be actually confusing…
- e.g. “finding lakes” example

- LLLLLLLLL
- LLWWLLWLL
- LWWLLLLLL
- LWWWLWWLL
- LLLWWWLLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

[Programming Appreciation Camp November 2008]

- Function is simply defined as follow:
- output function_name(inputs) process the inputs return output

[Programming Appreciation Camp November 2008]

- Template for most recursive function
Recursive_Function_Name(Parameter_List)

// you will always see “selection statement” (if, switch, etc)

if (base_case) // problem is trivial, there can be more than one

do_something_simple // produce the result directly

else // recursive case

// Simplify the problem and then call this same function again

Recursive_Function_Name(Modified_Parameter_List)

- Modified_Parameter_List must bring the recursive function closer to (one of) the base case!

[Programming Appreciation Camp November 2008]

7 recursion examples to get you started

[Programming Appreciation Camp November 2008]

- Problem Description:
- We want to count down the space shuttle launch, e.g. 10, 9, 8, …, 3, 2, 1, BLAST OFF!!!
- Must use recursion!
- Although this can be done iteratively.

[Programming Appreciation Camp November 2008]

- Recursive version:
count_down(n) if (n <= 0) print “BLAST OFF!!!!” else print “Time = ” + ncount_down(n-1)

Base Case

occurs when n <= 0

If this happens, simply print “BLAST OFF!!!!”

Recursive Case

occurs when n > 0

If this happens, print the current time,

and then call a simpler problem:

count_down(n-1)

Note that this recursive function returns nothing

[Programming Appreciation Camp November 2008]

- Recursive version:
count_down(n) if (n <= 0) print “BLAST OFF!!!!” else print “Time = ” + ncount_down(n-1)

- Sample calls:
count_down(3) Time = 3Time = 2Time = 1BLAST OFF!!!!

- Iterative version:
count_down_itr(n) for (t=n to 0, decrement by 1) print “Time = ” + t print BLAST OFF!!!!”// actually the iterative// version is simpler// for this case

- Sample calls:
count_down_itr(3) Time = 3Time = 2Time = 1BLAST OFF!!!!

[Programming Appreciation Camp November 2008]

- Problem Description:
- fact(n), n!, product of 1 to n, is defined as:
- fact(n) = n * (n-1) * (n-2) * ... * 2 * 1, and
- fact(0) = 1
- fact(<0) is not defined
- This is an iterative paradigm!

- Using recursion, it can be defined as
- fact(n) = 1 if (n = 0) // simple sub-problem
- n * fact (n-1) if (n > 0) // calls itself

- fact(n), n!, product of 1 to n, is defined as:

[Programming Appreciation Camp November 2008]

- Recursive version:
fact(n) if (n = 0) return 1 else return n * fact(n-1)

Base Case

occurs when n = 0

If this happens, simply return 1, 0! = 1

Recursive Case

occurs when n > 0

If this happens, return n * result of simpler problem of fact(n-1)

- Recursive definition:
- fact(n) = 1 if (n = 0)
- n * fact (n-1) if (n > 0)

Note that this recursive function returns a number, which is the result of fact(n)

[Programming Appreciation Camp November 2008]

- Recursive version:
fact(n) if (n = 0) return 1 else return n * fact(n-1)

- Sample calls:
fact(0) 1 (base case)fact(1) 1*fact(0) = 1*1 = 1fact(2) 2*fact(1) = 2*1 = 2fact(3) 3*fact(2) = 3*2 = 6fact(4) 4*fact(3) = 4*6 = 24fact(5) 5*fact(4) = 5*24 = 120…see visual explanation

[Programming Appreciation Camp November 2008]

- Recursive version:
fact(n) if (n = 0) return 1 else return n * fact(n-1)

- Sample calls:
fact(0) 1 (base case)fact(1) 1*fact(0) = 1*1 = 1fact(2) 2*fact(1) = 2*1 = 2fact(3) 3*fact(2) = 3*2 = 6fact(4) 4*fact(3) = 4*6 = 24fact(5) 5*fact(4) = 5*24 = 120…

- Iterative version:
fact_itr(n) { result 1 for (i=1 to n, increment by 1) result result * i return result

- Sample calls:
fact_itr(0) 1fact_itr (1) 1fact_itr (2) 1*2 = 2fact_itr (3) 1*2*3 = 6fact_itr (4) 1*2*3*4 = 24fact_itr (5) 1*2*3*4*5 = 120…

[Programming Appreciation Camp November 2008]

- Problem Description:
- Fibonacci numbers are defined recursively:
- Fn =0if n = 0
- 1if n = 1
- Fn-1 + Fn-2if n > 1

- Fibonacci numbers are defined recursively:

[Programming Appreciation Camp November 2008]

- Recursive version:
fib(n) if (n <= 1) // for 0 and 1 return n else return fib(n-1) + fib(n-2)

Base Case

occurs when n <= 1 (0 or 1 only)

If this happens, simply return n (the value itself)

Recursive Case

occurs when n > 1

If this happens, return fib(n-1) + fib(n-2)

we call the same function twice!

- Recursive definition:
- Fn =0if n = 0
- 1if n = 1
- Fn-1 + Fn-2if n > 1

Here, we observe that a recursive function can call itself more than once! (branching recursion)

[Programming Appreciation Camp November 2008]

- Recursive version:
fib(n) if (n <= 1) // for 0 and 1 return n else return fib(n-1) + fib(n-2)

- Sample calls:
fib(0) 0 (base case)fib(1) 1 (base case) fib(2) fib(0)+fib(1) = 0+1 = 1fib(3) fib(1)+fib(2) = 1+1 = 2fib(4) fib(2)+fib(3) = 1+2 = 3fib(5) fib(3)+fib(4) = 2+3 = 5…

- Iterative version:
fib[0] 0fib[1] 1for (i=2 to n, increment by 1) fib[i] fib[i-1] + fib[i-2]

- Sample calls:
fib[0] = 0fib[1] = 1fib[2] = 1fib[3] = 2fib[4] = 3fib[5] = 5…

[Programming Appreciation Camp November 2008]

- Recursive version:
- Sample calls:
On my machine

fib(20) ~ 0.0 second fib(30) ~ 0.0 secondfib(35) ~ 1 secondfib(36) ~ 2 secondsfib(37) ~ 4 secondsfib(38) ~ 6 secondsfib(39) ~ 8 seconds fib(40) ~ 11 seconds

- Iterative version:
- Sample calls:
On my machine

fib(20) ~ 0.0 second fib(30) ~ 0.0 secondfib(35) ~ 0.0 second fib(36) ~ 0.0 second fib(37) ~ 0.0 second fib(38) ~ 0.0 second fib(39) ~ 0.0 second fib(40) ~ 0.0 second

[Programming Appreciation Camp November 2008]

- The Recursive Pattern of Fibonacci(5)
- Many repetitions…
- Slow!

Fib 5

Fib 3

Fib 4

Fib 1

Fib2

Fib 2

Fib 3

1

Fib 1

Fib 0

Fib 0

Fib1

Fib 1

Fib 2

1

0

0

1

1

Fib 0

Fib1

0

1

- Problem Description:
- Exponentiation, written as an,where a is called as “base”and n is called “the exponent”.
- When n >= 0, exponentiation is defined as:
- an = a * a * …. * a (n times)
- This is iterative definition

- What is the recursive formulation?
- an = __________________________
_____________

- an = __________________________

[Programming Appreciation Camp November 2008]

- Recursive version:
pow2(n) if (n = 0) return 1 else if (n = 1) return 2 else return 2 * pow2(n-1)

Base Case

occurs when n = 0 or n = 1

there can be >= 1 base case(s)

If this happens, return 1 for 20 or 2 for 21

Recursive Case

occurs when n > 1

If this happens, return 2 * pow2(n-1)

pow2(n-1) is a simplified problem

Here, we observe that a recursive function can have more than one base cases (processed differently)

[Programming Appreciation Camp November 2008]

- Recursive version:
pow2(n) if (n = 0) return 1 else if (n = 1) return 2 else return 2 * pow2(n-1)

- Sample calls:
pow2(1) 2 (base case)pow2(2) 2*pow2(1) = 2*2 = 4pow2(3) 2*pow2(2) = 2*4 = 8 pow2(4) 2*pow2(3) = 2*8 = 16 …

- Iterative version:
pow2_itr(n) result 1 for (i=1 to n, increment by 1) result result * 2 return result

- Sample calls:
pow2_itr(1) 1*2=2pow2_itr (2) 1*2*2=4pow2_itr (3) 1*2*2*2=8pow2_itr (4) 1*2*2*2*2=16

[Programming Appreciation Camp November 2008]

- Problem Description:
- Given a decimal number (base 10),as what we used in daily life…
- Print its digit line by line.
- e.g. 2008 is printed as:Answer (red lines):2008

[Programming Appreciation Camp November 2008]

- Recursive version:
digit(n) if (n > 0) digit(n / 10) print n % 10

Base Case

occurs when n <= 0

If this happens, do nothing!

Recursive Case

occurs when n > 0

If this happens, call print_digit with simplified problem: one less ‘digit’ (n / 10)

then, print n % 10. % is called themodulus (remainder) operator.

Here, we observe that base case can be as simple as doing nothing and we can still do something after recursive calls!

[Programming Appreciation Camp November 2008]

- Recursive version:
digit(n) if (n > 0) digit(n / 10) print n % 10

- Sample calls:
digit(2008)

2

0

0

8

Details in the next slide!

- Iterative version *:
digit_itr(n) while (n > 0) print n % 10 n n / 10

- Sample calls:
digit_itr(2008)

8

0

0

2

* This is not what we want…

* We need to reverse the answer!

[Programming Appreciation Camp November 2008]

- Recursive version:
digit(n) if (n > 0) digit(n / 10) print n % 10

- Sample calls:
digit(2008)

2

0

0

8

See diagram for clarity

[Programming Appreciation Camp November 2008]

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

Compute nCk or C(n,k)

nCk = n! / (k! * (n – k)!)

[Programming Appreciation Camp November 2008]

32

- How many ways to choose k out of n items?
- Enumerate the Base Cases
- When k < 0, e.g. (k=-1) out of (n=3)?
- 0, this problem is undefined for k < 0…

- When k > n, e.g. (k=4) out of (n=3)?
- 0, also undefined for k > n…

- When k == n, e.g. (k=3) out of (n=3)?
- 1, take all

- When k == 0, e.g. (k=0) out of (n=3)?
- 1, do not take anything

- When k < 0, e.g. (k=-1) out of (n=3)?

- Enumerate the Base Cases

[Programming Appreciation Camp November 2008]

- How many ways to choose k out of n items?
- Think about the General (recursive) Cases
- When 0 < k < n, e.g. (k=2) out of (n=3)?
- Consider the item one by one, e.g. item X!

- choose(n,k) =
- choose(n-1,k-1)The number of ways if I take X
- The problem becomes simpler:choosing k-1 (k=1) out of n-1 (n=2)

- plus choose(n-1,k)The number of ways if I do not take X
- The problem becomes simpler:choosing k (k=2) out of n-1 (n=2)

- choose(n-1,k-1)The number of ways if I take X

- When 0 < k < n, e.g. (k=2) out of (n=3)?

- Think about the General (recursive) Cases

[Programming Appreciation Camp November 2008]

- How many ways to choose k out of n items?

Recursive version only:

choose(n, k) if (k = 0 or k = n) return 1 else

return choose(n-1,k-1) + choose(n-1,k)

[Programming Appreciation Camp November 2008]

- How many ways to choose k out of n items?

This problem is not intuitive to be solved iteratively!

However, the recursive solution has many similar sub-problems (compare with Fibonacci)

Special:

Closed form:nCk = n!/(k!*(n-k)!)

6 in this case!

[Programming Appreciation Camp November 2008]

- Searching in Sorted Array, e.g. array A =
- Standard:
- Scan item one by one from left to right

- Can we do better?
- Yes, using a technique called binary search
- Idea: Narrow the search space by half (left/right side) successively until we find the item or until we are sure that the item is not in the sorted array.

[Programming Appreciation Camp November 2008]

- Recursive version (iterative not shown):
bsearch(arr, key, low, high) {if (high < low)return -1 // not found, base case 1

mid (low + high) / 2

if (arr[mid] > key)return bsearch(arr, key, low, mid-1) // recursive case 1: leftelse if (arr[mid] < key)return bsearch(arr, key, mid+1, high) // recursive case 2: rightelsereturn mid // found, base case 2

[Programming Appreciation Camp November 2008]

- Assume we have a sorted array A:
- Sample calls:
- bsearch(A, 7, 0, 9) // mid is 4, A[4] = 47, 7 < 47, go to left side
bsearch(A, 7, 0, 3) // mid is 1, A[1] = 7, 7 == 7, found it

return 1; // found in index 1

- bsearch(A, 47, 0, 9) // mid is 4, A[4] = 47, 47 == 47, found it
return 4; // found in index 4

- bsearch(A, 82, 0, 9) // mid is 4, A[4] = 47, 82 > 47, go to right side
bsearch(A, 82, 5, 9) // mid is 7, A[7] = 73 , 82 > 73, go to right side

bsearch(A, 82, 8, 9) // mid is 8, A[8] = 81, 82 > 81, go to right side

bsearch(A, 82, 9, 9) // mid is 9, A[9] = 99, 82 < 99, go to left side

bsearch(A, 82, 9, 8) // base case, not found

return -1; // not found

- bsearch(A, 7, 0, 9) // mid is 4, A[4] = 47, 7 < 47, go to left side

[Programming Appreciation Camp November 2008]

- A function that call itself
- With simpler sub-problem
- Can have one or more recursive cases

- Has simple/trivial sub-problems: base cases
- This is the terminating condition
- Can be as simple as “doing nothing”
- Can have one or more base cases

- Can solve certain problems naturally
- Can be slower than iterative counterparts
- This can be optimized though…

[Programming Appreciation Camp November 2008]

- Given this 9x9 2-D map
- L is Land
- W is Water

- Q: “How many lakes in the map?”
- Adjacent ‘W’s (N, E, S, W) belong to one lake.

- LLLLLLLLL
- LLWWLLWLL
- LWWLLLLLL
- LWWWLWWLL
- LLLWWWLLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LL2LL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLL33LL4L
- LL5L3LLLL
- LLLLLLLLL

The answer for this scenario: 5 lakes!

[Programming Appreciation Camp November 2008]

- How to connect adjacent (N, E, S, W) ‘W’s?
- The idea: Analogy of real-life “water”!
- If we pour water to coordinate (1,2),
- The water will fill the area indicated with ‘1’s!

- But how to do this? (flooding the lakes?)

- LLLLLLLLL
- LLWWLLWLL
- LWWLLLLLL
- LWWWLWWLL
- LLLWWWLLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LLWLL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

[Programming Appreciation Camp November 2008]

- Flood Fill Recursive Algorithm
floodfill(r, c, ID) if (r < 0 or r > 9 or c < 0 or r > 9) return // base case 1: exceeding boundary if (cell(r,c) is not character ‘W’) return // base case 2: not water cell(r,c) ID // simplify the problem W to ID: 1 ‘W’ less// recursively fill the 4 directions floodfill(r, c+1, ID) // east floodfill(r-1, c, ID) // north floodfill(r, c-1, ID) // west floodfill(r+1, c, ID) // south

- Sample calls:
floodfill(1,2,‘1’) // fill lake starting from (1,2) with ‘1’

- Sample calls:

- LLLLLLLLL
- LLWWLLWLL
- LWWLLLLLL
- LWWWLWWLL
- LLLWWWLLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LLWLL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

[Programming Appreciation Camp November 2008]

- Now, Use floodfillto flood each lake,then increment the lake counter!

lake 0for each cell(i,j) in the map if (cell(i,j) is character ‘W’)floodfill(i, j, ‘1’+lake) lake lake + 1printf lake // 5 in this case

- LLLLLLLLL
- LLWWLLWLL
- LWWLLLLLL
- LWWWLWWLL
- LLLWWWLLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LLWLL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LL2LL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LL2LL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLL33LLWL
- LLWL3LLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LL2LL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLL33LL4L
- LLWL3LLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LL2LL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLL33LL4L
- LL5L3LLLL
- LLLLLLLLL

[Programming Appreciation Camp November 2008]

- It is a programming paradigm:
- Divide and Conquer

- Many data structures are recursively defined
- Linked List, Tree or Heap: these data structures and their operations are naturally recursive, etc

- Many algorithms are naturally recursive
- Sorting: Merge sort and Quick sort are recursive
- Top-Down Dynamic Programming, etc

- We do not discuss them now,but you may explore on your own

[Programming Appreciation Camp November 2008]

7 more recursion examples that we will discuss together

[Programming Appreciation Camp November 2008]

- Modify recursive function floodfill(r,c,ID)so that it is able to go to 8 directions(N, NE, E, SE, S, SW, W, NW)!
- Given the same map,there will be just 4 lakes this time:look at lake number 3!

- LLLLLLLLL
- LLWWLLWLL
- LWWLLLLLL
- LWWWLWWLL
- LLLWWWLLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LLWLL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LL2LL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLLWWLLWL
- LLWLWLLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LL2LL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLL33LLWL
- LL3L3LLLL
- LLLLLLLLL

- LLLLLLLLL
- LL11LL2LL
- L11LLLLLL
- L111L11LL
- LLL111LLL
- LLLLLLLLL
- LLL33LL4L
- LL3L3LLLL
- LLLLLLLLL

[Programming Appreciation Camp November 2008]

- Create pow(a,n) to compute an!
- Hint: modify pow2(n) a bit
- Sample calls:
- pow(3,3) = 27
- pow(4,2) = 16
- pow(5,3) = 125
- pow(7,3) = 343

[Programming Appreciation Camp November 2008]

- Create fast_pow(a,n) to compute anwith a better recursive formulation:
- Insight: a8 a8 = a4*a4; a4 = a2*a2; and a2 = a*a
- Only 3 multiplications to compute a8!
- Logarithmic growth: log2 8 = 3
- Special case: Odd exponent: a7=a*a6

- Sample calls:
- fast_pow(3,3) = 27
- fast_pow(4,2) = 16
- fast_pow(5,3) = 125
- fast_pow(7,3) = 343

[Programming Appreciation Camp November 2008]

- First documented algorithm by Greek mathematician Euclid in 300 B.C.
- To compute the GCD (greatest common divisor) of 2 integers.

- Let A and B be integers with A > B ≥ 0.
- If B = 0, then the GCD is A and algorithm ends.
- Otherwise, find q and r such that
- A = q.B + r where 0 ≤ r < B
- Note that we have 0 ≤ r < B < A and GCD(A,B) = GCD(B,r).
- Replace A by B, and B by r. Go to step 2.

[Programming Appreciation Camp November 2008]

- Create greatest common divisor function:gcd(a,b) according to:
- gcd(a,b) = aif (b == 0)gcd(b,a%b)otherwise
- Sample calls:
- gcd(5,2) = 1, i.e., common divisor of 5 and 2 is 1
- gcd(6,2) = 2
- gcd(3,12) = 3
- gcd(15,25) = 5

[Programming Appreciation Camp November 2008]

A word is a palindrome if it reads the same forward and backward

How do you determine if a word is a palindrome?

Recursively!

Sample calls:

Palindrome(“”) = false

Palindrome(“O”) = true

Palindrome(“OO”) = true

Palindrome(“NOON”) = true

Palindrome(“D”) = true

Palindrome(“ADA”) = true

Palindrome(“RADAR”) = true

[Programming Appreciation Camp November 2008]

52

North-east (NE) path: you may only move northward or eastward

Find the number of north-east pathsbetween A and C.

Recursively!

C

A

[Programming Appreciation Camp November 2008]

53

Given this list of coin denominations: $1, 50 cents, 20 cents, 10 cents, 5 cents, 1 cent, find the smallest number of coins needed for a given amount. You do not need to list out what coins are used.

Example 1: For $3.75, 6 coins are needed.

Example 2: For $5.43, 10 coins are needed.

Recursively!

[Programming Appreciation Camp November 2008]

54

- That’s all for today
- Any questions?
- Contact:
- Steven Halim
- [email protected]
- http://www.comp.nus.edu.sg/~stevenha/myteaching

- Steven Halim

[Programming Appreciation Camp November 2008]

[Programming Appreciation Camp November 2008]