From f7778469c34ced88e7c522f0ee3bb36f5e2d739d Mon Sep 17 00:00:00 2001 From: arvindh75 Date: Sun, 6 Oct 2019 14:26:13 +0530 Subject: [PATCH] Using Memoization for Fibonacci sequence --- memoization.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 memoization.cpp diff --git a/memoization.cpp b/memoization.cpp new file mode 100644 index 0000000..391b0db --- /dev/null +++ b/memoization.cpp @@ -0,0 +1,27 @@ +#include +#include +#define mod 1000000007 +typedef long long int lli; +lli fib(lli n,lli memo[]) +{ + lli res; + if(memo[n]!=0) + return memo[n]; + if(n==1 || n==2) + res = 1; + else + res = fib(n-1,memo) + fib(n-2,memo); + memo[n]=res; + return res; +} +using namespace std; +int main() +{ + lli memo[100000]={0},n,p; + cout<<"Enter the value of n :\n"; + cin>>n; + p = fib(n,memo); + cout<