The Problem
The Algorithm
Generating the whole sequence and filtering on parity works, but two thirds of the terms are thrown away. The pattern to exploit: the Fibonacci sequence is odd, odd, even, odd, odd, even, … — every third term is even.
Those even terms form their own sequence — 2, 8, 34, 144, 610, … — with a closed recurrence, so you can walk them directly and never touch an odd term:
Fibonacci numbers grow exponentially, so there are only about a dozen even terms below four million — the loop is and does a third of the work of the filter-everything approach.
The Prompt
Solve Project Euler Problem 2 in Python. Sum the even-valued Fibonacci terms whose value does not exceed a given limit. Use the fact that every third Fibonacci number is even and satisfies E_k = 4*E_(k-1) + E_(k-2); walk the even terms directly rather than generating the whole sequence and filtering it. Signature: solve(limit: int) -> int, returning the sum. Print solve(4_000_000).
The Solutions
1def solve(limit: int) -> int:2 """Sum of even-valued Fibonacci numbers below `limit`."""3 total = 04 prev, curr = 2, 8 # every third Fibonacci number is even5 while prev < limit:6 total += prev7 prev, curr = curr, 4 * curr + prev # E_k = 4*E_(k-1) + E_(k-2)8 return total9 10 11print(solve(4_000_000))Results
All three summed to 4613732. GPT and Claude both skipped straight to the even-term recurrence — every third Fibonacci number is even and satisfies E_k = 4·E_(k−1) + E_(k−2) — so they touch only the ~11 even terms below four million. Gemini generated the full sequence and filtered on parity, doing roughly three times the work.
GPT took it by a hair: the same even-term loop as Claude but with the least ceremony — a single tuple update carrying the recurrence. Claude's was equally correct and a touch more explained. Gemini's parity filter is the textbook answer and perfectly fine — it just leaves the key optimisation on the table.