DEV Community

Theo
Theo

Posted on β€’ Edited on

FizzBuzz challenge in as many languages as possible

I was recently reading this post inwhich the amazing Ady Ngom was disscussing different ways to solve the common FizzBizz challenge given in interviews and how to refactor and streamline your code.

This gave me an idea.

What if we ask the community here to write their own solution to the FizzBuzz challenge in their given language.

What to do:

--[[
Write a program that prints the numbers from 1 to 100.
But for multiples of three print Fizz instead of the number, 
For the multiples of five print Buzz and for numbers 
which are multiples of both three and five print FizzBuzz
--]]
Enter fullscreen mode Exit fullscreen mode

Rules:

  1. Have fun!
  2. Don't copy others code, keep it unique πŸ‘.
  3. Use this format:

Language: e.g Python
Code:
Other/info: e.g Hia folks, I like cheese.

Lets see how many different languages we can get here!
(You can make it as simple or as complex as you like.)

Top comments (20)

Collapse
Β 
believer profile image
Rickard Natt och Dag β€’

Language: ReasonML
Sketch: sketch.sh/s/XABe2ghxBqncDWTTKpNK8n/

module FizzBuzz = {
  let get =
    fun
    | (0, 0, _) => "FizzBuzz"
    | (0, _, _) => "Fizz"
    | (_, 0, _) => "Buzz"
    | (_, _, value) => string_of_int(value);
};

for (index in 1 to 100) {
  print_endline(FizzBuzz.get((index mod 3, index mod 5, index)));
};
Collapse
Β 
thomasthespacefox profile image
Thomas Leathers β€’

Language: SSTNPL
Code:

var remain3=+
var remain5=+

uiter fbnum,fbloop,@1,@100
stop

label fbloop
    divmod fbnum,@3
    set remain3
    divmod fbnum,@5
    set remain5

    if remain3,@0 gsub fizz
    if remain3,@0 return
    if remain5,@0 gsub buzz
    if remain5,@0 return
    dumpd fbnum
    newline
return

label fizz
    if remain5,@0 goto fizzbuzz
    prline Fizz
    return
    label fizzbuzz
        prline FizzBuzz
        return

label buzz
prline Buzz
return

Other/Info: Figured I'd contribute something a bit more obscure. :) SSTNPL is an architecture-specialized programming language thats used with my SBTCVM ternary computer simulator. Yes, its a bit clunkier-looking than most of the examples here, but mainly down to it using labeled GOTOs...

Collapse
Β 
jeddevs profile image
Theo β€’

Very interesing, thank you for sharing. Never heard of SSTNPL and even a quick google search doesn't render many results. πŸ‘ Thanks!

Collapse
Β 
thomasthespacefox profile image
Thomas Leathers β€’

