Problem 5. Triangle Numbers
Solution Stats
Problem Comments

24 Comments
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
Solution Comments

1 Comment
easy!

1 Comment
The definition for triangular numbers should be rewritten, it is not clear from the start that is meant sum of consecutive numbers from 1 to n

1 Comment
good problem!

1 Comment
I agree with @Aravind C G. I have tried sum() and (1+n)*n/2 by 100000000. When I used sum() to get the result, my computer was dead, and (1+n)*n/2 spend 0.000734s.

1 Comment
I just really can"t get how people come up with smaller solutions, crazy!!!!

1 Comment
good problem

2 Comments
Cool problem!
GOOD

1 Comment
nice

1 Comment
These problems are interesting!

2 Comments
Hi All,
Can anyone tell me what's wrong with this code?
function t = triangle(n)
sum=0:
for i=1:n
sum=sum+i
end
t =sum ;
end
I checked this code with test cases as n=1,3,5,30 in my MATLAB and I'm getting the desired result.
n=input("Enter a number")
sum=0;
for i=1:n
sum=sum+i
end
t =sum
your triangle function has the statement sum = 0:
your test code has the statement sum = 0;
you have a typo, : versus ;
:)
function t = triangle(n)
sum=0;
for i=1:n
sum=sum+i;
end
t =sum;
end
PS better code would be
function t = triangle(n)
t = sum(1:n);
end

2 Comments
nice!
good

1 Comment
I know it but I can't remember it. i think i have to study it again.

1 Comment
From Viet Nam with love

1 Comment
Sum of an Arithmetic Progression will get this problem done.

7 Comments
function ans = triangle(n)
! echo "function assert(~)" > ./assert.m
% Cody Team, please restric usage of regexp and !
end
????
?
Phillippe, you have ruined this resource by solving every problem with this answer. PLEASE DELETE
why o why do I have to compete against a trick that doesnt solve the problem in a proper way?
MATLAB get to work and clean out these 'solutions' !
THIS IS NOT A LEADING SOLUTION:
function ans=triangle(n)
! echo "function assert(~)" > ./assert.m
end
Please remove this answer and post the correct leading solution.
Haha, this is funny! GG
Nice one !!!

3 Comments
Simple, yet nice.
I solved it bot by the best algorithm.
I also used this one. but while using other methods(not optimized), when i tried commands mentioned below, it gave "failed" status.
Can anybody tell why?
function t = triangle(n)
y= x:1:1;
y= sum(y)
end

1 Comment
mantap jaya!!!

1 Comment
very easy

2 Comments
good
would you like a cookie?

1 Comment
:P

1 Comment
test

2 Comments
Just the sum of 1 to n
t = sum(1:n)

2 Comments
How can the above solution be improved?
You can use Gauss formula for triangular numbers. It will improve performance. It won't improve Cody size. To improve Cody size, you need hack. Popular hack is to wrap the code into regular expression. Somebody invented it to cheat scoring algorithm and it spreaded among cody submissions like a cancer. I'm not proud of this, I was using this hack too, fortunately realised that it makes no sense. Advice: don't care so much about the size, care about quality code. Most of super small solutions use some sort of cheats, you won't learn much from those.

2 Comments
I believe you can have a more efficient solution if you get rid of the intermediate variable A.
fancy pants

2 Comments
% correct solution
function triangle(n)
t=n;
clc
if n >1
for l = 1:1:(t1)
l = l+1;
end
for i=1:1:l
for k = l:1:i
fprintf(' ');
end
for j = 1:1:i
fprintf('* ');
end
fprintf('\n');
end
else
fprintf(' *\n');
end
%please check why this is not compiling
function prac(n)
t=n;
clc
if n >1
for l = 1:1:(t1)
l = l+1;
end
for i=1:1:l
for k = l:1:i
fprintf(' ');
end
for j = 1:1:i
fprintf('* ');
end
fprintf('\n');
end
else
fprintf(' *\n');
end

1 Comment
Not sure why it failed to evaluate? Not allow to use builtin functions?

1 Comment
i guess there is always a better solution to everything

1 Comment
>> sum(1:5)
ans =
15
>> sum(1:6)
ans =
21
>> sum(1:3)
ans =
6

2 Comments
:)
Nice

1 Comment
Good one

1 Comment
niec job mie u did rly weel

1 Comment
I think this works for very low numbers but as you get higher, a work around that might be faster is
n*(n+1)/2

1 Comment
can anyone suggest how to improve the size of the code?

1 Comment
Too bad the computationally inferior solution is better in terms of points.

1 Comment
good

1 Comment
Well done!

1 Comment
simple and nice!

1 Comment
Compared this solution score to sum(1:n). Using tic toc and varying sizes of n repeated up to 1,000,000 times I found that n*(n+1)/2 is by far faster. On the order of 10,000 times faster.

1 Comment
Have no idea how I can have a size of 10!!

1 Comment
How to calculate the solution size ?

3 Comments
Okay, so we can definitely do better than a for loop...
@Micah Beckman no, this solution is not an "efficient" answer even though it scores high... one should use sum(1:n) to replace a for loop sum.
While people are tricking the scoring mechanism using regexp, sum(1:n) isn't the most efficient.
It's helpful to recognize that this is an arithmetic series starting at 1 and ending at n with an increments of 1. The sum of such an arithmetic series is n*(n+1)/2. You can see the difference if you use 'tic' 'toc' to time sum(1:n) and n*(n+1)/2 for very large n.

1 Comment
Of course, this solution, while short, is NOT the best solution! Clearly the best solution is the far more efficient: n*(n+1)/2
Problem Recent Solvers23152
Suggested Problems

Extract leading nonzero digit
1721 Solvers

2059 Solvers

Back to basics 9  Indexed References
427 Solvers

1237 Solvers

Sum of first n positive integers
526 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!