07_recursion - Overview 2.3 Recursion What is recursion?...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 2.3 Recursion 2 Overview What is recursion? When one function calls itself directly or indirectly. Why learn recursion? New mode of thinking. Powerful programming paradigm. Many computations are naturally self-referential. Mergesort, FFT, gcd. Linked data structures. A folder contains files and other folders. Closely related to mathematical induction. Reproductive Parts M. C. Escher, 1948 3 Greatest Common Divisor Gcd. Find largest integer that evenly divides into p and q. Ex. gcd(4032, 1272) = 24. Applications. Simplify fractions: 1272/4032 = 53/168. RSA cryptosystem. 4032 = 2 6 × 3 2 × 7 1 1272 = 2 3 × 3 1 × 53 1 gcd = 2 3 × 3 1 = 24 4 Greatest Common Divisor Gcd. Find largest integer that evenly divides into p and q. Euclid's algorithm. [Euclid 300 BCE] gcd(4032, 1272) = gcd(1272, 216) = gcd(216, 192) = gcd(192, 24) = gcd(24, 0) = 24. gcd( p , q ) = p if q = 0 gcd( q , p % q ) otherwise base case reduction step, converges to base case 4032 = 3 × 1272 + 216
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 5 Greatest Common Divisor Gcd. Find largest integer d that evenly divides into p and q. p p % q q x x x x x x x x p = 8x q = 3x gcd(p, q) = x q gcd gcd( p , q ) = p if q = 0 gcd( q , p % q ) otherwise base case reduction step, converges to base case 6 Greatest Common Divisor Gcd. Find largest integer d that evenly divides into p and q. Java implementation. base case reduction step public static int gcd ( int p , int q ) { if ( q == 0 ) return p ; else return gcd ( q , p % q ); } gcd( p , q ) = p if q = 0 gcd( q , p % q ) otherwise base case reduction step, converges to base case Recursive Graphics 8
Background image of page 2
3 9 10 Htree H-tree of order n. Draw an H. Recursively draw 4 H-trees of order n-1, one connected to each tip. and half the size order 1 order 2 order 3 tip size 11 Htree in Java public class Htree { public static void draw ( int n , double sz , double x , double y ) { if ( n == 0 ) return ; double x0 = x - sz / 2 , x1 = x + sz / 2 ; double y0 = y - sz / 2 , y1 = y + sz / 2 ; } } draw the H, centered on (x, y) recursively draw 4 half-size Hs 12 20% 40% 60% 80% 100% Animated H-tree Animated H-tree. Pause for 1 second after drawing each H.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 13 Towers of Hanoi http://en.wikipedia.org/wiki/Image:Hanoiklein.jpg
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/21/2009 for the course COMPUTER computer 1 taught by Professor Abedauthman during the Fall '08 term at Aarhus Universitet.

Page1 / 9

07_recursion - Overview 2.3 Recursion What is recursion?...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online