Editor

Triangular Numbers

#526 · Loops · Beginner · 25 XP · about 4 min

Bowling pins are set up in a triangle: 1 pin in the first row, 2 in the next, 3 after that, and so on. The number of pins in a triangle with k rows is called a triangular number. The first ones are 1, 3, 6, 10 and 15: each is the one before it plus the pins of the new row.

Write triangular_numbers(n) that returns the first n triangular numbers as a list. For n = 0 return an empty list.

Examples

triangular_numbers(5)  # returns [1, 3, 6, 10, 15]
triangular_numbers(1)  # returns [1]
triangular_numbers(0)  # returns []

The fourth number is 10 because a triangle with 4 rows holds 1 + 2 + 3 + 4 = 10 pins.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.