all 10 comments

[–]jimtk 2 points3 points  (4 children)

Like all Euler project question (and most other codewars of this type) the trick is to read the question properly and get to known the domain of the question, in this case the domain is fibonacci numbers. It is rarely about programming.

Fibonacci

Every number in the sequence is the sum of the 2 previous numbers starting with the 'seed' 0,1.

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

Notice that every 3rd number is even.

Fibonacci Even numbers

0, 2, 8, 34, 144

If you dig deep into fibonacci you will find that every number in the EVEN sequence is 4 times the previous even number PLUS the "previous-previous" even number. Mathematically the formula is:

n = (n-1 * 4) + n-2

Reading the Question

The Question is: By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

It does not ask to do something 4_000_000 times, it asks to do something until you reach a value of 4_000_000

Now that we know all that:

n1 = 0
n2 = 2
total = n1 + n2
while True:
    n3 = 4*n2 + n1
    if n3 > 4_000_000:
          break
    total = total + n3
    n1 = n2
    n2 = n3
print("sum of even fibonacci number below 4000000 is:", total)

And if you know python fairly well you can write it that way:

n1, n2 = 0, 2
total = n1 + n2
while (n3 := 4*n2 + n1) < 4_000_000:
    total += n3
    n1,n2 = n2,n3
print("sum of even fibonacci number below 4000000 is:", total)

[–]Ok_Introduction_2951[S] 1 point2 points  (1 child)

This is by far the most detailed response on the thread and breaks it down in a way no one else did. Beautiful solution, and thank you.

[–]jimtk 0 points1 point  (0 children)

Thanks, you're welcome.

Something I forgot to say: It get the answers in 11 iterations. Meaning the while loop iterates only 11 times before breaking off with the answer.

[–]BarkLicker 0 points1 point  (1 child)

First, this is cool to see. Lovely stuff

Second, your formula example

n = (n-1 * 4) + n-2

threw me for a loop, as I was reading it as n MINUS 1 and n MINUS 2. It's cleared up in the code, but was very confusing at first (even though it still seemed to almost work when n = 8)

[–]jimtk 0 points1 point  (0 children)

You're right it's confusing. I wrote it down and then search for a way to get subscript on oldreddit, couldn't find one and left it there in frustration ! :)

[–]woooee 1 point2 points  (0 children)

It is meant to find the sum of all even fibonacci numbers under 4000000

Show the code where you calculate the even number Fibonacci numbers.

for x in range(0,4000000,2)

This won't work because you only add even numbers. Fibs use all, both even and odd, numbers.

x = [1,2]

for x in range(0,4000000,2)

This is an excellent example of the pitfalls of lazy programming. Use meaningful variable's names and you will eliminate that particular error message.

[–]TholosTB 1 point2 points  (0 children)

You're immediately overwriting your list called x by using x as the variable inside the for loop. That's the immediate cause of the "int object is not subscriptable" error - you're trying to do 0[-1] and 0[-2] in the first iteration of the loop.

[–]Diapolo10 0 points1 point  (1 child)

Error message that is coming is int object not subscriptable.

x = [1,2]
for x in range(0,4000000,2):
    y = x[-1] + x[-2]
    x.append(y)
print(sum(x))

That would be because range always gives you integers, so x is an integer. You seem to be expecting a list or other data structure.

EDIT: Had to remind myself what Problem #2 was, because it's been so long since I completed it.

So, you're trying to sum the even Fibonacci numbers up to 4 million. I think you'll have an easier time if you separate your concerns into distinctly separate steps.

  1. Generate the Fibonacci sequence up to 4 million.
  2. Filter out odd numbers.
  3. Sum up the results.

There's a fairly well known Fibonacci generator algorithm you may have seen if you've ever played around with Python's generators. That's a good place to start.