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
Solution Comments
Show comments
Loading...
Problem Recent Solvers30011
Suggested Problems
-
1533 Solvers
-
529 Solvers
-
549 Solvers
-
979 Solvers
-
Number of Even Elements in Fibonacci Sequence
1633 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
i dint realize i was writing a code for sum of first n natural numbers instead of computing n*(n+1)/2
Man, this takes me back to algebra... Its more fun in MATLAB tho!
It's interesting to work with and building the confidence to solve problems....nice
Good one. Reminds me of those old school days..!!
All solutions with score 10 or 11 use the regexp cheat.
Jon or anybody!
Can you explain how the regexp cheat works?
How do you reduce the size of your code?
mucha fantasía en el titulo
what it means?
I really had to use my brain for this problem
Interesting!
sound simple but annoying
Funny and interesting problem!
good!
For clarity sake,
t = triangle (n)
We need to calculate t for a given n
It is pretty easy but tricky!
Why doesn't this work?
function t = triangle(n)
t=1
for i=[1:(n+1)]
t=t+i;
t
end
that was fun
Cool problem!
Interesting
cool
The formula makes this an easy math problem.
Good Question
+10 points
Good question :)
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!