Problem 21. Return the 3n+1 sequence for n
Solution Stats
Problem Comments
-
5 Comments
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.
Solution Comments
-
2 Comments
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.
-
3 Comments
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.
-
1 Comment
why this will go wrong?
-
1 Comment
very easy
-
1 Comment
How to reduce it's size?
-
1 Comment
This solution does not work for n = 3. Test Suite should include n = 3.
-
1 Comment
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?
-
1 Comment
How can I reduce the size? Too many loops, yet I have to define the single commands :/
-
1 Comment
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.
-
3 Comments
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)
-
1 Comment
This is a poor solution
-
4 Comments
fantastic..I never thought of that...
Very clever
Nice
different approach but it used different functions frquenctly
-
1 Comment
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.
-
1 Comment
I really think the test set should go to much higher arguments. This sort of thing should not be allowed!
-
2 Comments
This is way more efficient than the recursive algorithms.
How?
-
4 Comments
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!:)
Problem Recent Solvers7384
Suggested Problems
-
971 Solvers
-
Project Euler: Problem 1, Multiples of 3 and 5
2508 Solvers
-
Return unique values without sorting
829 Solvers
-
Pernicious Anniversary Problem
786 Solvers
-
414 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!