Browse Source

simple collatz paths to colours

Brandon Wong 7 years ago
commit
8daf9da72f
5 changed files with 145 additions and 0 deletions
  1. 4 0
      .gitignore
  2. 100 0
      Main.elm
  3. 16 0
      elm-package.json
  4. 12 0
      index.html
  5. 13 0
      style.css

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+.idea
+*.swp
+elm-stuff
+client.js

+ 100 - 0
Main.elm

@@ -0,0 +1,100 @@
+
+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
+        }
+
+

+ 16 - 0
elm-package.json

@@ -0,0 +1,16 @@
+{
+    "version": "1.0.0",
+    "summary": "helpful summary of your project, less than 80 characters",
+    "repository": "https://github.com/user/project.git",
+    "license": "BSD3",
+    "source-directories": [
+        "."
+    ],
+    "exposed-modules": [],
+    "dependencies": {
+        "elm-lang/core": "5.1.1 <= v < 6.0.0",
+        "elm-lang/html": "2.0.0 <= v < 3.0.0",
+        "rtfeldman/hex": "1.0.0 <= v < 2.0.0"
+    },
+    "elm-version": "0.18.0 <= v < 0.19.0"
+}

+ 12 - 0
index.html

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Collatz Conjecture</title>
+    <link rel="stylesheet" href="style.css">
+</head>
+<body>
+    <script src="client.js"></script>
+    <script>Elm.Collatz.fullscreen();</script>
+</body>
+</html>

+ 13 - 0
style.css

@@ -0,0 +1,13 @@
+
+body {
+    font-family:sans-serif;
+}
+
+.box {
+    text-align:center;
+    padding:10px 0px;
+    width:50px;
+    float:left;
+}
+
+