A Collatz sequence is the sequence where, for a given number n, the next number in the sequence is either n/2 if the number is even or 3n+1 if the number is odd. The sequence always terminates with 1.
So if
n = 13
then
c = [13 40 20 10 5 16 8 4 2 1]
Hep, Cody Team, could you remove my solution please?
Thank you!
nice one but consumes size dunno how to reduce it
a bit tricky but nice
I like this problem, because I learnt about the Collatz sequence story. Very interessting.
What's wrong in this code?
the assignment c = [c,a]; must be out of the else clause, but still inside of the while block.
how can i simplify??
Try this！:D
c=n;
while n>1
if mod(n,2)
n =3*n+1;
else
n =n/2;
end
c=[c n];
end
Thats right> construct c thru concat operator [..] instead of indexing it.
Index "i" doesn't care because it don't care the size of the sequence.
why this will go wrong?
very easy
How to reduce it's size?
This solution does not work for n = 3. Test Suite should include n = 3.
Hi all. I have no idea why there are many outputs of ans (the last of which is the right one), can anyone help to explain?
How can I reduce the size? Too many loops, yet I have to define the single commands :/
I don't think you should hard code the lists you should be constructing. Regex is fine, but you're limiting yourself by making such a narrow solution.
I personally don't like the regex solution because I'm trying to find interesting things about the language and I don't want to run through and find the shortest non regexp.
Now give me 500$
Would some kind soul enlighten me please?
it works fine on matlab when i assign 'c' a value
disp(c) contains the correct answer,unlike c. c is always equal to 1(last value of the iteration process)
This is a poor solution
fantastic..I never thought of that...
Very clever
Nice
different approach but it used different functions frquenctly
this function collatz(N) produces specific values for N=10000, try this :
----------------------
N=collatz(1000);
plot(N);
-----------------------
% more interesting figure, derivate N
-----------------------
dN=diff(N);
figure,plot(dN)
-----------------------
dN looks like a sample of voice signal.
I really think the test set should go to much higher arguments. This sort of thing should not be allowed!
This is way more efficient than the recursive algorithms.
How?
Nice, David!
Nice indeed.
I am surprised that it is more efficient to solve this with a recursive function than a while loop.
This solution has size 43:
while n(end) > 1
n = [n mod(n(end),2)*(n(end)*2.5+1) + n(end)/2];
end
Is the loop overhead greater than the recursion overhead? Or is the weak point having to go to n(end) at each iteration?
Strange I can not delete my first comment. I wanted to format it a bit nicer :)
Good solution, David!:)