- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputeFibonacci.java
More file actions
Latest commit
35 lines (22 loc) · 871 Bytes
/
Copy pathComputeFibonacci.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)
importjava.util.Scanner;
publicclassComputeFibonacci {
publicstaticvoidmain(Stringargs[]) {
Scannerinput = newScanner(System.in);
System.out.print("Enter an index for the Fibonacci number: ");
intindex = input.nextInt();
longstartTime = System.nanoTime();
System.out.println("Fibonacci number at index " + index + " is " + fib(index));
longelapsedTime = System.nanoTime() - startTime;
// System.out.println(elapsedTime);
}
publicstaticlongfib(longindex) {
if (index == 0) // Base case
return0;
elseif (index == 1) // Base case
return1;
else// Reduction and recursive calls
returnfib(index - 1) + fib(index - 2);
}
}