Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

Fibonacci challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p120015

Psedo code:
1. if first two numbers return same
2. else call same fibonacci method by passing n-1 and n-2 and sum it up

public int fibonacci(int n) {
  
   if ((n == 0) || (n == 1)) // base cases
      return n;
    else
      // recursion step
      return fibonacci(n - 1) + fibonacci(n - 2);
  

}

Open in new window




I am passing all tests
Expected      Run            
fibonacci(0) → 0      0      OK      
fibonacci(1) → 1      1      OK      
fibonacci(2) → 1      1      OK      
fibonacci(3) → 2      2      OK      
fibonacci(4) → 3      3      OK      
fibonacci(5) → 5      5      OK      
fibonacci(6) → 8      8      OK      
fibonacci(7) → 13      13      OK      
other tests
OK      

How to improve/modify my design, code and any other alternate approaches. please advise
SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

Here's a graph of the continuous function (i.e., let n be a continuous real positive number) that I asked wolframalpha to plot.
 u(n) = [(1/2+sqrt(5)/2)^n - (1/2-sqrt(5)/2)^n]/sqrt(5)

Open in new window

User generated imagehttp://www.wolframalpha.com/input/?i=plot+%5B(1%2F2%2Bsqrt(5)%2F2)%5En+-+(1%2F2-sqrt(5)%2F2)%5En%5D%2Fsqrt(5),+where+n+%3D+1+to+8

When n is a positive integer, then u(n) is also a positive integer. Pretty amazing!
In practice, when computing, you probably won't get a positive integer, but a real number that is very close. To get the positive integer, you need to use a round function.
http://www.tutorialspoint.com/java/number_round.htm
As n gets larger, the speed at which you compute fibonacci(n) really rocks over the other approaches, whether recursive or just looping. However, if you want a table of the first N values of fibonacci(n), then just adding the previous two integers and storing it into an array of length N will be faster than using fibonacci(n) for all values of n = 1..N, where n is an integer.
Avatar of gudii9

ASKER

public int fibonacci(int n) {
  
   if ( n == 0 || n == 1) // base cases
      return n;
    else
      // recursion step
      return fibonacci(n - 1) + fibonacci(n - 2);
  

}

Open in new window


got rid of extra brackets. when i have to put and when i can get rid of brackets?
They make absolutely NO difference (in this case) to the execution of the code, if it is easy for you to read with them there, leave them there. That's all that it comes down to, how easy it is to read for YOU.
Yes +1 for what mccarl said - I actually think it's a little better with the extra brackets.  You should always prefer clarity and to me the extra with the extra braces is clearer.

Doug