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.
n = 13
c = [13 40 20 10 5 16 8 4 2 1]
Hep, Cody Team, could you remove my solution please?
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?
how can i simplify??
why this will go wrong?
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...
different approach but it used different functions frquenctly
this function collatz(N) produces specific values for N=10000, try this :
% more interesting figure, derivate N
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.
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];
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!:)
Right and wrong
Project Euler: Problem 7, Nth prime
Back to basics 19 - character types
Is it an Armstrong number?
Beginner's Problem - Squaring
Prime factor digits
Make a checkerboard matrix
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office