aboutsummaryrefslogtreecommitdiff
path: root/challenge-072/lance-wicks/elm/src/Main.elm
blob: ec034ad971121fb2b809bfc423ce653000f2f6fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module Main exposing (..)

import Array exposing (map)
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
import Regex
import String


main =
    Browser.sandbox { init = initModel, update = update, view = view }


type alias Model =
    { number : Int
    , factorialOfNumber : Int
    , trailingZeroes : Int
    }


initModel =
    { number = 10
    , factorialOfNumber = 0
    , trailingZeroes = 0
    }


type Msg
    = Increment
    | Decrement


update msg model =
    case msg of
        Increment ->
            { model | number = model.number + 1 }

        Decrement ->
            { model | number = model.number - 1 }


view model =
    div []
        [ button [ onClick Decrement ] [ text "-" ]
        , div [] [ text ("Number:" ++ String.fromInt model.number) ]
        , div [] [ text ("Trailing zeroes of factorial:" ++ String.fromInt (calcFactorial model.number)) ]
        , div [] [ text ("Factorial of number:" ++ String.fromInt (trailingZeroes (calcFactorial model.number))) ]
        , button [ onClick Increment ] [ text "+" ]
        ]


calcFactorial : Int -> Int
calcFactorial n =
    List.product (List.range 1 n)


trailingZeroes : Int -> Int
trailingZeroes x =
    let
        a =
            List.head <|
                Regex.find zeroes <|
                    String.fromInt x
    in
    case a of
        Just foo ->
            String.length foo.match

        Nothing ->
            0


zeroes =
    Maybe.withDefault Regex.never (Regex.fromString "0+$")