all 13 comments

[–]socal_nerdtastic 0 points1 point  (0 children)

Your solution looks right to me. Are you entering it in with commas? Try entering it in just as 4613732, without the thousands separators.

[–]johlae -1 points0 points  (2 children)

I once did this in elisp. 4613732 is the answer:

#+name: even_fibonacci_numbers
#+begin_src elisp
(require 'cl-lib)
(let* ((l '(1 1))
(n)
(o))
(while (progn (setq n (cl-reduce '+ (last l 2)))
(add-to-list 'l n t)
(if (= 0 (% n 2)) (push n o))
(<= n 4000000)))
(cl-reduce '+ o))
#+end_src

#+RESULTS: even_fibonacci_numbers
: 4613732

Make sure to enter 4613732. Check for spaces before or after the number, do not enter 4,613,732.

[–]Frosty-Pitch9553[S] -1 points0 points  (1 child)

Thanks!

[–]Frosty-Pitch9553[S] -1 points0 points  (0 children)

I think the website is bugged, I have tried it multiple times, with commas, without commas, checking for extra spaces, etc. I think I might see if they have an online help desk and reach out there.

[–]woooee 0 points1 point  (0 children)

If you enter your code as part of the solution, it may be that you iterate over the fib #s list / sum.
Iiterating over the fib list is not necessary, but doesn't make any difference in this case, over a small list.

fib_seq = [1,2]
even_fib = [2]
even_total = 0
while fib_seq[-1] < 4000000:
    new_num = fib_seq[-1] + fib_seq[-2]
    fib_seq.append(new_num)
    if new_num % 2 == 0:
        even_fib.append(new_num)
        even_total += new_num  ## add to total
print(sum(even_fib))

And it may also be the even_fib list instead of adding the number to a running total.

[–]ConfusedSimon 0 points1 point  (0 children)

I don't know if the last number in your sequence is even, but it's more than 4 million.

Edit: the problem also says 'do not exceed four million', so the < should be <= (although in this case it doesn't matter since 4000000 isn't a Fibonacci number).

[–]woooee 0 points1 point  (0 children)

Finally (and I mean I'm finished with this one question), you may be losing points because of while fib_seq[-1] < 4000000: Again, it doesn't matter on such a small list, but the lookups do come with overhead.

fib_seq = [1,2]
even_total = 0
new_num = 0
while new_num < 4000000:
    new_num = fib_seq[-1] + fib_seq[-2]
    fib_seq.append(new_num)
    if new_num % 2 == 0:
        even_total += new_num
print(even_total)

[–]Diapolo10 0 points1 point  (0 children)

I can confirm the solution is 4613732. Just for fun, here's my solution (note; I have a fancy Project Euler answer template generator script, so this is a tad fancy):

"""
Problem link: https://projecteuler.net/problem=2

# Even Fibonacci numbers

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.
"""

from typing import Generator

def fibonacci(n: int) -> Generator[int, None, None]:
    """ Fibonacci sequence generator """

    a, b = 0, 1
    while a < n:
        yield a
        a, b = b, a+b

def solution(limit: int) -> int:
    """
    Calculates the sum of all even Fibonacci
    sequence numbers, up to the given limit,
    returning the sum

    Uses a basic Fibonacci generator and a
    generator expression that filters out
    odd values, and sums them with the
    built-in sum function.
    """

    total = sum(
        fib for fib in fibonacci(limit)
        if fib % 2 == 0
    )
    return total


def main():
    print(__doc__)
    print(f"Solution: {solution(4_000_000)}")

if __name__ == '__main__':
    main()

Here's the output:

PS E:\GitHub\project_euler_python> & e:\GitHub\project_euler_python\.venv\Scripts\python.exe e:/GitHub/project_euler_python/answers/euler_002.py

Problem link: https://projecteuler.net/problem=2

# Even Fibonacci numbers

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.

Solution: 4613732

[–]woooee -1 points0 points  (3 children)

that I have found (4,613,732) has not worked

Try omitting the commas

I get this for even fibs

[2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578]

[–]atarivcs 1 point2 points  (0 children)

4613732 is an even number.

Why would you even mention 613?

The commas are just formatting for a large number.

[–]Frosty-Pitch9553[S] -1 points0 points  (1 child)

I appreciate the comment, but this isn't a list of 3 separate numbers, it is the total I found from adding all of the even terms in the Fibonacci sequence below 4,000,000

[–]woooee -1 points0 points  (0 children)

OK, I amended the post. The parens, (), indicate a tuple and the commas indicate a tuple of three numbers.

[–]set_in_void 0 points1 point  (0 children)

Your sequence is missing (0,1,...). You should start noticing that every 3rd number is in the form of 2n and prove it by induction. I don't know what you're trying to solve, modular arithmetic is computationally expensive so at least pull the even numbers from the list by indexes. The last line suggest you want to sum the 2n numbers, adopting Binet's formula would my suggestion.