Created
May 27, 2010 07:06
-
-
Save matiit/415559 to your computer and use it in GitHub Desktop.
ciag fib rekurencyjnie i iteracyjnie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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