fibonacci sequence java
/*
Author: Jeffrey Huang
This finds the fibonacci number using a term provided by a user
A recursive method is used.
b)
9 calls are made to the fibonacci sequence in order to compute the 5th term.
*/
import java.io.*;
public class Fibonacci_JeffreyHuang
{
//static int counter=0;
public static long Fibonacci (long n)
{
//count++;
if (n == 1 || n == 2)
{
return 1;
}
else if (n <= 0)
{
System.out.println ("Error: There is no term less than 1");
return 0;
}
else
{
return (Fibonacci (n - 1) + Fibonacci (n - 2));
}
}
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String sentinel = "yes";
long fib_term = 0;
boolean invalid;
while (sentinel.equalsIgnoreCase ("yes") || sentinel.equalsIgnoreCase ("y"))
{
//input
System.out.println ("Which fibonacci term would you like to find?");
invalid = true;
while (invalid == true)
{
invalid = false;
try
{
fib_term = Integer.parseInt (br.readLine ());
/*
while (fib_term<=0)
{
System.out.println ("Invalid input. Please try again.");
System.out.println ();
fib_term = Integer.parseInt (br.readLine ());
}
*/
}
catch (Exception e) //in case the string is not a number
{
System.out.println ("Invalid input. Please try again.");
System.out.println ();
invalid = true;
}
}
//output
System.out.println ();
if (fib_term <= 0)
{
Fibonacci (fib_term);
}
else
{
System.out.println ("Term " + fib_term + " of the fibonacci sequence is " + Fibonacci (fib_term));
}
//System.out.println(counter);
//rerun program
System.out.println ();
System.out.println ("Would you like to run the program again? (y/n)");
System.out.println ();
sentinel = br.readLine ();
System.out.println ();
while (!(sentinel.equalsIgnoreCase ("yes") || sentinel.equalsIgnoreCase ("y")
|| sentinel.equalsIgnoreCase ("n") || sentinel.equalsIgnoreCase ("no")))
{
//System.out.println();
System.out.println ("Invalid input. Please try again.");
System.out.println ("Would you like to run the program again? (y/n)");
sentinel = br.readLine ();
System.out.println ();
}
}
System.out.println ("Program terminated by user.");
}
}
# Function for nth Fibonacci number
def Fibonacci(n):
if n<0:
print("Incorrect input")
# First Fibonacci number is 0
elif n==0:
return 0
# Second Fibonacci number is 1
elif n==1:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
# Driver Program
print(Fibonacci(9))
#This code is contributed by Saket Modi
// C# program for Fibonacci Series
// using Recursion
using System;
public class GFG
{
public static int Fib(int n)
{
if (n <= 1)
{
return n;
}
else
{
return Fib(n - 1) + Fib(n - 2);
}
}
// driver code
public static void Main(string[] args)
{
int n = 9;
Console.Write(Fib(n));
}
}
// This code is contributed by Sam007
class fibonacci
{
static int fib(int n)
{
int F[][] = new int[][]{{1,1},{1,0}};
if (n == 0)
return 0;
power(F, n-1);
return F[0][0];
}
/* Helper function that multiplies 2 matrices F and M of size 2*2, and
puts the multiplication result back to F[][] */
static void multiply(int F[][], int M[][])
{
int x = F[0][0]*M[0][0] + F[0][1]*M[1][0];
int y = F[0][0]*M[0][1] + F[0][1]*M[1][1];
int z = F[1][0]*M[0][0] + F[1][1]*M[1][0];
int w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
/* Helper function that calculates F[][] raise to the power n and puts the
result in F[][]
Note that this function is designed only for fib() and won't work as general
power function */
static void power(int F[][], int n)
{
int i;
int M[][] = new int[][]{{1,1},{1,0}};
// n - 1 times multiply the matrix to {{1,0},{0,1}}
for (i = 2; i <= n; i++)
multiply(F, M);
}
/* Driver program to test above function */
public static void main (String args[])
{
int n = 9;
System.out.println(fib(n));
}
}
/* This code is contributed by Rajat Mishra */