SSTNPL is somthing i put together for SBTCVM specifically. its kinda somehwhere between an assembler (as it compiles to SBTCVM's assembly language), and C, with a bit of a primitive syntax, and a fairly static structure. Its simple, but it does have a few neat features like 2-axis array-like tables, and a module system.

as far as SBTCVM itself, the blog's about page has a good overview of it:
sbtcvm.blogspot.com/p/about.html

Collapse
Β 
mattonem profile image
maxmattone β€’

Language: Smalltalk
Code:

1 to: 100 do: [ :i | 
    Transcript
        show:
            ((i isDivisibleBy: 15)
                ifTrue: [ 'FizzBuzz' ]
                ifFalse: [ (i isDivisibleBy: 3)
                        ifTrue: [ 'Fizz' ]
                        ifFalse: [ (i isDivisibleBy: 5)
                                ifTrue: [ 'Buzz' ]
                                ifFalse: [ i ] ] ]);
        cr ]
Collapse
Β 
jeddevs profile image
Theo β€’

πŸ‘πŸ‘πŸ‘, never used that language.

Collapse
Β 
mattonem profile image
maxmattone β€’ β€’ Edited

Language: Emacs Lisp
Code:

(cl-defun fizzbuzz(&optional (n 100))
  (cl-loop for i from 1 to n do
       (print
        (cond ((= (mod i 15) 0) "FizzBuzz")
          ((= (mod i 3) 0) "Fizz")
          ((= (mod i 5) 0) "Buzz")
          (t i)
         ))
       ))
(fizzbuzz)

Collapse
Β 
jeddevs profile image
Theo β€’

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
Β 
jeddevs profile image
Theo β€’

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
Β 
jeddevs profile image
Theo β€’

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

Collapse
Β 
jeddevs profile image
Theo β€’

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
Collapse
Β 
oscherler profile image
Olivier β€œΓ–lbaum” Scherler β€’

Language: Erlang
Code:

Short version, generate a list (of strings and integers), don’t bother printing:

lists:map(
    fun( X ) ->
        case { X rem 3, X rem 5 } of
            { 0, 0 } -> "FizzBuzz";
            { 0, _ } -> "Fizz";
            { _, 0 } -> "Buzz";
            _ -> X
        end
    end,
    lists:seq( 1, 100 )
).

Full version, print one term per line. Here we have to convert integers to strings:

lists:foreach(
    fun( X ) ->
        io:format(
            "~s~n",
            [
                case { X rem 3, X rem 5 } of
                    { 0, 0 } -> "FizzBuzz";
                    { 0, _ } -> "Fizz";
                    { _, 0 } -> "Buzz";
                    _ -> integer_to_list(X)
                end
            ]
        )
    end,
    lists:seq( 1, 100 )
).
Collapse
Β 
jeddevs profile image
Theo β€’

Language: Python
Code:

def FizzBuzz():
  for i in range (1,101):
    #print(i/3)
    if i%3 == 0 and i%5 == 0:
      print("FizzBuzz")
    elif i%3 == 0:
      print("Fizz")
    elif i%5 == 0:
      print("Buzz")
    else: 
      print(i)
FizzBuzz()
Collapse
Β 
reinhart1010 profile image
Shift / Reinhart Previano K. β€’

Language: JavaScript
Code: gist.github.com/reinhart1010/d2b81...

Other/Info: The above code execute console.log command for each number. And yes, this is compiled on JSFuck based on the original code:

var i;
for (i = 1; i <= 100; i++){
    var fb = 0;
    if (i % 3 == 0){
        console.log("Fizz");
        fb = 1;
    }
    if (i % 5 == 0){
        console.log("Buzz");
        fb = 1;
    }
    if (fb == 0) console.log(i);
}

Trying to decode the first snippet on JSUnFuck will likely to cause browsers to hang.

Interestingly, this one can be exported to C with minimal modifications:

#include <stdio.h>

int main(){
    int i;
    for (i = 1; i <= 100; i++){
        int fb = 0;
        if (i % 3 == 0){
            printf("Fizz");
            fb = 1;
        }
        if (i % 5 == 0){
            printf("Buzz");
            fb = 1;
        }
        if (fb == 0) printf("%d", i);
        printf("\n");
    }
}

JavaScript should be welcoming to my folks who are currently learning C thanks to similarity in syntaxes. (If you're reading this, good luck in facing your mid-term exams next week!)

Collapse
Β 
brianverm profile image
Brian Vermeer πŸ§‘πŸΌβ€πŸŽ“πŸ§‘πŸΌβ€πŸ’» β€’

Language: Java (8 and above)
Code:

    public void fizzBuzz() {
        IntStream.rangeClosed(1,100)
                .mapToObj(this::fizzBuzzer)
                .forEach(System.out::println);
    }

    private String fizzBuzzer(int i){
        if (i % 15 == 0) return "FizzBuzz";
        if (i % 3 == 0) return "Fizz";
        if (i % 5 == 0) return "Buzz";
        return String.valueOf(i);
    }
Collapse
Β 
ap13p profile image
Afief S β€’

Language: Javascript
Code:

const array = Array.from(Array(100).keys()).map(it => it + 1)
  .map(it => it % 3 === 0 && it % 5 === 0 ? 'FizzBuzz' : it)
  .map(it => typeof it === 'number' && it % 3 === 0 ? 'Fizz' : it)
  .map(it => typeof it === 'number' && it % 5 === 0 ? 'Buzz' : it)

console.log(array)