Thread: what is recursion?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1

    Question what is recursion?

    What is recursion?

    For example, how can this series implemented with recursion?

    a=1/2+1/3+1/4+..........1/n.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well by definition its when repeated occurence. Recurrence in an equation like that would be the pattern just going on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on....... you get the picture, don't you?

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    double Divide(int num,int maxnum)
    {
      static double value;
      if (num>=maxnum) return value;
      value+=(1.0/(double)num);
      Divide(num+1,maxnum);
    }
    Haven't tested it, but pretty sure it will do what you want.
    You cannot go to infinite num in this function because of two reasons. It would be infinite loop, duh, and because your stack would overflow and crash the computer.


    I think this will execute 2^n-1 times, but not positive on that.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >a=1/2+1/3+1/4+..........1/n.

    This is a recursive relation which can be written as:

    Code:
           n
    a =  E 1/i
          i=2
    So you see the recursion step can be seen from:

    Code:
    n-1
     E 1/i + 1/n
    i=2
    And the end-step is i=n. This means that the function could be implemented as:

    Code:
    double serie (double i, double n)
    {
        if (i == n)
        {
            return 1/i;
        }
        else
        {
            return 1/i + serie (i+1, n);
        }
    }
    Note there is an more elegant way to write this function, but that's up to you. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 02:58 AM
  3. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  5. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM