import qualified Data.Array
import Data.Word
import Data.List
import Data.Array.ST
import Data.Array.MArray
import Data.Array.IArray
upperbound = 1000000
collatz_array_mutable =
runSTUArray ( do
arr
<- newArray
(1, upperbound
) maxBound :: ST s
(STUArray s Word64 Word16
) writeArray arr 1 0
forM_ [1..upperbound] (\i ->
do
{
let
next
_i i
= f
((if (even i
) then i
else 3 * i
+ 1) `
div`
2) f i =
do
if i > upperbound
then
do
new_elem <- next_i i
else
do
then
do
new_elem <- next_i i
writeArray arr i (new_elem + 1)
else
in
f i
}
)
)
main =
foldl1' (\(x1,x2) (y1,y2) -> if (x2 >= y2) then (x1, x2) else (y1, y2)) $! (assocs collatz_array_mutable)
aW1wb3J0IHF1YWxpZmllZCBEYXRhLkFycmF5CmltcG9ydCBEYXRhLldvcmQKaW1wb3J0IERhdGEuTGlzdAppbXBvcnQgRGF0YS5BcnJheS5TVAppbXBvcnQgRGF0YS5BcnJheS5NQXJyYXkKaW1wb3J0IENvbnRyb2wuTW9uYWQuU1QKaW1wb3J0IENvbnRyb2wuTW9uYWQKaW1wb3J0IERhdGEuQXJyYXkuSUFycmF5Cgp1cHBlcmJvdW5kID0gMTAwMDAwMAoKY29sbGF0el9hcnJheV9tdXRhYmxlID0KICBydW5TVFVBcnJheSAoIGRvCiAgICBhcnIgPC0gbmV3QXJyYXkgKDEsIHVwcGVyYm91bmQpIG1heEJvdW5kIDo6IFNUIHMgKFNUVUFycmF5IHMgV29yZDY0IFdvcmQxNikKICAgIHdyaXRlQXJyYXkgYXJyIDEgMAogICAgZm9yTV8gWzEuLnVwcGVyYm91bmRdIChcaSAtPiAKICAgICAgZG8KICAgICAgICB7CiAgICAgICAgICBsZXQKICAgICAgICAgICAgbmV4dF9pIGkgPSBmICgoaWYgKGV2ZW4gaSkgdGhlbiBpIGVsc2UgMyAqIGkgKyAxKSBgZGl2YCAyKQogICAgICAgICAgICBmIGkgPSAKICAgICAgICAgICAgICBkbwogICAgICAgICAgICAgICAgaWYgaSA+IHVwcGVyYm91bmQKICAgICAgICAgICAgICAgICB0aGVuCiAgICAgICAgICAgICAgICAgIGRvCiAgICAgICAgICAgICAgICAgICAgbmV3X2VsZW0gPC0gbmV4dF9pIGkKICAgICAgICAgICAgICAgICAgICByZXR1cm4gKG5ld19lbGVtICsgMSkKICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgIGRvCiAgICAgICAgICAgICAgICAgICAgZWxlbSA8LSByZWFkQXJyYXkgYXJyIGkKICAgICAgICAgICAgICAgICAgICBpZiBlbGVtID09IG1heEJvdW5kCiAgICAgICAgICAgICAgICAgICAgIHRoZW4KICAgICAgICAgICAgICAgICAgICAgIGRvCiAgICAgICAgICAgICAgICAgICAgICAgIG5ld19lbGVtIDwtIG5leHRfaSBpCiAgICAgICAgICAgICAgICAgICAgICAgIHdyaXRlQXJyYXkgYXJyIGkgKG5ld19lbGVtICsgMSkKICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIChuZXdfZWxlbSArIDEpCiAgICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgIHJldHVybiBlbGVtCiAgICAgICAgICBpbgogICAgICAgICAgICBmIGkKICAgICAgICB9CiAgICAgICkKICAgIHJldHVybiBhcnIKICApCiAgCm1haW4gPSAKICBwdXRTdHJMbiAkIHNob3cgJCAKICAgIGZvbGRsMScgKFwoeDEseDIpICh5MSx5MikgLT4gaWYgKHgyID49IHkyKSB0aGVuICh4MSwgeDIpIGVsc2UgKHkxLCB5MikpICQhIChhc3NvY3MgY29sbGF0el9hcnJheV9tdXRhYmxlKQo=