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)...
Fibonacci Sequence in JAVA f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2) Part...
Fibonacci Sequence in JAVA f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2) Part of a code public class FS { public static void main(String[] args){ IntSequence seq = new FibonacciSequence(); for(int i=0; i<20; i++) { if(seq.hasNext() == false) break; System.out.print(seq.next()+" "); } System.out.println(" "); } } ///Fill in the rest of the code! Output 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 You should define...
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.  
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn =...
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn = Fn−1 + Fn−2 for n ≥ 2. Use induciton to prove that F0F1 + F1F2 + · · · + F2n−1 F2n = F^2 2n for all positive integer n.
. Fibonacci sequence Fn is defined as follows: F1 = 1; F2 = 1; Fn =...
. Fibonacci sequence Fn is defined as follows: F1 = 1; F2 = 1; Fn = Fn−1 + Fn−2, ∀n ≥ 3. A pseudocode is given below which returns n’th number in the Fibonacci sequence. RecursiveFibonacci(n) 1 if n = 0 2 return 0 3 elseif n = 1 or n = 2 4 return 1 5 else 6 a = RecursiveFibonacci(n − 1) 7 b = RecursiveFibonacci(n − 2) 8 return a + b Derive the complexity of the...
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
Find f(1), f(2), f(3) and f(4) if f(n) is defined recursively by f(0)=4f(0)=4 and for n=0,1,2,…n=0,1,2,…...
Find f(1), f(2), f(3) and f(4) if f(n) is defined recursively by f(0)=4f(0)=4 and for n=0,1,2,…n=0,1,2,… by: (a) f(n+1)=−2f(n) f(1)= f(2)= f(3)= f(4)= (b) f(n+1)=4f(n)+5 f(1)= f(2)= f(3)= f(4)= (b) f(n+1)=f(n)2−4f(n)−2 f(1)= f(2)= f(3)= f(4)=
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT