22 lines
684 B
Python
22 lines
684 B
Python
#!/usr/bin/env python3
|
|
"""Project Euler Problem 2: Find sum of even Fibonacci numbers below 4,000,000."""
|
|
|
|
def sum_even_fibs(limit: int) -> int:
|
|
"""Return sum of even Fibonacci numbers less than limit."""
|
|
a, b = 1, 2
|
|
total = 0
|
|
while b < limit:
|
|
if b % 2 == 0:
|
|
total += b
|
|
a, b = b, a + b
|
|
return total
|
|
|
|
if __name__ == "__main__":
|
|
limit = 4_000_000
|
|
result = sum_even_fibs(limit)
|
|
print(f"Sum of even Fibonacci numbers below {limit}: {result}")
|
|
|
|
# Verification assertion
|
|
expected = 4613732
|
|
assert result == expected, f"Expected {expected}, got {result}"
|
|
print(f"VERIFIED: result matches expected value {expected}") |