Il secondo problema del Progetto Eulero e' cosi' formulato
---------------------------------------------
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
----------------------------------------------
Il programma per il calcolo e' il seguente con soluzione 4613732
------------------------
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int fibo_1 = 0;
int fibo_2 = 1;
int fibo = 0;
int contatore = 1;
int somma = 0;
for (fibo = 1; fibo<4000000;fibo++)
{
fibo = fibo_1 + fibo_2;
fibo_2 = fibo_1;
fibo_1 = fibo;
contatore++;
cout << fibo << endl;
if ((fibo%2)==0){
somma = somma + fibo;
//cout << "Pari " << contatore%2 << " " << fibo << " " << somma << endl;
}
}
cout << "Somma " << somma << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Nessun commento:
Posta un commento