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

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

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

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

View Full Document
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
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.

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

View Full Document
4 13 Towers of Hanoi http://en.wikipedia.org/wiki/Image:Hanoiklein.jpg
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern