Created
January 20, 2013 12:20
-
-
Save GaProgMan/4578229 to your computer and use it in GitHub Desktop.
One possible solution for problem 2 on the Project Euler website
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
/* | |
* Project Name: Problem Two | |
* Solution Name: Problem Two | |
* Original creation date: 03/07/2011 | |
* Edit date: 18/01/2013 | |
* Programmer name: Jamie Taylor (aka "GaProgMan") | |
* File name: ProblemTwo.cpp | |
* | |
* Purpose of the project: | |
* This code is my solution for "Problem Two" listed | |
* on Project Euler. The original problem is listed | |
* in the comment block below | |
* | |
* Problem Two, from Project Euler. | |
* URL: http://projecteuler.net/index.php?section=problems&id=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. | |
* | |
* Chosen Algorithm: | |
* Step 1) add up the Fibonacci sequence, until we | |
* reach 4 million (4000000) and make a note | |
* of the final number | |
* Step 2) sum all of the even numbers between 0 and | |
* the final number in step 1 | |
* | |
* Algorithm discussion: | |
* Running through my fibonacci calculation method | |
* (CalculateFibonacci() ) tells me something quite | |
* useful. | |
* 33 steps through the fibonacci sequence give | |
* the answer 3,524,578 34 steps through the fibonacci | |
* sequence give the answer 5,702,887 | |
* Since the Fibonacci sequence is a sequence of whole | |
* numbers (1,2,3... etc), then step 33 is where I shall | |
* stop. This means that I need to sum the even valued | |
* terms between 1 and 33 | |
* | |
* GNU Copyright information | |
* Copyright 2011 Jamie Taylor <jamie@taylorj.org.uk> | |
* | |
* This program is free software; you can redistribute | |
* it and/or modify it under the terms of the GNU General | |
* Public License as published by the Free Software | |
* Foundation; either version 2 of the License, or (at | |
* your option) any later version. | |
* | |
* This program is distributed in the hope that it will | |
* be useful, but WITHOUT ANY WARRANTY; without even the | |
* implied warranty of MERCHANTABILITY or FITNESS FOR A | |
* PARTICULAR PURPOSE. See the GNU General Public | |
* License for more details. | |
* | |
* You should have received a copy of the GNU General | |
* Public License along with this program; if not, write | |
* to the Free Software Foundation, Inc., 51 Franklin | |
* Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
*/ | |
#include <iostream> | |
using namespace std; | |
int main () { | |
int initial = 1; | |
int previous = 1; | |
int total = 0; | |
int sum = 0; | |
do { | |
total = initial + previous; | |
if ( total % 2 == 0 ) | |
sum += total; | |
initial = previous; | |
previous = total; | |
} while ( total < 4000000 ); | |
cout << "The sum of all even terms (up to 4 mil): " << sum << endl; | |
char ch; | |
cin >> ch; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment