The Hailstone sequence of numbers can be generated from a starting positive integer,   n   by:

   If   n   is     1     then the sequence ends.
   If   n   is   even then the next   n   of the sequence    = n/2 
   If   n   is   odd   then the next   n   of the sequence    = (3 * n) + 1 


The (unproven) Collatz conjecture is that the hailstone sequence for any starting number always terminates.



This sequence was named by Lothar Collatz in 1937   (or possibly in 1939),   and is also known as (the):

   hailstone sequence,   hailstone numbers
   3x + 2 mapping,   3n + 1 problem
   Collatz sequence
   Hasse's algorithm
   Kakutani's problem
   Syracuse algorithm,   Syracuse problem
   Thwaites conjecture 
   Ulam's problem


The hailstone sequence is also known as   hailstone numbers   (because the values are usually subject to multiple descents and ascents like hailstones in a cloud).



Task

 Create a routine to generate the hailstone sequence for a number.
 Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
 Show the number less than 100,000 which has the longest hailstone sequence together with that sequence's length.
   (But don't show the actual sequence!)


See also

   xkcd (humourous).
   The Notorious Collatz conjecture Terence Tao, UCLA (Presentation, pdf).
   The Simplest Math Problem No One Can Solve Veritasium (video, sponsored).


HAI 1.3
 
HOW IZ I hailin YR stone
    I HAS A sequence ITZ A BUKKIT
    sequence HAS A length ITZ 1
    sequence HAS A SRS 0 ITZ stone
 
    IM IN YR stoner
        BOTH SAEM stone AN 1, O RLY?
            YA RLY, FOUND YR sequence
        OIC
 
        MOD OF stone AN 2, O RLY?
            YA RLY, stone R SUM OF PRODUKT OF stone AN 3 AN 1
            NO WAI, stone R QUOSHUNT OF stone AN 2
        OIC
 
        sequence HAS A SRS sequence'Z length ITZ stone
        sequence'Z length R SUM OF sequence'Z length AN 1
    IM OUTTA YR stoner
IF U SAY SO
 
I HAS A hail27 ITZ I IZ hailin YR 27 MKAY
VISIBLE "hail(27) = "!
 
IM IN YR first4 UPPIN YR i TIL BOTH SAEM i AN 4
    VISIBLE hail27'Z SRS i " "!
IM OUTTA YR first4
VISIBLE "..."!
 
IM IN YR last4 UPPIN YR i TIL BOTH SAEM i AN 4
    VISIBLE " " hail27'Z SRS SUM OF 108 AN i!
IM OUTTA YR last4
VISIBLE ", length = " hail27'Z length
 
I HAS A max, I HAS A len ITZ 0
 
BTW, DIS IZ RLY NOT FAST SO WE ONLY CHEK N IN [75000, 80000)
IM IN YR maxer UPPIN YR n TIL BOTH SAEM n AN 5000
    I HAS A n ITZ SUM OF n AN 75000
    I HAS A seq ITZ I IZ hailin YR n MKAY
    BOTH SAEM len AN SMALLR OF len AN seq'Z length, O RLY?
        YA RLY, max R n, len R seq'Z length
    OIC
IM OUTTA YR maxer
 
VISIBLE "len(hail(" max ")) = " len
 
KTHXBYE