Editor

Fibonacci Sequence

#21 · Loops · Intermediate · 70 XP · about 7 min

Write a function called fibonacci(n) that returns a list of the first n Fibonacci numbers.

The Fibonacci sequence starts with 0, 1, and each subsequent number is the sum of the two preceding numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Example

fibonacci(5)   # Returns [0, 1, 1, 2, 3]
fibonacci(1)   # Returns [0]
fibonacci(2)   # Returns [0, 1]
fibonacci(10)  # Returns [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Requirements

  • Return a list starting from 0
  • Handle n = 1 and n = 2 as edge cases
  • You can assume n >= 1

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.