Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Check Whether a Number Belongs to the Fibonacci Sequence

Basic Acceptance 74.95% Points 10

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.

Examples
Example 1

Input: n = 21

Output: True

Explanation: 21 appears in the Fibonacci sequence, so the output is True.

Example 2

Input: n = 22

Output: False

Explanation: 22 does not appear in the Fibonacci sequence, so the output is False.

Example 3

Input: n = 0

Output: True

Explanation: 0 is the first number in the Fibonacci sequence, so the output is True.

Constraints
  • 0 <= n <= 1018
Topics
Data Structures Basics Basics
Companies
Samsung Intuit
Added 01 Nov 2026 74.95% acceptance
Ln 1, Col 1Sign in to save your code
1
True