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 Solvers2526
Suggested Problems
-
8970 Solvers
-
How to find the position of an element in a vector without using the find function
2748 Solvers
-
Back to basics 22 - Rotate a matrix
910 Solvers
-
Sum of first n terms of a harmonic progression
444 Solvers
-
2172 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!