Revision: 40554
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 3, 2011 15:46 by mju4t
Initial Code
/* Alexander DeTrano 2/1/2010 Project Euler - Problem 2 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. */ #include <iostream> #include <vector> using namespace std; int fib(int n); int main() { vector<int> myArray; //declare vector to store Fibonacci numbers int s=34; // Size needed for vector length int sum=0; //Initialize sum to 0 //Generate Fibonacci numbers and store in array, add every third for(int i=0; i<s; i++){ myArray.push_back(i); if (i%3 == 0){ myArray[i]=fib(i); cout<< "fib(" << i << ")= " << myArray[i] << " \n"; sum+=myArray[i]; cout<<" Sum = "<< sum <<"\n"; } } cout<<"Total sum = " << sum; } // Function to generate Fibonacci numbers int fib(int n) { if ( n==0 ) return 0; if ( n==1 ) return 1; /*for(int i=0;i<5;i++){ z= fib(n-1) + fib(n-2); */ return fib(n-1) + fib(n-2); }
Initial URL
Initial Description
Sorted via problem ID.
Initial Title
Project Euler Problem 2
Initial Tags
Initial Language
C++