ShareThis

Sunday, August 12, 2012

[java] One line Fibonacci Sequence Code!

Here is a cool recursive solution to the fibonacci sequence in one line! However, runtime is O(2^n)... so iterative solution is the better route to take for performance.

int fib(int n){
  return n <= 2 ? 1: fib(n-1) + fib(n-2);
}

0 comments:

Post a Comment