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!
Cool problem!
nice
These problems are interesting!
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
nice!
good
I know it but I can't remember it. i think i have to study it again.
From Viet Nam with love
Sum of an Arithmetic Progression will get this problem done.
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 !!!
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
t = sum(1: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.
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.
Of course, this solution, while short, is NOT the best solution! Clearly the best solution is the far more efficient: n*(n+1)/2
4938 Solvers
Find the sum of the elements in the "second" diagonal
1031 Solvers
301 Solvers
Determine the number of odd integers in a vector
488 Solvers
528 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!