Problem 52669. Count the primes in Collatz sequences
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
Show
1 older comment
Tim
on 8 Sep 2021
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.
ChrisR
on 11 Sep 2021
Nice observation. I'll keep that one in mind.
Nicolas Douillet
on 10 Jul 2025
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 ?
Nicolas Douillet
on 10 Jul 2025
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)
Solution Comments
Show commentsProblem Recent Solvers18
Suggested Problems
-
45119 Solvers
-
1290 Solvers
-
911 Solvers
-
Get the elements of diagonal and antidiagonal for any m-by-n matrix
496 Solvers
-
2138 Solvers
More from this Author310
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!