Project Euler Problem 2

September 24th, 2008

Problem 2 was another easy one.

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, …

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

I brute forced it again, with a bit of optimisation.

We only need to add the even Fibonacci numbers, and if you look closely at the example above you might notice a pattern.

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144

An even number shows up every 3 numbers. So instead of calculating every number in the sequence below 4,000,000 and then test if it’s even, we can calculate every third number and add it straight onto the total. Here is what I came up with.

sum = 0
prev = 1
current = 2
while current < 4000000:
    sum = sum + current
    n = current
    current += 2 * (current + prev)
    prev += 2 * n
    print current
print sum

For fun I also did it in 4 lines. This way is very inefficient, but it is small. :P

a = [1, 2]
while a[len(a) - 1] < 4000000:
    a.append(a[len(a) - 1] + a[len(a) - 2])
print sum(filter(lambda x: x % 2 == 0, a))

Can anyone think of a shorter and/or more efficient way?

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • description
  • Furl
  • Slashdot
  • StumbleUpon
  • Technorati

6 Responses to “Project Euler Problem 2”

  1. Carmelo Henricksen Says:

    Hah Alles hat sein Für und Wider.

  2. Tawana Campble Says:

    thank you for such a fantastic website. Where else could someone get that kind of info written in such a perfect way? I have a presentation that I am presently working on, and I have been on the watch out for such information.

  3. Hochstuhl Says:

    The following time I learn a blog, I hope that it doesnt disappoint me as a lot as this one. I imply, I do know it was my option to read, but I actually thought youd have something attention-grabbing to say. All I hear is a bunch of whining about one thing that you would repair when you werent too busy looking for attention.

  4. Energy Monitoring Solutions Says:

    Eloquent…

    Silver-tongued diction in this blog post. I thought I was reading Ronald Reagan….

  5. slim weight patch Says:

    Hello! I recently would wish to give a massive thumbs up for your fantastic information you could have here on this post. I’ll be returning to your blog to get more detailed soon.

  6. moncler spaccio Says:

    A large percentage of of what you mention is astonishingly accurate and that makes me ponder the reason why I had not looked at this with this light previously. This particular piece really did turn the light on for me as far as this particular issue goes. But at this time there is one particular factor I am not really too comfy with so while I attempt to reconcile that with the actual central idea of your position, let me see exactly what the rest of your subscribers have to point out.Very well done.

Leave a Reply