1 / 26

Recursion a Wonderful Thing!

Recursion a Wonderful Thing!. By: Mr. Simpson. Introduction. Hi! So you're all set to dive into recursive programming. Well, the following presentation is about the basics. The definition. The meaning. And a small simple example.

noreen
Download Presentation

Recursion a Wonderful Thing!

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion a Wonderful Thing! By: Mr. Simpson

  2. Introduction • Hi! So you're all set to dive into recursive programming. Well, the following presentation is about the basics. The definition. The meaning. And a small simple example. • I assume that you have a fair knowledge of one of the languages: C, C++, Pascal, Java etc. which supports recursion.

  3. Formalities • Meaning of Recursion. You may skip it if you already know what the concept is. • Recursion • In normal procedural languages, one can go about defining functions and procedures, and 'calling' these from the 'parent' functions. I hope you already know that. Some languages also provide the ability of a function to call itself. This is called Recursion.

  4. Factorial! • Factorial is a mathematical term. • Factorial of a number, say n, is equal to the product of all integers from 1 to n. Factorial of n is denoted by n! = 1x2x3...x n. Eg: 10! = 1x2x3x4x5x6x7x8x9x10

  5. Factorial! • The simplest program to calculate factorial of a number is a loop with a product variable. • The pseudocode(sample code that isn’t really code is as follows: • Take in a variable n from the function. • Create a counter I • While I less than or (less than or equal to?) n do • temp = temp * I; • Increment I; • Return temp;

  6. Factorial : While Structure int factorial(int n){ int temp=1; int i=1; while (i<=n){ temp = temp*I; //multiply I to the original of temp. i = i+1;//increment the counter; } }

  7. Factorial : Recursive • The while loop is great but is at times cumbersome code yet easy to read! • Instead, it is possible to give a recursive definition for Factorial as follows: • If n=1, then Factorial of n = 1 • Otherwise, Factorial of result = product of n and Factorial of (n-1)

  8. What is happening here? Factorial 3 is being calculated Check it out it works!!! The following code example is an implementation of the psuedocode described earlier. Pictorial Example Factorial

  9. Factorial Pseudocode • Recall Factorial(5) is: 1x2x3x4x5 • Factorial(n) is as follows: • If n=1, then Factorial of n = 1 • Otherwise(else), Factorial of result = product of n and Factorial of (n-1)

  10. Factorial : Code Example • The following code fragment (in C) depicts Recursion at work.Try it out! public int Factorial(int n) { if (n==1) return 1; else return Factorial(n-1) * n; }

  11. Recursion: End Condition • End Condition: when creating a recursive function is to give an 'end-condition' • The reason is that we don’t want the function to go on FOREVER!!!!! • When we have the final condition we have a way to terminate the recursion

  12. Recursion: End Condition • One of the simplest is by means of an 'if condition' statement, as above. In the example below , the recursion stops when n reaches 1. public int Factorial(int n) { //termination statement is when n=1; if (n==1) return 1; else //otherwise return the factorial of n-1 times n return Factorial(n-1) * n; }

  13. Recursion: Cloning!!!! • Imagination is a very hard thing. Imagination of Recursion is all the more tricky. • Think of clones. • Say you have a machine to make clones of yourself, and (for lack of a better pass-time) decide to find the factorial of a number, say 10

  14. Factorial : Concrete Example • Now we’ll use that cloning machine and be smart about it so we can calculate the factorial of 10. • So, being smart, this is what you do: • First, there's only You. Let's call you You-1. You have the number 10 in your pocket. Being smart, you know that all you need to find the factorial of 10 (10*9*...*2*1) is to somehow obtain the value of 9 factorial (9*8*..*2*1),

  15. Factorial : Concrete Example • There you are sitting quietly thinking….Ah ha..Make a clone! • So that's what you do. You turn on your machine and out pops a clone! • You give the clone You-2 strict instructions to find the factorial of 9 and make it quick! Your job is done for a while, so you (You-1) stretch on your sofa sipping on your lemonade. Meanwhile...

  16. Factorial : Concrete Example • You-2 is (you guessed it) just as smart as you! He tucks his number (9) into his pocket, turns on the machine, and out pops a clone (You-3). The new clone is given the job of 8-factorial, which it proceeds to do while (unbeknownst to you) You-2 is sipping on his own glass of lemonade on his own sofa. And so the story goes on until finally one fine day...

  17. Factorial : Concrete Example • Out pops You-10 who is given strict instructions (by You-9) to get the factorial of 1. Now, You-10, being just as smart as any of the other you's, knows very well that the factorial of 1 is... 1.

  18. Factorial : Concrete Example • So he says to You-9 (who was just about to doze off on his sofa), "Here's your factorial of 1." • You-9 snatches the result from his subordinate You-10, takes out his plasma gun, and zaps You-10 out of existence.

  19. Factorial : Concrete Example • "Heh, heh, heh" he thinks, and goes to his boss, You-8, saying,"Here's your factorial of 2..." ...blah...blah... and finally You-2 wakes you up from your slumber, and says to you, "Here's your factorial of 9" You zap him off, multiply by the 10 in your pocket, and There You Have It !! Now, wasn't that simple?

  20. Weekend Homework! • Write a function using Recursion to print numbers from n to 1.

  21. Today’s Homework • Write a function using Recursion to print numbers from 0 to n. (You just need to shift one line in the program of problem 1.)

  22. Today’s Homework • Write a function using Recursion to enter and display a string in reverse. Use the charAt method in the class String.

  23. Today’s Homework • Write a function using Recursion to enter and display a string in reverse and state whether the string contains any spaces. Use the charAt method in the String class.

  24. Today’s Homework • Write a function using Recursion to check if a number n is prime. (You have to check whether n is divisible by any number below n)

  25. Today’s Homework • Write a function using Recursion to enter characters one by one until a space is encountered. The function should return the depth at which the space was encountered.

  26. Resources • http://personal.vsnl.com/erwin/recintro.htm • http://pages.britishlibrary.net/patgardiner/images/Mushroom%20Cloud%203.gif

More Related