Alle Herausforderungen
Einfach 1 gelöst 25.0% Erfolgsquote

Sum Even Fibonacci Terms

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

Das lernst du

  • Generate Fibonacci terms up to a limit
  • Keep only the even terms
  • Stop the loop at the right moment

Problem

Übersetzung anzeigen

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.

Einschränkungen

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

Beispiele

Beispiel 1
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.

Beispiel 2
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.

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

You only need the even Fibonacci terms, not the entire sequence.

Hinweis 2

Generate the sequence until the next term exceeds the limit and add qualifying terms.

Hinweis 3

Watch the boundary when the limit itself is a Fibonacci term.

Erwartete Komplexität

O(log limit) time, O(1) extra space

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

# Coder Sprache Zeit Speicher KI-Bewertung Lösung
1 HJhettory Jurash Python 1,177 ms - 3/5 Code ansehen

Von anderen lernen

Community-Lösungen

Achtung Spoiler: 1 vollständige Lösung unten Versuche es zuerst selbst zu lösen. Du kannst trotzdem einen Blick auf den Code und die KI-Reviews anderer Entwickler werfen. Melde dich an und löse die Aufgabe, um an der Diskussion teilzunehmen.