//Code in java public class Recurrences { //static int fibonacci ( int n ) [stat
ID: 3835206 • Letter: #
Question
//Code in java
public class Recurrences {
//static int fibonacci ( int n ) [static]
//Recursively implements the classic Fibonacci function. Note, this implementation requires the fibonacci ( 0 ) = 0. And the first couple of //Fibonacci numbers is given here:
//0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
//See if you can find the pattern. Hint: the next fibonacci is determined by summing previous fibonacci numbers (how many)? If you don't //know what a fibonacci number is, try a Google search, or Wolfram Mathworld.
public static int fibonacci( int n ) {
throw new UnsupportedOperationException();
}
//static boolean isPrime ( int n ) [static]
//Returns true iff n is a prime integer, which in this case means a positive integer > 1 that is divisible only by itself and 1.
public static boolean isPrime( int n ) {
throw new UnsupportedOperationException();
}
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class reccurrence {
public static int fibonacci(int n)
{
if (n <= 1)
return n;
return fibonacci(n-1) + fibonacci(n-2);
}
public static boolean isPrime( int n ) {
int i=2;
while(i<n)
{
if(n%i==0)return true;
i++;
}
return false;
}
public static void main(String argv[])
{
System.out.println("fib 0 : "+fibonacci(0));
System.out.println("fib 1 : "+fibonacci(1));
System.out.println("fib 5 : "+fibonacci(5));
if(isPrime(5))
{
System.out.println("Not a Prime number");
}
else
{
System.out.println("Prime number");
}
}
}
output:-
run:
fib 0 : 0
fib 1 : 1
fib 5 : 5
Prime number
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.