import java.util.Scanner;
import java.util.Arrays;
import java.lang.System;

public class Fibonacci {
    public void checkNthPosition() {
        Scanner userInput = new Scanner(System.in);
        System.out.println("Enter an integer for n to get the nth integer of the Fibonacci Sequence:");
        int n = userInput.nextInt();
        forLoop(n);
        whileLoop(n);
        long timeStartRecursion = System.nanoTime();
        recursion(n);
        double timeElapsedRecursion = (double) (System.nanoTime() - timeStartRecursion)/ (double) (1000);
        System.out.println("Using a recursion loop:");
        System.out.println("Computational Time (In Microseconds): " + timeElapsedRecursion);
        System.out.println("The nth integer of the Fibonacci sequence: " + recursion(n));
    }

    public int forLoop(int n) {
        long timeStart = System.nanoTime(); 
        int[] fiboArray = new int[]{0, 1};
        if (n == 0) {
            fiboArray = Arrays.copyOf(fiboArray, fiboArray.length - 1);
        } else if (n == 1) {
            fiboArray = fiboArray;
        } else if (n > 1) {
            fiboArray = Arrays.copyOf(fiboArray, n + 1);
            for (int i = 2; i <= n; i++) {
                fiboArray[i] = fiboArray[i - 2] + fiboArray[i - 1];
            }
        }
        double timeElapsed = (double) (System.nanoTime() - timeStart)/ (double) (1000);
        String fiboArrayPrint = Arrays.toString(fiboArray);
        System.out.println("Using a for loop:");
        System.out.println("Computational Time (In Microseconds): " + timeElapsed);
        System.out.println("Fibonacci array with n integers: " + fiboArrayPrint);
        System.out.println("The nth integer of the Fibonacci sequence: " + fiboArray[n]);
        return fiboArray[n];
    }

    public int whileLoop(int n) {
        long timeStart = System.nanoTime();
        int[] fiboArray = new int[]{0, 1};
        if (n == 0) {
            fiboArray = Arrays.copyOf(fiboArray, fiboArray.length - 1);
        } else if (n == 1) {
            fiboArray = fiboArray;
        } else if (n > 1) {
            fiboArray = Arrays.copyOf(fiboArray, n + 1);
            int i = 2;
            while (i <= n) {
                fiboArray[i] = fiboArray[i - 2] + fiboArray[i - 1];
                i++;
            }
        }
        double timeElapsed = (double) (System.nanoTime() - timeStart)/ (double) (1000);
        String fiboArrayPrint = Arrays.toString(fiboArray);
        System.out.println("Using a while loop:");
        System.out.println("Computational Time (In Microseconds): " + timeElapsed);
        System.out.println("Fibonacci array with n integers: " + fiboArrayPrint);
        System.out.println("The nth integer of the Fibonacci sequence: " + fiboArray[n]);
        return fiboArray[n];
    }

    public int recursion(int n) {
        if (n == 0) {
            return 0;
        } else if (n == 1) {
            return 1;
        }
        return recursion(n-2) + recursion(n-1);
    }

    
}

public class Fibo extends Fibonacci {
    public static void main(String[] args) {
        Fibonacci play = new Fibonacci();
        play.checkNthPosition();
    }
}
Fibo.main(null);
Enter an integer for n to get the nth integer of the Fibonacci Sequence:
Using a for loop:
Computational Time (In Microseconds): 7.409
Fibonacci array with n integers: [0, 1, 1, 2, 3, 5, 8, 13]
The nth integer of the Fibonacci sequence: 13
Using a while loop:
Computational Time (In Microseconds): 4.479
Fibonacci array with n integers: [0, 1, 1, 2, 3, 5, 8, 13]
The nth integer of the Fibonacci sequence: 13
Using a recursion loop:
Computational Time (In Microseconds): 13.698
The nth integer of the Fibonacci sequence: 13

Questions

  1. Skill 1.B: Determine code that would be used to complete code segments (i.e. For, While, Recursion).
  • To complete the code segments, I used loop statements such as for, while, and recursion.
  1. Skill 4.C: Determine if two or more code segments yield equivalent results (Be sure to discuss how you know the results are the same).
  • All code segments yield the same results, because although they are different kinds of statements, they essentially use the same operations and ultimately return the same result.
  1. Skill 5.A: Describe the behavior of a given segment of program code (Describe the different in recursion versus for and while loops; perhaps add timing to determine speed).
  • Using the timeStart and timeElapsed variables to determine the computational time of each program code, I found out the the recursion loop takes slightly longer than the for and while loops. This is likely because it has to calculate a lot of the values due to the multiple return statements it has. The for and whiles loops likely took less time because their caching methods (Appending values to and iterating through arrays) are faster.