getSequence = function(n) results = [n] while n > 1 if n % 2 then n = 3 * n + 1 else n = n / 2 end if results.push n end while return results end function h = getSequence(27) print "The hailstone sequence for 27 has 112 elements starting with" print h[:4] print "and ending with" print h[-4:] maxSeqLen = 0 maxSeqVal = 0 for i in range(1,100000) h = getSequence(i) if h.len > maxSeqLen then maxSeqLen = h.len maxSeqVal = i end if end for print print "The number < 100,000 which has the longest hailstone sequence is " + maxSeqVal + "." print "This sequence has " + maxSeqLen + " elements."