Tất cả thử thách
Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Sum Even Fibonacci Terms

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

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.

Ràng buộc

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

Ví dụ

Ví dụ 1
Đầu vào:
limit = 50

Kết quả mong đợi:
expected = 44
Đầu vào:
long long limit = 50;

Kết quả mong đợi:
long long expected = 44;
Đầu vào:
long limit = 50;

Kết quả mong đợi:
long expected = 44;

Giải thích

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

Ví dụ 2
Đầu vào:
limit = 1

Kết quả mong đợi:
expected = 0
Đầu vào:
long long limit = 1;

Kết quả mong đợi:
long long expected = 0;
Đầu vào:
long limit = 1;

Kết quả mong đợi:
long expected = 0;

Giải thích

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.