Advertisement
Guest User

The Heavy Way to FizzBuzz solution

a guest
Jul 13th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.18 KB | None | 0 0
  1. let fb nn =
  2.    
  3.     // set of all numbers
  4.     let all =
  5.         seq { for i in 1 .. nn do yield i }
  6.         |> Set.ofSeq
  7.  
  8.     // an ideal of Z ring  (Z = field of integers)
  9.     // http://en.wikipedia.org/wiki/Ideal_(ring_theory)
  10.     let nZ n =
  11.         seq { for i in 1 .. nn/n do yield i * n }
  12.         |> Set.ofSeq
  13.  
  14.     // no buzz, no fizz...
  15.     let onlynormal =
  16.         nZ 3 |> Set.union (nZ 5)
  17.         |> Set.difference all
  18.         |> Set.map (fun x -> (x, x.ToString()))
  19.        
  20.     // only "fizzes"
  21.     let buzzes =
  22.         nZ 15 |> Set.difference (nZ 3) |> Set.map (fun x -> (x, "Fizz"))
  23.  
  24.     // only "buzzes"
  25.     let fizzes =
  26.         nZ 15 |> Set.difference (nZ 5) |> Set.map (fun x -> (x, "Buzz"))
  27.  
  28.     // only "fizzbuzz"
  29.     let buzzfizzes =
  30.         nZ 15 |> Set.map ( fun x -> (x, "FizzBuzz"))
  31.  
  32.     // result
  33.     onlynormal
  34.     |> Set.union buzzes
  35.     |> Set.union fizzes
  36.     |> Set.union buzzfizzes
  37.     |> Set.toList
  38.     |> List.map (fun x -> snd x)
  39.  
  40.  
  41. // first 99
  42. printfn "%A" (fb 99)
  43.  
  44. // 597 position
  45. printfn "%s" (fb 600).[597-1]
  46.  
  47. // 1200 position
  48. printfn "%s" (fb 1200).[1200-1]
  49.  
  50. // 9995 position
  51. printfn "%s" (fb 10000).[9995-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement