Question
//// Implement Fibonacci to work and check runtime ////// package main.java; import java.math.BigInteger; public class Fibonacci { // naive public static long recursiveFibonacci(int n) {
//// Implement Fibonacci to work and check runtime //////
package main.java;
import java.math.BigInteger;
public class Fibonacci { // naive public static long recursiveFibonacci(int n) { return -1; }
// efficient public static long arrayFibonacci(int n) { return -1; }
public static void main(String[] args) { int startingNumber = 43; for (int i = 0; i < 6; i++) { System.out.println("--------n = " + (startingNumber+i) + "--------"); long start = System.nanoTime(); long fib = recursiveFibonacci(startingNumber+i); long stop = System.nanoTime(); long timeElapsed = (stop - start) / 1_000_000_000; System.out.println(" naive: " + timeElapsed + "seconds");
long start2 = System.nanoTime(); long fib2 = arrayFibonacci(startingNumber+i); long stop2 = System.nanoTime(); long timeElapsed2 = (stop2 - start2) / 1_000; System.out.println(" efficient: " + timeElapsed2 + "microseconds");
if (fib == fib2) { System.out.println(" fibonacci number: " + fib); } else { System.err.println("!!!! The naive algorithm and the efficient algorithm produced two different numbers: " + fib + " and " + fib2); System.exit(1); }
System.out.println("----------------------"); }
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started