Results 1 to 2 of 2

Math Help - Recursive Algorithm

  1. #1
    Newbie
    Joined
    Jul 2008
    Posts
    4

    Recursive Algorithm

    Question: Give a recursive algorithm for finding the sum of the first n positive integers.

    procedure sum(n: nonnegative integers)
    if n = 0 then sum(n):=1
    else sum(n) := sum(??)

    I am not sure how to find the sum of the previous integer and add it to the next. Any help is greatly appreciated!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Ramin_Shahab View Post
    Question: Give a recursive algorithm for finding the sum of the first n positive integers.

    procedure sum(n: nonnegative integers)
    if n = 0 then sum(n):=1
    else sum(n) := sum(??)

    I am not sure how to find the sum of the previous integer and add it to the next. Any help is greatly appreciated!
    Code:
    function sum(n)
     
      if n==1
         return 1
      else
         return n+sum(n-1)
      endif
     
    endfunction
    RonL
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. recursive algorithm
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: November 23rd 2009, 10:22 AM
  2. Recursive Algorithm
    Posted in the Discrete Math Forum
    Replies: 4
    Last Post: January 6th 2009, 01:55 AM
  3. Recursive Algorithm
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: November 9th 2008, 10:56 PM
  4. Recursive algorithm
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: November 6th 2008, 04:50 AM
  5. Recursive algorithm
    Posted in the Discrete Math Forum
    Replies: 7
    Last Post: May 21st 2007, 03:15 PM

Search Tags


/mathhelpforum @mathhelpforum