forked from IND-Debugs/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibooptimised.java
More file actions
33 lines (31 loc) · 822 Bytes
/
Copy pathfibooptimised.java
File metadata and controls
33 lines (31 loc) · 822 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
package Allprogram;
import java.util.Scanner;
public class fibooptimised {
public static void main(String[] args) throws Exception {
System.out.println("Enter the value");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int j = 0;
int[] gb = new int[n + 1];
long start = System.currentTimeMillis();
int fibn = fib1(n, gb);
System.out.println(fibn);
long end = System.currentTimeMillis();
float sec = (end - start) /10000F;
System.out.println(sec + " seconds");
}
public static int fib1(int n, int[] gb) {
if (n == 0 || n == 1) {
return n;
}
if (gb[n] != 0) {
return gb[n];
}
System.out.println("Hello" + n);
int fibo1 = fib1(n - 1, gb);
int fibo2 = fib1(n - 2, gb);
int fibn = fibo1 + fibo2;
gb[n] = fibn;
return fibn;
}
}