Problem
Compute the sum of the first n terms of the Fibonacci sequence starting with 0, 1. In this practice version n = 0 returns 0. Use a numeric type that can hold the result.
Worked examples
Input: n = 6
Output: 12
0 + 1 + 1 + 2 + 3 + 5 = 12.
Hints
Hint 1
You only need the previous two terms.
Solution approach
- Track a = 0, b = 1 and total = 0. Repeat n times: add a to total, then advance the pair.
- Clarify whether the sequence starts at 0 or 1 and whether a modulus is required.
- Python integers grow as needed; fixed-width languages must handle overflow for the stated input range.
Python reference implementation
def fibonacci_sum(n):
a, b, total = 0, 1, 0
for _ in range(n):
total += a
a, b = b, a + b
return totalComplexity
O(n) recurrence steps and O(1) integer variables; big-integer arithmetic grows with the result bit length.
Report & practice notes
Restated practice version with original examples and explanation. The source is a candidate account, not an official question paper; assessment details can vary. Difficulty is our editorial estimate.
Read the candidate’s source report ↗