आसान 0 हल किए गए 0% स्वीकृति

Sum Even Fibonacci Terms

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

आप क्या सीखेंगे

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

समस्या

अनुवाद दिखाएँ

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.

सीमाएँ

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

उदाहरण

उदाहरण 1
इनपुट:
limit = 50

अपेक्षित आउटपुट:
expected = 44
इनपुट:
long long limit = 50;

अपेक्षित आउटपुट:
long long expected = 44;
इनपुट:
long limit = 50;

अपेक्षित आउटपुट:
long expected = 44;

व्याख्या

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

उदाहरण 2
इनपुट:
limit = 1

अपेक्षित आउटपुट:
expected = 0
इनपुट:
long long limit = 1;

अपेक्षित आउटपुट:
long long expected = 0;
इनपुट:
long limit = 1;

अपेक्षित आउटपुट:
long expected = 0;

व्याख्या

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

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

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

संकेत 2

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

संकेत 3

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

अपेक्षित जटिलता

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

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।