Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let fb nn =
- // + and - operator are defined in Set.
- // operator for intersection of sets.
- let inline ( |^| ) a b = Set.intersect a b
- // an ideal of Z ring (Z = field of integers)
- // http://en.wikipedia.org/wiki/Ideal_(ring_theory)
- let nZ n =
- seq { for i in 1 .. nn/n do yield i * n }
- |> Set.ofSeq
- // sets from ideals
- let all = nZ 1
- let z3 = nZ 3
- let z5 = nZ 5
- let z15 = z3 |^| z5 // (15) is m.c.m(3,5)
- // no buzz, no fizz...
- let onlynormal =
- all - (z3 + z5)
- |> Set.map (fun x -> (x, x.ToString()))
- // only "fizzes"
- let fizzes =
- z3 - z15 |> Set.map (fun x -> (x, "Fizz"))
- // only "buzzes"
- let buzzes =
- z5 - z15 |> Set.map (fun x -> (x, "Buzz"))
- // only "fizzbuzz"
- let buzzfizzes =
- Set.map ( fun x -> (x, "FizzBuzz")) z15
- // result
- // Note: apparently automatically sorts when convert the set to a list
- onlynormal + fizzes + buzzes + buzzfizzes
- |> Set.toList
- |> List.map (fun x -> snd x)
- // -----------------------------------------------------------------------
- // -- check ---
- // first 102
- printfn "1 - 102: %A" (fb 102)
- // 597 position
- printfn "597: %s" (fb 600).[597-1]
- // 1200 position
- printfn "1200: %s" (fb 1200).[1200-1]
- // range
- ((fb 10000) |> List.toArray).[9900..9999] |> printfn "From 9901 to 10000: %A"
- // WARNING: !!!!
- // 49500 (over 1.000.000 elements!!!!!)
- printfn "49500: %s" (fb 1000000).[49500-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement