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!:)
Test | Status | Code Input and Output |
---|---|---|
1 | Pass |
%%
n = 1;
c_correct = 1;
assert(isequal(collatz(n),c_correct))
|
2 | Pass |
%%
n = 2;
c_correct = [2 1];
assert(isequal(collatz(n),c_correct))
|
3 | Pass |
%%
n = 5;
c_correct = [5 16 8 4 2 1];
assert(isequal(collatz(n),c_correct))
|
4 | Pass |
%%
n = 22;
c_correct = [22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1];
assert(isequal(collatz(n),c_correct))
|
Arrange Vector in descending order
4080 Solvers
284 Solvers
Getting the indices from a matrice
360 Solvers
583 Solvers
302 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!