You are given two integers L and R. Your task is to return the sum of all Fibonacci numbers that lie within the inclusive range [L, R].
The Fibonacci sequence starts with 0 and 1. Each subsequent number is calculated as the sum of the two preceding numbers. Include every Fibonacci number that falls between L and R, including both endpoints. If no Fibonacci number lies in the given range, return 0.
Input: L = 1, R = 10
Output: 20
Explanation: The Fibonacci numbers 1, 1, 2, 3, 5, 8 lie within the range. Their sum is 20.
Input: L = 5, R = 20
Output: 26
Explanation: The Fibonacci numbers 5, 8, and 13 lie within the range. Their sum is 26.
Input: L = 100, R = 120
Output: 0
Explanation: No Fibonacci number lies within the range, so the sum is 0.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
0 10
20
2 10
18