Created
October 22, 2012 08:30
-
-
Save juanfal/3930336 to your computer and use it in GitHub Desktop.
Sin subprograma, calculamos iterativamente el n-simo (usuario dice) número de Fibonacci
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
// fibonacci.cpp | |
// juanfc 2012-10-22 | |
// Diseña un algoritmo que lea un núero n por teclado y calcule el n-simo | |
// número de la serie de Fibonacci. Los dos primeros números de esta | |
// serie son el cero y el uno, y a partir de éstos cada número se | |
// calcula realizando la suma de los dos anteriores: | |
// F(n)=F(n−1)+F(n−2), F(0)=1, F(1)=1 | |
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, … | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int n; | |
cout << "Introducir n: "; | |
cin >> n; | |
int a; // número anterior al anterior | |
int b; // número anterior | |
int f; // número fib actual | |
a = 0; | |
b = 1; | |
f = a + b; | |
// f corresponde a i = 2, el siguiente sería el tercero | |
for ( int i = 3; i <= n; ++i ) { | |
a = b; | |
b = f; | |
f = a + b; | |
} | |
cout << "Fib: " << f << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment