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
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!
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
mantap jaya!!!
very easy
good
would you like a cookie?
:P
test
Just the sum of 1 to n
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.
I believe you can have a more efficient solution if you get rid of the intermediate variable A.
fancy pants
% correct solution
function triangle(n)
t=n;
clc
if 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
if 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
Not sure why it failed to evaluate? Not allow to use built-in functions?
i guess there is always a better solution to everything
>> sum(1:5)
ans =
15
>> sum(1:6)
ans =
21
>> sum(1:3)
ans =
6
:)
Good one
niec job mie u did rly weel
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
can anyone suggest how to improve the size of the code?
Too bad the computationally inferior solution is better in terms of points.
good
Well done!
simple and nice!
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.
Have no idea how I can have a size of 10!!
How to calculate the solution size ?
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.
Of course, this solution, while short, is NOT the best solution! Clearly the best solution is the far more efficient: n*(n+1)/2
1268 Solvers
Generate a vector like 1,2,2,3,3,3,4,4,4,4
1009 Solvers
155 Solvers
Switch matrix to a column vector
173 Solvers
386 Solvers