You are given a non-negative integer n. Your task is to determine whether the given number belongs to the Fibonacci sequence.
The Fibonacci sequence starts with 0 and 1. Each subsequent number is calculated as the sum of the two preceding numbers. Return True if n appears in the sequence; otherwise, return False.
Input: n = 21
Output: True
Explanation: 21 appears in the Fibonacci sequence, so the output is True.
Input: n = 22
Output: False
Explanation: 22 does not appear in the Fibonacci sequence, so the output is False.
Input: n = 0
Output: True
Explanation: 0 is the first number in the Fibonacci sequence, so the output is True.
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.
1
True
3
False