Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS1110 Lec. 11 5 Oct 2010 More on Recursion We develop recursive functions and look at execution of recursive functions Study Sect 15.1, p. 415. Watch.

Similar presentations


Presentation on theme: "1 CS1110 Lec. 11 5 Oct 2010 More on Recursion We develop recursive functions and look at execution of recursive functions Study Sect 15.1, p. 415. Watch."— Presentation transcript:

1 1 CS1110 Lec. 11 5 Oct 2010 More on Recursion We develop recursive functions and look at execution of recursive functions Study Sect 15.1, p. 415. Watch activity 15-2.1 on the CD. In DrJava, write and test as many of the self-review exercises as you can (disregard those that deal with arrays). Thursday 7:30pm prelim: A-K Olin 155, L-Z Olin 255 Wednesday 3:35 lab is less crowded; go there instead?

2 2........... while there is room A draws or ; B draws or ; A wants to get a solid closed curve. B wants to stop A from getting a solid closed curve. Who can win? What strategy to use?..... Board can be any size: m by n dots, with m > 0, n > 0 A won the game to the right because there is a solid closed curve. A game A and B alternate moves

3 3 /** = non-negative n, with commas every 3 digits e.g. commafy(5341267) = “5,341,267” */ public static String commafy(int n) { } What is the base case? A: 0..1 B: 0..9 C: 0..99 D: 0..999 E: 0..9999

4 4 Executing recursive function calls. /** = non-negative n, with commas every 3 digits e.g. commafy(5341267) = “5,341,267” */ public static String commafy(int n) { 1: if (n < 1000) 2: return “” + n; // n >= 1000 3: return commafy(n/1000) + “,” + to3(n%1000); } /** = p with at least 3 chars — 0’s prepended if necessary */ public static String to3(int p) { if (p < 10) return “00” + p; if (p < 100) return “0” + p; return “” + p; } n commafy: 1 Demo commafy(5341266 + 1)

5 5 Recursive functions /** = b c. Precondition: c ≥ 0*/ public static int exp(double b, int c) Properties: (1)b c = b * b c-1 (2)For c even b c = (b*b) c/2 e.g 3*3*3*3*3*3*3*3 = (3*3)*(3*3)*(3*3)*(3*3)

6 6 Recursive functions /** = b c. Precondition: c ≥ 0*/ public static int exp(double b, int c) { if (c == 0) return 1.0; if (c is odd) return b * exp(b, c–1); // c is even and > 0 return exp(b*b, c / 2); } c number of calls 0 1 1 2 2 4 3 8 4 16 5 32 6 2 n n + 1 32768 is 2 15 so b 32768 needs only 16 calls!

7 7 Binary arithmetic Decimal Binary OctalBinary 00 00 00 2 0 = 1 1 01 01 01 2 1 = 2 10 02 10 02 2 2 = 4 100 03 11 03 2 3 = 8 1000 04 100 042 4 = 1610000 05 101 05 2 5 = 32100000 06 110 062 6 = 641000000 07 111 07 2 15 = 327681000000000000000 08 1000 10 09 1001 11 10 1010 12 Test c odd: Test last bit = 1 Divide c by 2: Delete the last bit Subtract 1 when odd: Change last bit from 1 to 0. Exponentiation algorithm processes binary rep. of the exponent.

8 8 Hilbert’s space-filling curve Hilbert(1): Hilbert(2): Hilbert(n): H(n-1) left As the size of each line gets smaller and smaller, in the limit, this algorithm fills every point in space. Lines never overlap. H(n-1) dwn H(n-1) right All methods used in today’s lecture will be on course website

9 9 Hilbert’s space-filling curve


Download ppt "1 CS1110 Lec. 11 5 Oct 2010 More on Recursion We develop recursive functions and look at execution of recursive functions Study Sect 15.1, p. 415. Watch."

Similar presentations


Ads by Google