Skip to content

Instantly share code, notes, and snippets.

@sarahhodne
Created December 14, 2010 10:28
Show Gist options
  • Save sarahhodne/740244 to your computer and use it in GitHub Desktop.
Save sarahhodne/740244 to your computer and use it in GitHub Desktop.
Recursive fibonacci
#include <iostream>
using namespace std;
unsigned long long fib(int n)
{
if (n < 2)
return n;
else
return fib(n-1) + fib(n-2);
}
int main()
{
for(int i=0;i<20;i++)
cout<<fib(i)<<endl;
return 0;
}
def fib(n)
n < 2 ? n : fib(n-1) + fib(n-2)
end
20.times do |i|
puts fib(i)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment