Problem 5. Triangle Numbers
Solution Stats
Problem Comments
-
21 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..!!
所有分数万博 尤文图斯10或11的解决方案都使用Regexp作弊。
乔恩or anybody!
Can you explain how the regexp cheat works?
如何减少代码的大小?
木栅幻想曲en el titulo
这是什么意思?
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
凉爽的
Solution Comments
-
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
我真的可以“ t人们如何提出较小的解决方案,疯狂!!!!万博 尤文图斯
-
1 Comment
good problem
-
2 Comments
Cool problem!
GOOD
-
1 Comment
好的
-
1 Comment
These problems are interesting!
-
2 Comments
大家好,
谁能告诉我此代码怎么了?
function t = triangle(n)
sum=0:
对于i = 1:n
sum=sum+i
end
t = sum;
end
我用测试用例检查了此代码,因为我的MATLAB中的n = 1,3,5,30,并且得到了理想的结果。
n =输入(“输入数字”)
sum=0;
对于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;
对于i = 1:n
sum=sum+i;
end
t =sum;
end
PS更好的代码我uld be
function t = triangle(n)
t = sum(1:n);
end
-
2 Comments
好的呢
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条评论
function ans = triangle(n)
呢echo“函数断言(〜)”> ./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开始工作并清理这些“解决方案”!万博 尤文图斯
THIS IS NOT A LEADING SOLUTION:
function ans=triangle(n)
呢echo“函数断言(〜)”> ./assert.m
end
Please remove this answer and post the correct leading solution.
哈哈,这很有趣!GG
好的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
如何改进上述解决方案?
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
如果n> 1
for l = 1:1:(t-1)
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
如果n> 1
for l = 1:1:(t-1)
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 built-in 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
:)
好的
-
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
太糟糕了,在点上,计算较低的解决方案更好。
-
1 Comment
good
-
1 Comment
做得好!
-
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
问题最近的求解器21610
Suggested Problems
1844 Solvers
Remove the polynomials that have positive real elements of their roots.
1055求解器
950 Solvers
Matrix indexing with two vectors of indices
589个求解器
336 Solvers
更多的from this Author95
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
开始狩猎!