Problem 5. Triangle Numbers
Triangle numbers are the sums of successive integers. So 6 is a triangle number because
6 = 1 + 2 + 3
which can be displayed in a triangular shape like so
*
* *
* * *
Thus 6 = triangle(3). Given n, return t, the triangular number for n.
Example:
Input n = 4
Output t is 10
Solution Stats
Problem Comments
-
26 Comments
Show
23 older comments
Ian
on 11 Dec 2022
+10 points
Anirban Kar Roy
on 5 Apr 2025
Good question :)
yuyuyu
on 25 Apr 2025 at 11:25
You might know this as the Triangular Number formula—or as some folks call it, the Gauss Summation trick—for adding numbers 1 through n super fast!
Solution Comments
Show commentsGroup

Introduction to MATLAB
- 23 Problems
- 8731 Finishers
- Add two numbers
- Find the sum of all the numbers of the input vector
- Maximum value in a matrix
- Return area of square
- Finding Perfect Squares
- Make the vector [1 2 3 4 5 6 7 8 9 10]
- Generate a vector like 1,2,2,3,3,3,4,4,4,4
- Triangle Numbers
- Length of the hypotenuse
- Select every other element of a vector
- Reverse the vector
- Column Removal
- Swap the input arguments
- Swap the first and last columns
- Check if number exists in vector
- Determine whether a vector is monotonically increasing
- Getting the indices from a vector
- Create times-tables
- Return the first and last characters of a character array
- Number of 1s in a binary string
- Make a random, non-repeating vector.
- Magic is simple (for beginners)
- Sum all integers from 1 to 2^n
Problem Recent Solvers28696
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!