Project Euler – Problem 2

The Problem:

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.

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

Solution in C++

Leave a comment


  1. Anonymous

     /  January 14, 2012

    Really unethical to post solutions!

    • virajmahesh

       /  January 14, 2012

      Why ? 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 ?

      • Yes, 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, 2012

        That’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.

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

  3. Mamajkowa

     /  January 19, 2012

    Thanks 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, 2012

      I cant really say without looking at code.

      Have you initialized sum to 0?
      What answer are you getting ?

      • Mamajkowa

         /  January 20, 2012

        It’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];

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

      • I think I know what the problem is!

        If you read the problem carefully it says:

        find the sum of the even-valued terms

        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 of even terms

        Also, I find your choice of using array’s for this problem to be strange.

  4. Mamajkowa

     /  January 20, 2012

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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: