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.)

Latest comments (20)

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: Haskell
Code:

fizzBuzzer :: Int -> String
fizzBuzzer i | (i `mod` 15) == 0 = "FizzBuzz"
       | (i `mod` 5) == 0 = "Buzz"
       | (i `mod` 3) == 0 = "Fizz"
       | otherwise = show i

fizzBuzz :: IO()
fizzBuzz = mapM_ print [ fizzBuzzer i | i <- [1..100]]
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)
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
 
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
 
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 • 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: 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
 
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
 
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)));
};