Skip to content

Instantly share code, notes, and snippets.

@astinaam
Created February 21, 2017 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astinaam/75be5800b9e09dc179e66a8451910c21 to your computer and use it in GitHub Desktop.
Save astinaam/75be5800b9e09dc179e66a8451910c21 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
string fib[5001];
string add(string a,string b)
{
string ans;
int sa = a.size();
int sb = b.size();
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int carry=0;
for(int i=0; i<max(sa,sb); ++i)
{
int x;
if(i<sa)
x = a[i]-'0';
else x=0;
int y=0;
if(i<sb)
y = b[i]-'0';
int add = x+y+carry;
carry=0;
if(add>9)
{
ans.push_back((add%10)+'0');
carry = add/10;
}
else
ans.push_back(add+'0');
}
if(carry)
ans.push_back(carry+'0');
reverse(ans.begin(),ans.end());
return ans;
}
void precalc()
{
fib[0]="0";
fib[1]="1";
for(int i=2; i<5001; ++i)
{
fib[i] = add(fib[i-1],fib[i-2]);
}
}
int main()
{
ios_base::sync_with_stdio(false);
precalc();
int n;
while(cin>>n)
{
cout<<"The Fibonacci number for "<<n<<" is ";
cout<<fib[n]<<"\n";
}
}
@sdmg15
Copy link

sdmg15 commented Feb 21, 2017

What if the series i want to generate is more than 5001?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment