-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputeFibonacci.java
More file actions
35 lines (22 loc) · 871 Bytes
/
ComputeFibonacci.java
File metadata and controls
35 lines (22 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Christopher Yonek
// Compute Fibbonacci (Recursion)
import java.util.Scanner;
public class ComputeFibonacci {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an index for the Fibonacci number: ");
int index = input.nextInt();
long startTime = System.nanoTime();
System.out.println("Fibonacci number at index " + index + " is " + fib(index));
long elapsedTime = System.nanoTime() - startTime;
// System.out.println(elapsedTime);
}
public static long fib(long index) {
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
return fib(index - 1) + fib(index - 2);
}
}