Java EE Fibonacci Solution

Home Forums Java Java EE Fibonacci Solution

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #9380
    DigitalMirko
    Participant

    Java EE Servlet that calculates the nth Fibonacci number

    Input: int, Output: int

    F(0) = 1, F(1) = 1, F(n)=F(n-1) + F(n-2)

    package fibonacci;
    // Method created that calculates the nth Fibonacci number
    // Input: int,  Output: int
    // F(0) = 1, F(1) = 1, F(n)= F(n-1) + F(n-2)
    
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    
    @WebService 
    public class Fibonacci {
    
    	public void constructor(){	
    	}
    	@WebMethod
    	
    	public int fibonacci(int index){
    		int a = 1;
    		int b = 1;
    		for(int i=1; i<index; i++){
    			int temp = a + b;
    			a = b;
    			b = temp;			
    		}
    		return b;
    	}	
    }
    #9385
    daryl
    Participant

    This is the first time I saw the code the compute a Fibonacci numbers. I am amazed that it takes only that few lines.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.