The Problem

Each new term in the Fibonacci sequence is the sum of the previous two, beginning 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …. By considering the terms whose values do not exceed , find the sum of the even-valued terms.

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

Prompt · sent identically to all three

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

Sent to
Claude
GPT
Gemini

The Solutions

×
−
⤢
1def solve(limit: int) -> int:
2 """Sum of even-valued Fibonacci numbers below `limit`."""
3 total = 0
4 prev, curr = 2, 8 # every third Fibonacci number is even
5 while prev < limit:
6 total += prev
7 prev, curr = curr, 4 * curr + prev # E_k = 4*E_(k-1) + E_(k-2)
8 return total
9 
10 
11print(solve(4_000_000))

Results

ANSWER
4613732
✓ all correct · 4613732
WINNER
GPT15/15 · leanest even-term loop

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.

THE BREAKDOWN
METRICCLAUDEGPTGEMINI
Instruction following
Verbosity
Truthfulness
Speed0.4 µs0.3 µs1.1 µs
Peak RAM9.1 MB9.1 MB9.2 MB
Cost$0.0129$0.0057$0.0028
JUDGED TOTAL14/1515/1512/15