29 April 2024 Kolkata (Q1) — Fibonacci Sum up to N Terms
E · easyP · Verified PYQarraysmathseries
Problem
Generate the Fibonacci series and find the sum of its elements up to N terms. F(1)=1, F(2)=1, F(n)=F(n-1)+F(n-2). Print the sum of first N Fibonacci numbers.
Example
Input
7
Output
33
Generate Fibonacci iteratively. Accumulate the sum as you go. O(N) time O(1) space.
Fibonacci: 1,1,2,3,5,8,13. Sum = 1+1+2+3+5+8+13 = 33