Results 1 to 5 of 5

Thread: Recursion

  1. #1
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default Recursion

    Here's a sample recursive method for Fibonacci numbers:

    Java Code:
     public int fibonacci(int n)
        {
        	if(n == 1 || n == 2) // The reason why I did it this way was because I made the recursive call two different ways so I made it                                
    //clear with the ending statement that there was two different ways this was being called
        	{
        		return 1;
        	}
        	else
        	{
        		return fibonacci(n - 1) + fibonacci(n - 2);
        	}
        }
    Here's a sample recursive algorithms to find a N x N board that has N queens on it where no of the queens can attack each other.

    Java Code:
    public void nQueens(int i) 
        {
            if (i == n)
            {
            	return solution;
           	}
            else 
            {
                for (int k = 0; k < n; k++) 
                {
                    col[i] = k;
                    if (isPromising(i))
                    {
                   		nQueens(i+1);
                   	}
                }
            }
        }
        
        private boolean isPromising(int n) 
        {
            for (int i = 0; i < n; i++) 
            {
                if (col[i] == col[n])             
                	return false;   
                if (Math.abs((col[i] - col[n])) == (n - i)) 
                	return false;   
            }
            return true;
        }
    Last edited by Zosden; 05-05-2008 at 07:14 AM. Reason: To answer a question
    My IP address is 127.0.0.1

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Eranga, I'm afraid I'm not sure what the point of your post was... care to elaborate?
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yep, I always try to write more clear codes. Then anyone looking at easy to understand what's going on there.

    Do you think it's bad? :)

  5. #5
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    The way I posted it was very clear. Since I'm recursively calling it two different ways, I should have to stopping points. The way you tried to edit it isn't has clear because it doesn't tell the reader what values that should stop the call.
    My IP address is 127.0.0.1

Similar Threads

  1. Help With Recursion
    By andrew777 in forum New To Java
    Replies: 1
    Last Post: 03-29-2008, 12:51 PM
  2. recursion
    By kdeighan in forum New To Java
    Replies: 3
    Last Post: 01-25-2008, 09:48 PM
  3. Recursion
    By bozovilla in forum Advanced Java
    Replies: 3
    Last Post: 01-07-2008, 04:53 PM
  4. recursion
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 05:15 PM
  5. Help with recursion
    By scts102 in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 10:07 PM

Posting Permissions

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