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.

## General Algorithm:

Again, a fairly iterative problem. It explains what the Fibonacci sequence is. In terms of mathematical operations, it is defined as:

The process is continuous. So continue to repeat this, to get successive terms.

- Initialize a sum variable to 0.
- Iterate through all the term of the Fibonacci sequence.
- If a term is more than 4,000,000 then break the loop
- Otherwise add it to the sum.
- When the loop breaks, print the sum.

Advertisements

## Anonymous

/ January 14, 2012Really unethical to post solutions!

## virajmahesh

/ January 14, 2012Why ? I’ve solved them myself, not ripped them off from somewhere else. Project Euler is fun. Not a serious competition like TopCoder. I’ve clearly marked the files as solutions, to tell people what they’re downloading.

I’ve tried to make it learning oriented, by posting general algorithms.

Don’t people post solutions in the problem thread ?

## Matthew Booth

/ January 18, 2012Yes, but you can only see the problem thread once you’ve solved the problem. I wouldn’t say ‘unethical’, but not really in the spirit of the problem set.

## virajmahesh

/ January 19, 2012That’s true. I’ve removed the ‘Answers’ but I’m keeping the c++ solution.

If people are going to just compile other people’s source code to solve problems then it’s not going to be fun for them. Eventually, it would become a mindless exercise and they’d stop.

Discussing and posting code doesn’t hurt anyone.

## Paul Ogle

/ January 15, 2012Thanks so much, Following the simple Fibonacci outline and looking at where I went wrong in my code really helped out! Cheers!

## Mamajkowa

/ January 19, 2012Thanks so much, but I compiled the code and write ‘Answer’ to this problem, I take this same answer from my program. But I get ‘wrong’, I tried several times, but without positive. What could be wrong?

## virajmahesh

/ January 20, 2012I cant really say without looking at code.

Have you initialized sum to 0?

What answer are you getting ?

## Mamajkowa

/ January 20, 2012It’s my code in C#:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Problem2

{

class Program

{

static void Main(string[] args)

{

int[] tablica = new int[40];

tablica[1] = 1;

tablica[2] = 2;

int suma = 3;

for (int i = 3; i < tablica.Length; i++)

{

tablica[i] = tablica[i – 2] + tablica[i – 1];

if (tablica[i] < 4000000)

{

suma = suma + tablica[i];

Console.WriteLine(suma);

}

}

Console.ReadLine();

}

}

}

I get the answer 9227463, the same as from your code.

http://imageshack.us/f/72/problem2n.gif/

and always error http://imageshack.us/f/59/problem2error.gif/

## virajmahesh

/ January 20, 2012I think I know what the problem is!

If you read the problem carefully it says:

I don’t really know C# but from what I can read, you are not checking if the term is even. You seem to have mis-understood the line in my code:

`if(term_3%2 == 0 && term_3 <= 4000000)`

This line clearly checks if the term is even. Removing the condition

`term_3%2 == 0`

gives the answer you are getting, which is wrong, since the problem asks for the sum ofeventermsAlso, I find your choice of using array’s for this problem to be strange.

## Mamajkowa

/ January 20, 2012Oh… My bad. Thanks you very much, now it’s working!:)