Main.elm 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. module Collatz exposing (..)
  2. import Html exposing (..)
  3. import Html.Events exposing (..)
  4. import Html.Attributes exposing (id, class, style)
  5. import Hex
  6. type alias Model =
  7. {
  8. }
  9. initModel : Model
  10. initModel =
  11. {
  12. }
  13. type Msg
  14. = NoOp
  15. box : Int -> List Int -> Html Msg
  16. box n p =
  17. let
  18. truval = List.length p
  19. val = round (atan (toFloat truval) * 2670177)
  20. code = "#" ++ (String.padLeft 6 '0' (Hex.toString val))
  21. fore =
  22. if truval < 127 then
  23. "#ffffff"
  24. else
  25. "#000000"
  26. in
  27. div
  28. [ class "box"
  29. , style
  30. [ ("background-color", code)
  31. , ("color", fore)
  32. ]
  33. ]
  34. [ text (toString n) ]
  35. collatzSteps : Int -> Int
  36. collatzSteps n =
  37. let
  38. inner (n, c) =
  39. case n of
  40. 1 ->
  41. (n, c)
  42. _ ->
  43. if (rem n 2) == 0 then
  44. inner ((n // 2), (c + 1))
  45. else
  46. inner ((n * 3 + 1), (c + 1))
  47. (one, steps) = inner (n, 0)
  48. in
  49. steps
  50. collatzPath : Int -> List Int
  51. collatzPath n =
  52. let
  53. helper n p =
  54. case n of
  55. 1 ->
  56. (n :: p)
  57. _ ->
  58. if (rem n 2) == 0 then
  59. helper (n // 2) (n :: p)
  60. else
  61. helper (n * 3 + 1) (n :: p)
  62. in
  63. helper n []
  64. view : Model -> Html Msg
  65. view model =
  66. div
  67. []
  68. <|
  69. List.map (\n -> (box n (collatzPath n))) (List.range 1 777)
  70. update : Msg -> Model -> (Model, Cmd Msg)
  71. update msg model =
  72. ( model, Cmd.none )
  73. --subscriptions : Model -> Sub Msg
  74. --subscriptions model =
  75. -- Sub.batch
  76. -- [
  77. -- ]
  78. main =
  79. Html.program
  80. { init = (initModel, Cmd.none)
  81. , view = view
  82. , update = update
  83. , subscriptions = \_ -> Sub.none
  84. }