Problem 69. Find the peak 3n+1 sequence value
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. See Problem 21 for more information.
Let c(n) be the sequence for n, and p(n) be the peak value of that sequence. For a given threshold nmax, find the highest peak value max(p(n)) for all Collatz sequences starting with integers between 1 and nmax.
Solution Stats
Problem Comments
-
11 Comments
Show
8 older comments
Meg Noah
on 14 Oct 2022
Broken link in description.
Dyuman Joshi
on 14 Oct 2022
Link has been updated.
xb
on 21 Aug 2023
why does it not work it can run successfully in matalb
function pmax = peakOfPeaks(nmax)
B=[]
for n=1:1:nmax
A=[];
if n==1
A=1
else A=n
end
while n~=1
if mod(n,2)==1
A=[A,3*n+1];
n=3*n+1
else A=[A,n/2];
n=n/2
end
end
B=[B,max(A)]
end
pmax=max(B)
end
Solution Comments
Show commentsProblem Recent Solvers2527
Suggested Problems
-
3413 Solvers
-
Read a column of numbers and interpolate missing data
2314 Solvers
-
Back to basics 19 - character types
265 Solvers
-
We love vectorized solutions. Problem 1 : remove the row average.
832 Solvers
-
Number of Even Elements in Fibonacci Sequence
1432 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!