Please write pseudo code for this program import java.util.*; public class Effic
ID: 3701220 • Letter: P
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 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("");
}
}
}
Explanation / Answer
Below is the pseudo code:
Method START fib
Step 1: Take a parameter n of long type
Step 2: Initilize three variables of long type fibonacci1,fibonacci2,fibonacci3
Step 3: IF n is less than or 1 (n <=1)
return 1 and exit;
END IF
ELSE
SET:
fibonacci1 =0
fibonacci2 =0
fibonacci3 =0
Step 3.1: BEGIN LOOP
LOOP i from o to less than n
SET :
fibonacci3 = fibonacci1 + fibonacci2
fibonacci1 = fibonacci2
fibonacci2 = fibonacci3
INCREMENT i by 1 (i=i+1)
EXIT LOOP
return fibonacci3
END ELSE
Method END fib
Method START MAIN
Step 1: Initilize a variable user of Scanner object
Step 2: Input integer value number from user
Step 3: Initilize a variable currentTimeNanoSec of type double
Step 3.1: SET
currentTimeNanoSec = System.nanoTime();
Step 4: Initilize a variable previousTimeNanoSec of type double
Step 5: Initilize a variable elapsedTimeNanoSec of type double
Step 6: Initilize a variable elapsedTimeMilliSec of type double
Step 7: Initilize a variable elapsedTimeSec of type double
Step 8: BEGIN LOOP
LOOP x from o to less than or equal to 5
Step 8.1 SET
previousTimeNanoSec = currentTimeNanoSec
Step 8.2 PRINT : "The fibonacci number at position: "
Step 8.3 COMPUTE : number = number + x
Step 8.4 PRINT : number
Step 8.5 SET
currentTimeNanoSec = System.nanoTime();
Step 8.6 SET
elapsedTimeNanoSec = currentTimeNanoSec - previousTimeNanoSec
elapsedTimeMilliSec = elapsedTimeNanoSec / 1000000
elapsedTimeSec = elapsedTimeMilliSec / 1000
Step 8.7 PRINT : elapsedTimeSec, elapsedTimeMilliSec and currentTimeNanoSec
EXIT LOOP
Method END MAIN