Results 1 to 5 of 5

Math Help - Recursive Algorithm

  1. #1
    Banned
    Joined
    Jan 2009
    Posts
    58

    Recursive Algorithm

    How do I find a recursive algorithm for finding the sum of the first n positive integers?

    procedure sum of integers (n: positive integer)
    if n:=1
    then sum:=1
    else sum:=n+ sum of (n-1)

    That's what I have so far, but it still doesn't make sense to me.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Rhymes with Orange Chris L T521's Avatar
    Joined
    May 2008
    From
    Chicago, IL
    Posts
    2,844
    Thanks
    3
    Quote Originally Posted by jennifer1004 View Post
    How do I find a recursive algorithm for finding the sum of the first n positive integers?

    procedure sum of integers (n: positive integer)
    if n:=1
    then sum:=1
    else sum:=n+ sum of (n-1)

    That's what I have so far, but it still doesn't make sense to me.

    You can do this (modify the code to the proper syntax you need):

    Code:
    ...
    sum = 0;    // initialize sum
    count = 1;  // initialize counter
    while count <= n do   
       sum = sum + count;  
       count = count + 1;
    end do;
    return sum;
    ...
    Does this make sense?
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Banned
    Joined
    Jan 2009
    Posts
    58

    Confused

    I haven't anything in my textbook like that. I am sure it is correct, but I think I would need to simplify it to use just the terms in my textbook. Are you saying what I already have is correct? oor does it need more? THANKS!
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Super Member
    Joined
    Mar 2008
    Posts
    934
    Thanks
    33
    Awards
    1
    Chris, your algorithm is correct but is not recursive, which appears to be a requirement of the assignment based on the OP. (I could be wrong about this.)

    So here is a recursive version, in no particular programming language--

    Code:
    function f(n)
      if n = 0 then
        return 0
      else
        return n + f(n-1)
    Jennifer, you had the right idea in your original post, you just need to make your algorithm a little more programming-like. I generalized your algorithm a little by allowing for a sum of the first 0 integers; the sum is 0.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Super Member PaulRS's Avatar
    Joined
    Oct 2007
    Posts
    571
    Quote Originally Posted by Chris L T521 View Post
    You can do this (modify the code to the proper syntax you need):

    Code:
    ...
    sum = 0;    // initialize sum
    count = 1;  // initialize counter
    while count <= n do   
       sum = sum + count;  
       count = count + 1;
    end do;
    return sum;
    ...
    Does this make sense?
    That's an iterative algorithm. You repeat an operation as many times as needed, with a loop like 'repeat', 'while' or 'for'.
    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: 6
    Last Post: November 10th 2008, 10:44 PM
  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