2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2) with...

50.1K

Verified Solution

Question

Programming

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( );
}
}

Answer & Explanation Solved by verified expert
4.4 Ratings (854 Votes)
Hello there Answer 2You are facing the problem because of the time complexity of the algorithm you are using that is it    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students