//https://en.wikipedia.org/wiki/Collatz_conjecture
//https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36

//3n + 1 Problem (printing all)
let min a b = if (a > b) then b else a
let max a b = if (b > a) then b else a
let f a b = 
    for j = (min a b) to (max a b) do
        let mutable i = j
        while (i > 1) do
            printf "%d " i
            if ((i % 2) = 0) then i <- i / 2 else i <- 3 * i + 1 
        printfn "1"
(*

> f 1 10;;
1
2 1
3 10 5 16 8 4 2 1
4 2 1
5 16 8 4 2 1
6 3 10 5 16 8 4 2 1
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
8 4 2 1
9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
10 5 16 8 4 2 1
val it : unit = ()

*)

//3n + 1 Problem (counting max)
let min a b = if (a > b) then b else a
let max a b = if (b > a) then b else a
let f a b = 
    let mutable max_length = 1
    for j = (min a b) to (max a b) do
        let mutable i = j
        let mutable count = 1
        while (i > 1) do            
            count <- count + 1
            if ((i % 2) = 0) then i <- i / 2 else i <- 3 * i + 1
        max_length <- (max max_length count)
    max_length
(*

> f 1 10;;
val it : int = 20
> f 100 200;;
val it : int = 125
> f 201 210;;
val it : int = 89
> f 900 1000;;
val it : int = 174

*)