Results 1 to 8 of 8

Thread: Recursion

  1. #1
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default Recursion

    I need some help writing a recursive method to add the first n terms of the series

    1+(1/2)+(1/3)+(1/4)+.....+(1/n)

    I'm thinking I would have to isolate the 1 because that is independent of the other terms(?) and keep passing in a number 1 higher than the previous time, but im not sure.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by fam2315 View Post
    I need some help writing a recursive method to add the first n terms of the series

    1+(1/2)+(1/3)+(1/4)+.....+(1/n)

    I'm thinking I would have to isolate the 1 because that is independent of the other terms(?) and keep passing in a number 1 higher than the previous time, but im not sure.
    Write down your series as: 1/1+1/2+1/3+1/i+1/(i+1)+1/(i+2) ... 1/n. I hope see can see that i is the loop variable in the range from 1 to n, so:

    Java Code:
    double sum= 0;
    for (int i= 1; i <= n; i++)
       sum+= 1.0/i;
    This is an iterative solution, but imagine that a (recursive) method F(i) can calculate the sum 1/i+1/(i+1)+1/(i+2)+ ... +1/n. F can be written as:

    Java Code:
    double F(int i, int n) {
       if (i > n) return 0;
       return 1.0/i+F(i+1, n);
    }

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    I think this should do it:

    Java Code:
    	
    public static double addSeries(double n) {
    	
    		if (n == 1)
    			return 1;
    		
    		return (1/n) + addSeries(n-1);
    				
    }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That looks correct, did you test it? Also, if you are done, please mark your thread solved with the thread tools at the top.

  5. #5
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    I tested my method. It seems to work fine. Who should mark this solved? I'm sorry, I'm new. Thanks.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can, at the top there should be something that says thread tools, click it and it will open a drop down menu, the last item on it should be mark the thread solved.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    pssst, sunde, you are not talking to the OP.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  2. Recursion
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 02:26 AM
  3. Recursion
    By jachandru in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:52 PM
  4. Recursion
    By Mika in forum New To Java
    Replies: 5
    Last Post: 01-04-2009, 01:13 AM
  5. Please help with recursion
    By pheonix in forum New To Java
    Replies: 9
    Last Post: 12-27-2008, 11:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •