Problem
Übersetzung anzeigenBegin 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.
Einschränkungen
0 ≤ limit ≤ 10¹⁵. The result fits a signed 64-bit integer.
Beispiele
Eingabe:
limit = 50
Erwartete Ausgabe:
expected = 44
Eingabe:
long long limit = 50;
Erwartete Ausgabe:
long long expected = 44;
Eingabe:
long limit = 50;
Erwartete Ausgabe:
long expected = 44;
Erläuterung
The even terms are 2, 8, and 34. Their sum is 44.
Eingabe:
limit = 1
Erwartete Ausgabe:
expected = 0
Eingabe:
long long limit = 1;
Erwartete Ausgabe:
long long expected = 0;
Eingabe:
long limit = 1;
Erwartete Ausgabe:
long expected = 0;
Erläuterung
The first even term is 2, which exceeds the limit.
Referenzen
Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.