collatz(n) := block([L], L: [n], while n > 1 do (n: if evenp(n) then n/2 else 3*n + 1, L: endcons(n, L)), L)$ collatz_length(n) := block([m], m: 1, while n > 1 do (n: if evenp(n) then n/2 else 3*n + 1, m: m + 1), m)$ collatz_max(n) := block([j, m, p], m: 0, for i from 1 thru n do (p: collatz_length(i), if p > m then (m: p, j: i)), [j, m])$ collatz(27); /* [27, 82, 41, ..., 4, 2, 1] */ length(%); /* 112 */ collatz_length(27); /* 112 */ collatz_max(100000); /* [77031, 351] */