DEV Community

loading...

Discussion on: FizzBuzz challenge in as many languages as possible

Collapse
teotcd profile image
JedDevs Author

Language: Lua
Code:

function FizzBuzz()
  for i = 1,100 do
    --print(i)
    if i%3 == 0 and i%5 == 0 then
      print("FizzBuzz")
    elseif i%3 == 0 then
      print("Fizz")
    elseif i%5 == 0 then
      print("Buzz")
    else
      print(i)
    end
  end
end
FizzBuzz()
Enter fullscreen mode Exit fullscreen mode

Other/info: I guess I'll start the challenge off.
This is a pretty simple function in lua of the FizzBuzz challenge.

Collapse
vrotaru profile image
Vasile Rotaru

Language: OCaml
Code:

let fizzbuzz n =
  let rec loop i max div3 div5 =
    if i < max then
      match i = div3, i = div5 with
      | true, true   -> print_endline "FizzBuzz"; loop (i + 1) max (div3 + 3) (div5 + 5)
      | true, false  -> print_endline "Fizz"; loop (i + 1) max (div3 + 3) div5
      | false, true  -> print_endline "Buzz"; loop (i + 1) max div3 (div5 + 5)
      | false, false -> print_endline (string_of_int i); loop (i + 1) max div3 div5
  in
  loop 1 n 3 5

let _ = fizzbuzz 100
Collapse
teotcd profile image
JedDevs Author

Interesting! What kind of work is OCaml used for? 😊

Thread Thread
vrotaru profile image
Vasile Rotaru

Compilers, Proof Assistants, Static Verification Tools. Facebook uses it a lot under the hood.

Thread Thread
teotcd profile image
JedDevs Author

Interesting.
Welcome to dev.to btw (saw you joined today).

Collapse
teotcd profile image
JedDevs Author

I've learnt a lot since this old thread, here's how i'd attempt this question in an interview today:


-- FizzBuzz

local function isDivisible(num: number, by: integer)
    return num % by == 0
end

local function result(number)
    local str = ""

    if isDivisible(number, 3) then
        str = str.."Fizz"
    end

    if isDivisible(number, 5) then
        str = str.."Buzz"
    end

    return (str ~= "" and str) or number
end


for i = 1, 100 do
    print(result(i))
end
Enter fullscreen mode Exit fullscreen mode