Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Compute factorial recursivelyE02. Generate first N Fibonacci numbers recursivelyE03. Find all subsets of a setE

Problem No 2

Generate first N Fibonacci numbers recursively

Easy

8 minute session

Summary

Return the list of the first N Fibonacci numbers using recursion.

Problem statement

Generate the first N numbers of the Fibonacci sequence using recursion. The sequence starts with 0, 1, ...; for example, the first 5 numbers are [0, 1, 1, 2, 3]. For n = 0 return an empty list. Assume n >= 0.

Task

Implement fibonacci(n) that returns the first n Fibonacci numbers (starting from 0) using a recursive approach.

Examples

Example 1

Input

fibonacci(5)

Output

[0, 1, 1, 2, 3]

Explanation

First five Fibonacci numbers: 0, 1, 1, 2, 3

Input format

A single integer n supplied as an argument to fibonacci(n).

Output format

A Python list of the first n Fibonacci numbers.

Constraints

0 ≤ n ≤ 20 for test inputs. Use a recursive approach to build the sequence (a helper recursive function that returns the list is acceptable).

Samples

Sample input 0

fibonacci(6)

Sample output 0

[0, 1, 1, 2, 3, 5]

Explanation 0

First six Fibonacci numbers.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:49 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.