module Collatz exposing (..)
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (id, class, style)
import Hex
type alias Model =
{
}
initModel : Model
initModel =
{
}
type Msg
= NoOp
box : Int -> List Int -> Html Msg
box n p =
let
truval = List.length p
val = round (atan (toFloat truval) * 2670177)
code = "#" ++ (String.padLeft 6 '0' (Hex.toString val))
fore =
if truval < 127 then
"#ffffff"
else
"#000000"
in
div
[ class "box"
, style
[ ("background-color", code)
, ("color", fore)
]
]
[ text (toString n) ]
collatzSteps : Int -> Int
collatzSteps n =
let
inner (n, c) =
case n of
1 ->
(n, c)
_ ->
if (rem n 2) == 0 then
inner ((n // 2), (c + 1))
else
inner ((n * 3 + 1), (c + 1))
(one, steps) = inner (n, 0)
in
steps
collatzPath : Int -> List Int
collatzPath n =
let
helper n p =
case n of
1 ->
(n :: p)
_ ->
if (rem n 2) == 0 then
helper (n // 2) (n :: p)
else
helper (n * 3 + 1) (n :: p)
in
helper n []
view : Model -> Html Msg
view model =
div
[]
<|
List.map (\n -> (box n (collatzPath n))) (List.range 1 777)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
( model, Cmd.none )
--subscriptions : Model -> Sub Msg
--subscriptions model =
-- Sub.batch
-- [
-- ]
main =
Html.program
{ init = (initModel, Cmd.none)
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}