Question
Please write pseudo code for this program import java.util.*; public class EfficientComputationOfFibonacciNumbers { // Computes the nth term of the fibonacci sequence public static long
Please write pseudo code for this program
import java.util.*;
public class EfficientComputationOfFibonacciNumbers {
// Computes the nth term of the fibonacci sequence
public static long fib(long n)
{
long fibonacci1, fibonacci2, fibonacci3;
if(n <= 1)
return 1;
else
{
fibonacci1 = 0;
fibonacci2 = 1;
fibonacci3 = 0;
for(int i = 0; i < n; i++)
{
fibonacci3 = fibonacci1 + fibonacci2;
fibonacci1 = fibonacci2;
fibonacci2 = fibonacci3;
}
return fibonacci3;
}
}
public static void main(String [] args)
{
Scanner user = new Scanner(System.in);
// gets the integer from the user
System.out.println("Enter any positive integer: ");
int number = user.nextInt();
/* Determines the time of a function call */
double currentTimeNanoSec = System.nanoTime();
double previousTimeNanoSec, elapsedTimeNanoSec;
double elapsedTimeMilliSec, elapsedTimeSec;
for(int x = 0; x <= 5; x++)
{
// Records time before the method is called
previousTimeNanoSec = currentTimeNanoSec;
System.out.println("-----------------------------");
System.out.print("The fibonacci number at position: ");
System.out.print((number + x) + " is ");
// Computes and prints fibonacci number for the next number
System.out.println(fib(number + x));
// Records time after the method is called
currentTimeNanoSec = System.nanoTime();
// Computes elapsed time in nanoseconds
elapsedTimeNanoSec = currentTimeNanoSec - previousTimeNanoSec;
// Converts nanoseconds to milliseconds
elapsedTimeMilliSec = elapsedTimeNanoSec / 1000000;
// Converts milliseconds to seconds
elapsedTimeSec = elapsedTimeMilliSec / 1000;
// Prints time in seconds and milliseconds
System.out.println("Computed time in Seconds: " + elapsedTimeSec + " MilliSeconds: " +
elapsedTimeMilliSec + " NanoSeconds: " + currentTimeNanoSec);
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