Fibonacci

In mathematics, the Fibonacci numbers are the numbers in the following integer sequence. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.

F(n)= \left\{\begin{matrix}0,\\1,\\F(n-1) +F(n-2)\end{matrix}\right.\begin{matrix}\rightarrow n = 0\\\rightarrow n = 1\\\rightarrow n > 1\end{matrix}
package org.paingan.labs.logic;

public class Fibonacci {
public static long fib(int n) {
if (n < = 1) return n;
else return fib(n-1) + fib(n-2);
}

public static void main(String[] args) {
for (int i = 1; i <= 5; i++)
System.out.println(i + ": " + fib(i));
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s