Several Cody problems (21, 69, 42937, 44448, 44784, 52422) involve Collatz sequences. These start with a seed n. If n is odd, the next element is
, and if n is even, the next element is
. For example, if the seed is 3, then the sequence is 3, 10, 5, 16, 8, 4, 2, 1. The Collatz conjecture is that all of these sequences terminate at the value 1; that is, all seeds lead to terminating sequences.
This problem deals with the number of primes in the sequence. With a seed of 3, the number of primes is 3 (2, 3, 5).
Write a function to determine the numbers of primes in the sequences with seeds of 1 to the input number. See the test suite for banned terms and commands.
Solution Stats
Problem Comments
4 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers20
Suggested Problems
-
Remove any row in which a NaN appears
8768 Solvers
-
588 Solvers
-
Smallest distance between a point and a rectangle
275 Solvers
-
504 Solvers
-
138 Solvers
More from this Author321
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
I notice that p=isprime(x) can be a lot slower than p=ismember(x,primes(max(x))); for x=1:1e7, the former takes about 100 seconds and the latter takes about 0.1 seconds.
Nice observation. I'll keep that one in mind.
Actually for the first case (seed n = 1), the answer should be 1 and not 0, since the corresponding Collatz sequence is the loop (1, 4, 2), should'nt it be ?
Ok I suppose we don't count the special case of this infinite loop since we do have to stop the sequence at a point ! (1)