Triangular Number Series - Problem
Given a positive integer n, generate the first n triangular numbers.
A triangular number is the sum of all natural numbers up to a given position. The sequence starts as: 1, 3, 6, 10, 15, 21, 28, ...
For example:
- 1st triangular number:
1(sum of 1) - 2nd triangular number:
3(sum of 1+2) - 3rd triangular number:
6(sum of 1+2+3) - 4th triangular number:
10(sum of 1+2+3+4)
Return the result as an array of integers.
Input & Output
Example 1 — First 5 Triangular Numbers
$
Input:
n = 5
›
Output:
[1, 3, 6, 10, 15]
💡 Note:
T₁=1, T₂=1+2=3, T₃=1+2+3=6, T₄=1+2+3+4=10, T₅=1+2+3+4+5=15
Example 2 — First 3 Triangular Numbers
$
Input:
n = 3
›
Output:
[1, 3, 6]
💡 Note:
First triangular: 1, second: 1+2=3, third: 1+2+3=6
Example 3 — Edge Case Single Number
$
Input:
n = 1
›
Output:
[1]
💡 Note:
Only the first triangular number, which is 1
Constraints
- 1 ≤ n ≤ 1000
- Result fits in 32-bit integer
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code