Skip to content

Instantly share code, notes, and snippets.

@matiit
Created May 27, 2010 07:06
Show Gist options
  • Save matiit/415559 to your computer and use it in GitHub Desktop.
Save matiit/415559 to your computer and use it in GitHub Desktop.
ciag fib rekurencyjnie i iteracyjnie
#include <cstdlib>
#include <iostream>
using namespace std;
double fibonacci(long long n){
if (n<2)
return 1;
else
return fibonacci(n-1)+fibonacci(n-2);
}
double fibonacci_iter(double n){
if (n<2)
return 1;
double p=1;
double pp=1;
double wynik=0;
double tmp;
for (int i=0;i<n-1;i++){
wynik=p+pp;
tmp = p;
p = wynik;
pp = tmp;
}
return wynik;
}
int main(int argc, char *argv[])
{
double n;
while (1){
cout << "Podaj \n";
cin >> n;
double wynik;
wynik = fibonacci_iter(n);
cout << "Wynik to: " << wynik << endl << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment