Question

In: Computer Science

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

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 must be 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 long type 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), then you need do more calculation.
4. Analyze this code and run it, if there are any issues note them and fix them, if not give the output and Big O notation runtime:
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 an Integer");
}
}
public static void main (String[] args){
printBits (-17);
System.out.println();
printBits (17);
System.out.println( );
}
}

Solutions

Expert Solution

Hello there! :)

Answer 2.

You are facing the problem because of the time complexity of the algorithm you are using, that is, it takes too much time. There is a better time complexity algorithm to make it work on almost all modern machines pretty fast, that is, to find the Fibonacci number in a bottom up fashion. You know the values of and . Use them to calculate . Then use and to calculate and so on till you find . Here is a documented code to solve the assignment:

public class FibonacciTest2 {
    public static long BinaryFibonacci(int k) {
        if (k == 0)
            return 0L; // f(0) = 0
        if (k == 1)
            return 1L; // f(1) = 1

        // when k >= 2
        long a = 0L; // f(0)
        long b = 1L; // f(1)
        long c = a + b; // f(2)

        for (int index = 3; index <= k; ++index) {
            a = b;
            b = c;
            c = a + b; // f(index)
        }

        return c; // returning f(k)
    }

    public static void main (String args[]) {
        System.out.println(BinaryFibonacci(54));
    }
}

Here is a snapshot of a demo run showing my system specs, the time to taken to run and the computed value of :

Answer 4.

This one compiles and runs just fine on my system - no issues! :)

Here's the snapshot of the run:

The program computes the binary representation of the argument integer and loops through all the characters in the generated string of bits. Since an integer has a binary representation of a size that is of the order of the logarithm of the number base two, the Big O notation runtime is .

Hope this helps! :)


Related Solutions

Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n −...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n − 2) for n ≥ 2 (a) Use strong induction to show that F(n) ≤ 2^n for all n ≥ 0. (b) The answer for (a) shows that F(n) is O(2^n). If we could also show that F(n) is Ω(2^n), that would mean that F(n) is Θ(2^n), and our order of growth would be F(n). This doesn’t turn out to be the case because F(n)...
For the Fibonacci sequence, prove the formula u2n+1 = un un+2 + (-1)n
For the Fibonacci sequence, prove the formula u2n+1 = un un+2 + (-1)n
The Fibonacci sequence is defined as F_1 = 1, F_2 = 1, and F_n = F_n-1...
The Fibonacci sequence is defined as F_1 = 1, F_2 = 1, and F_n = F_n-1 + F_n-2 for n >= 3. Calculate the sum F_1 + F_2 + ... + F_n using the fundamental theorem of summation.  
3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a...
3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a function to quickly compute this sequence. >>> [hc(i) for i in range(1,20)] [1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 8, 8, 8, 9, 10, 11]
Let’s revisit the Fibonacci sequence, this time without an array. Recall that the sequence is defined...
Let’s revisit the Fibonacci sequence, this time without an array. Recall that the sequence is defined by the following recurrence relation: F0 = 0 F1 = 1 Fk = Fk-1 + Fk-2 So, the sequence looks like: 0, 1, 1, 2, 3, 5, 8, 13, 21, … Write a program fib.c consisting only of a main() function that prompts the user for the last Fibonacci number n to print, and then prints them from F0 to Fn. (No file input...
For f: N x N -> N defined by f(m,n) = 2m-1(2n-1) a) Prove: f is...
For f: N x N -> N defined by f(m,n) = 2m-1(2n-1) a) Prove: f is 1-to-1 b) Prove: f is onto c) Prove {1, 2} x N is countable
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence,...
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 114, … etc. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. We define Fib(0)=0,...
#2 The Jacobsthal sequence is defined by J(1)=J(2)=1 and J(n)=J(n-1)+2J(n-2). Use recursion to write a function...
#2 The Jacobsthal sequence is defined by J(1)=J(2)=1 and J(n)=J(n-1)+2J(n-2). Use recursion to write a function that takes in a positive integer n and returns the nth Jacobsthal number. >>> J(8) 85 >>> J(9) 171 #3 Use recursion to write a function that takes in a positive integer n and returns all n digit numbers containing only odd digits. >>> f(1) [1, 3, 5, 7, 9] >>> f(2) [11, 13, 15, 17, 19, 31, 33, 35, 37, 39, 51, 53,...
Write the first four terms of the sequence defined by the recursive formula a1 = 2, an = an − 1 + n.
Write the first four terms of the sequence defined by the recursive formula a1 = 2, an = an − 1 + n.
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 +...
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 + Fn for all n ∈ N ∪ {0}. (1) Make and prove an (if and only if) conjecture about which Fibonacci numbers are multiples of 3. (2) Make a conjecture about which Fibonacci numbers are multiples of 2020. (You do not need to prove your conjecture.) How many base cases would a proof by induction of your conjecture require?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT