Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Recursion ouput is not coming for value more than 40.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Recursion ouput is not coming for value more than 40.

    Hello All,

    I have written following code using recursion.It is working however it is not giving me ouput for value 50.Infact nothing is display when I enter number greater than 40.

    import java.util.Scanner;
     
    public class Test {
     
     
    	public static void main(String[] args) {
     
    		Scanner in =new Scanner(System.in);
    		System.out.println("Enter number");
    		int n = in.nextInt();
     
    		System.out.println(Mymethod(n));
     
    	}
     
     
     
    public static long Mymethod(long n){
     
    	if(n==0){
     
    		return 0;
    	}
     
    	else if(n==1){
     
    		return 1;
    	}
     
       else if(n==2){
     
    		return 1;
    	}
    	else{
     
    	return (Mymethod(n-1))+ (Mymethod(n-2))+ (Mymethod(n-3));
     
    	}
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: Recursion ouput is not coming for value more than 40.

    Have you waited long enough for the program to finish?

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion ouput is not coming for value more than 40.

    Hi..Thank you for reply but I waited for around 20 min.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: Recursion ouput is not coming for value more than 40.

    Your algorithm must be doing a lot of recursive calling.
    Where did you get the code? Did it give an expression or description of the number of calls it makes.

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion ouput is not coming for value more than 40.

    I have written this code.NO there is no description.Expression was

    f(n) =f(n-1)+f(n-2)+f(n-3)

    Base case was f(0)=1,f(1)=2,f(2)=1

    Question was to write code in java or any object oriented language to return nth number from given expression.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: Recursion ouput is not coming for value more than 40.

    Have you tried an iterative approach vs a recursive one?

    Your code doesn't follow the equations:

    f(1)=2

    else if(n==1){

    return 1;

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion ouput is not coming for value more than 40.

    Is there anything wrong in this code?

  8. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion ouput is not coming for value more than 40.

    oh sorry no,it is f(1) =1 only.
    sorry for that typing mistake.

    I have not yet tried iterative way.

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion ouput is not coming for value more than 40.

    My computer is very slow.Is it running at your end for value 50?

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Recursion ouput is not coming for value more than 40.

    Please do not post the same question twice to the forums, and please use the code tags. I've locked your other post, and am providing a link to said post as it has my reply - you must realize how many calls your code makes with a value of 50 (we're talking trillions +) which don't happen in the blink of an eye

    Hi Recursion method in java does not give output for big values.
    Last edited by copeg; July 7th, 2011 at 03:50 PM.

  11. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion ouput is not coming for value more than 40.

    @ copeg:Thanks for reply.I registered today here so sorry about that.

    Yes.. I waited around 20 min..Can I write this code using recursion better way?efficient?

  12. #12
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion ouput is not coming for value more than 40.

    there is no exception coming..

  13. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Recursion ouput is not coming for value more than 40.

    Can I write this code using recursion better way?efficient?
    Yes. Draw out how the algorithm actually works. Start from a given value, lets say 5. There is a better way to do it than like this, but here's a start:

    5 recursively computes f(4), f(3), f(2).
    ->4 recursively computes f(3), f(2), f(1).
    ->3 recursively computes f(2), f(1), f(0).

    So here's the BIG hint: notice how values are calculated more than once a) think about how this would be impacted with larger starting values. Is there really a need to recalculate what you've already calculated? b) if you understand the above, do you have ideas for reducing redundancy?

    Edit: And suggested reading: http://en.wikipedia.org/wiki/Memoization
    Last edited by copeg; July 7th, 2011 at 06:55 PM.

Similar Threads

  1. Recursion Help
    By WeaKeN in forum Algorithms & Recursion
    Replies: 10
    Last Post: May 27th, 2011, 08:17 AM
  2. Output not coming through properly
    By Camiot in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 16th, 2011, 09:25 PM
  3. Null value coming from Database
    By kosuri.dilip in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 1st, 2011, 02:03 PM
  4. JFrames not coming up?
    By scooty199 in forum AWT / Java Swing
    Replies: 13
    Last Post: October 30th, 2010, 02:54 AM
  5. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 10:50 PM