2. The Fibonacci sequence is defined as
f(n) = f(n - 1) + f(n - 2)
with f(0) = 0 and f(1) = 1.
Find f(54) by a program or maually. Note that this number mustbe positive
and f(53) = 53.......73 (starting with 53 and ending with 73).I must admit that
my three machines including a desktop are unable to find f(54)and they
quit during computation.
The answer is f(54) = 86267571272
*/
The Java code:
public class FibonacciTest2 {
​public static long BinaryFibonacci(int k) { // This is longtype rather than int here
​​if (k == 1 || k==0)
​​ return k;
​​else
​​ return BinaryFibonacci(k - 1) + BinaryFibonacci(k -2);
​}
​public static void main (String args[]) {
​​System.out.println(BinaryFibonacci(53));
​}
}
My new computer is able to find
f(53) = 53316291173 and
f(52) = 32951280099.
Therefore,
f(54) = f(53) + f(52) = 86267571272.
Note that if your computer is only able to find f(52), thenyou need do more calculation.
4. Analyze this code and run it, if there are any issues notethem and fix them, if not give the output and Big O notationruntime:
public class PrintBits {
public static void printBits(int a) {
try {
String str = Integer.toBinaryString((Integer) a);
for(int i = 0; i < str.length(); i++){
System.out.print (str.charAt(i));
}
}
catch (ClassCastException e) {
throw new RuntimeException (\"Argument is not anInteger\");
}
}
public static void main (String[] args){
printBits (-17);
System.out.println();
printBits (17);
System.out.println( );
}
}