Results 1 to 7 of 7

Math Help - Recursive algorithm

  1. #1
    Junior Member
    Joined
    Nov 2008
    Posts
    26

    Recursive algorithm

    Give a recursive algorithm for finding the sum of the first n positive integer.

    Procedure sum of integer ( n : positive integer )
    if n<= 0
    then sum of integer(n) := 0
    else sum of integer(n) := n+ sum of integer(n)

    is it correct or not ?? some one please help me ??
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Senior Member
    Joined
    Nov 2008
    From
    Paris
    Posts
    354
    if n<= 0
    then sum of integer(n) := 0
    else sum of integer(n) := n+ sum of integer(n)
    That should be
    else sum of integer(n+1) = sum of integer(n) + 1
    Follow Math Help Forum on Facebook and Google+

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

    Procedure sum of integer ( n : positive integer )
    if n<= 0
    then sum of integer(n) := 0
    else sum of integer(n) := n+ sum of integer(n)

    is it correct or not ?? some one please help me ??
    You have to have express sum_of_integer(n) in terms of sum_of_integer(n-1) so:

    Also as n is declared as a positive integer you cannot test if it is <=0

    Code:
    Procedure sum_of_integer ( n : positive integer )
      if n== 1
        then
           sum_of_integer := 1
        else
           sum_of_integer := 1+ sum_of_integer(n-1)
    CB
    Last edited by CaptainBlack; November 10th 2008 at 11:30 AM. Reason: to remove some of the confusion between return value and procedure call
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Junior Member
    Joined
    Nov 2008
    Posts
    26
    Thank you very much for your reply but i do not understand the logic

    Procedure sum_of_integer ( n : positive integer )
    if n== 1
    then
    sum_of_integer(n) := 1
    else
    sum_of_integer(n) := 1+ sum_of_integer(n-1)
    what is the value of sum_of_integer(n-1) ?? if we take n = 2 if you can describe little bit ?? appreciate your reply.

    Thanks
    Last edited by CaptainBlack; November 10th 2008 at 11:27 AM.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by bhuvan View Post
    Thank you very much for your reply but i do not understand the logic

    Procedure sum_of_integer ( n : positive integer )
    if n== 1
    then
    sum_of_integer(n) := 1
    else
    sum_of_integer(n) := 1+ sum_of_integer(n-1)
    what is the value of sum_of_integer(n-1) ?? if we take n = 2 if you can describe little bit ?? appreciate your reply.

    Thanks
    Well it is confusing as I retained your confusion of the name of the procedure (it should be a function by the way) and the name of the return value, try:
    Code:
    Function sum_of_integer ( n : positive integer )
      if n== 1
        then
            return 1
        else
            return 1+ sum_of_integer(n-1)
    CB
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Junior Member
    Joined
    Nov 2008
    Posts
    26
    Give a recursive algorithm for finding the maximum of the finite set of integer, making use of the fact that the maximum of n integer is the larger of the last integer in the list and the maximum of the first n-1 integer in the list.

    procedure largest(a1,a2,.....,an: integer)
    if n=1
    then largest (a1,a2,.....,an: integer) : = a1
    else largest(a1,a2,.....,an: integer) : = max(largest(a1,a2,.....,an-1),an)

    is the answer correct or not ?? if not please suggest the correct answer. appreciate your reply.

    Thanks
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by bhuvan View Post
    Give a recursive algorithm for finding the maximum of the finite set of integer, making use of the fact that the maximum of n integer is the larger of the last integer in the list and the maximum of the first n-1 integer in the list.

    procedure largest(a1,a2,.....,an: integer)
    if n==1
    then largest (a1,a2,.....,an: integer) : = a1
    else largest(a1,a2,.....,an: integer) : = max(largest(a1,a2,.....,an-1),an)

    is the answer correct or not ?? if not please suggest the correct answer. appreciate your reply.

    Thanks
    I think your pseudo-code use is not acceptable, something more like:

    Code:
    procedure largest(n:unsigned, a[1:n]: integer)
      if n=1
        then 
           return  a[1]
        else
           return  max(largest(n-1, a[1:n-1]),a[n])
    would be better.

    Alternativly can you give a link to a site that specified the syntax for your pseudo-code.

    CB
    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