For integer n 0, define f(n) = f(n - 1) + f(n - 2) where f(0) = 0 and f(1) = 1.
ID: 441763 • Letter: F
Question
For integer n 0, define f(n) = f(n - 1) + f(n - 2) where f(0) = 0 and f(1) = 1. Write a recursive method static long f (int n) that returns the value f(n) and a non-recursive method static double F(int n) that returns the value F(n), where F(n) = Write a test main method which, for a given n 0 (say n = 10), outputs the value f (n) and F (n), and true if f (n) = F (n), and false if f (n) F (n). Your test should be using n = 10. Note: f (n) is an integer and F (n) is a real number. In order for f (n) = F (n), the integer part of F (n) must be the same as f (n) and the fraction part of F (n) should be 0.0. That is 6 = 6.0 but 6 6.00 0 1.Explanation / Answer
//expanded
//please rate and flag anyone that copies
import java.util.Scanner;
public class test{
public static void main(String[]args)
{
int n=10;
System.out.printf("%b", f(n)==F(n));
}
public static long f(int n){
if(n==1){
return 1;
}
else if(n==0){
return 0;
}
else
return f(n-1)+f(n-2);
}
public static double F(int n){
//broke into parts for readability
double xplus=(1+Math.sqrt(5))/2.0;
double xminus=(1-Math.sqrt(5))/2.0;
return (1/Math.sqrt(5))*(Math.pow(xplus, n)-Math.pow(xminus, n));
}
}//end class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.