From 6fad010a9fbc3d4ae455082b0e4c75e4541ea39c Mon Sep 17 00:00:00 2001 From: priya Date: Sun, 4 Oct 2020 09:48:44 +0530 Subject: [PATCH 1/2] added Fib-code to Algorithms --- N-th_Fibonacci/Readme.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 N-th_Fibonacci/Readme.md diff --git a/N-th_Fibonacci/Readme.md b/N-th_Fibonacci/Readme.md new file mode 100644 index 0000000..71e8115 --- /dev/null +++ b/N-th_Fibonacci/Readme.md @@ -0,0 +1,7 @@ +# Readme + +***n-th Fibonacci using memoization*** + +Finding the n-th fibonacci number using normal recursive algorithm has a time complexity of O(2^n). +Here we use a Dynamic Programming approach called Memoization. The answers to subproblems are stored in an array and thereby reducing the number of computations drastically. This Algorithm runs in the order of O(n). + From 461921f50ffbe9a9830b291fd0f4bed207855b6c Mon Sep 17 00:00:00 2001 From: priya Date: Sun, 4 Oct 2020 09:53:42 +0530 Subject: [PATCH 2/2] added Fibo-code to Algorithms --- N-th_Fibonacci/Fibonacci.java | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 N-th_Fibonacci/Fibonacci.java diff --git a/N-th_Fibonacci/Fibonacci.java b/N-th_Fibonacci/Fibonacci.java new file mode 100644 index 0000000..9f9a8c2 --- /dev/null +++ b/N-th_Fibonacci/Fibonacci.java @@ -0,0 +1,66 @@ + + +import java.util.Scanner; + +public class Fibonacci +{ + double f=0; + double mem[]=new double[1001]; + public static void main(String args[]) + { + Scanner scanner=new Scanner(System.in); + int n =scanner.nextInt(); + System.out.println(n+"-th Fibonacci number is: "+new Fibonacci().fibo2(n)); + scanner.close(); + } + public double fibo2(int n) + { + for(int k=1;k<=n;k++) + { + if(k<=2) + f=1; + else + f=mem[k-1]+mem[k-2]; + mem[k]=f; + } + return mem[n]; + + } +} + + + + + + + + + + + + + +/* + * public double fibo(int n) + { + if(n<=2) + f=1; + else + f=fibo(n-1)+fibo(n-2); + return f; + + } + + public double fibo1(int n) + { + if(mem[n]!=0) + f=mem[n]; + else if(n<=2) + f=1; + else + f=fibo1(n-1)+fibo1(n-2); + mem[n]=f; + return f; + + } + */