All challenges
Easy 0 solved 0% acceptance

Sum Even Fibonacci Terms

Add the even Fibonacci terms that do not exceed a limit.

Problem

Begin the sequence with 1 and 2. Each following term is the sum of the previous two. Include only even-valued terms no greater than limit, then add them.

Implement even_fibonacci_sum(limit).

Input

limit: The largest term value that may be included.

Output

Return the sum, or 0 if there are no eligible terms.

Constraints

0 ≤ limit ≤ 10¹⁵. The result fits a signed 64-bit integer.

Examples

Example 1
Input:
limit = 50

Expected output:
expected = 44
Input:
long long limit = 50;

Expected output:
long long expected = 44;
Input:
long limit = 50;

Expected output:
long expected = 44;

Explanation

The even terms are 2, 8, and 34. Their sum is 44.

Example 2
Input:
limit = 1

Expected output:
expected = 0
Input:
long long limit = 1;

Expected output:
long long expected = 0;
Input:
long limit = 1;

Expected output:
long expected = 0;

Explanation

The first even term is 2, which exceeds the limit.

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.