Since this post was originally written, the FizzBuzz github repository has received 23 additional contributions so the total number of languages supported is now 33. These additional languages are now included in the article below.
FizzBuzz, as you might have heard, is a simple programming challenge given as an interview question to see if someone can code or not.
Write a short program that prints each number
from 1 to 100 on a new line.
> For each multiple of 3, print "Fizz" instead of the number.
> For each multiple of 5, print "Buzz" instead of the number.
> For numbers which are multiples of both 3 and 5,
print "FizzBuzz" instead of the number.
Simple modulo operations to see if something is divisible by 3 and/or 5, a loop and printing to the console. It doesn't sound complicated. And it isn't, unless you are doing it in an obscure language you've never used before or in a programming paradigm which is less than common. So the thought occurred to me, FizzBuzz is actually perfect for learning the basics of a new language. It's simple enough, it doesn't leave much room for interpretation and we've all done it before in a language we are familiar with.
So I thought to myself, why not use some time this holiday to experiment with FizzBuzz? Oh boy did I experiment.
I started with JavaScript, more for calibration purposes if nothing else. To remind myself of the problem and do it in a language I am very familiar with. Nothing special, here it is.
for (let i = 1; i <= 100; ++i) {
const divBy3 = i % 3 === 0;
const divBy5 = i % 5 === 0;
if (divBy3 && divBy5) {
console.log(`FizzBuzz`);
} else if (divBy3) {
console.log(`Fizz`);
} else if (divBy5) {
console.log(`Buzz`);
} else {
console.log(i);
}
}
Then I thought, you know what I haven't used in a while? A functional language. I did a bit of Haskell in university so this wasn't soo unknown to me, but it wasn't super easy either. About 20 minutes. The hardest part was probably the fact that there is no loop, the loop is implicit. But this code is really elegant.
fb :: Integer -> String
fb n
| mod n 3 == 0 && mod n 5 == 0 = "FizzBuzz"
| mod n 3 == 0 = "Fizz"
| mod n 5 == 0 = "Buzz"
| otherwise = show n
main = do
putStrLn $ unlines (map fb [1..100])
Rust. I've been hearing about rust from a bunch of people, and now I finally got around to trying it. It was quite nice, I liked the pattern matching. I can't say I got into the more complex aspects of the language like the reference counting or friendly compiler messages, but I liked what I saw so far.
fn main() {
for x in 1..=100 {
match (x % 3, x % 5) {
(0, 0) => println!("FizzBuzz"),
(0, _) => println!("Fizz"),
(_, 0) => println!("Buzz"),
_ => println!("{}", x),
}
}
}
Not even worth discussing. Probably around a minute.
for i in range(1, 101):
divBy3 = i % 3 == 0
divBy5 = i % 5 == 0
if divBy3 and divBy5:
print("FizzBuzz")
elif divBy3:
print("Fizz")
elif divBy5:
print("Buzz")
else:
print(i)
Now, this one was quite interesting. My observations were:
:=
assignment syntax and begin
s and end
sfor I in Integer range 1 .. 100 loop
is quite considerably less elegant than for x in 1..101
)Overall it was an enjoyable experience though, I can't complain.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Hello is
Div_By_3 : Boolean := false;
Div_By_5 : Boolean := false;
begin
For_Loop :
for I in Integer range 1 .. 100 loop
Div_By_3 := I mod 3 = 0;
Div_By_5 := I mod 5 = 0;
if Div_By_3 and Div_By_5 then
Put_Line("FizzBuzz");
elsif Div_By_3 then
Put_Line("Fizz");
elsif Div_By_5 then
Put_Line("Buzz");
else
Put( I );
New_Line(1);
end if;
end loop For_Loop;
end Hello;
Probably around a minute (even though I don't know go). At this point I realized a few things:
package main
import (
"fmt"
)
func main() {
for i := 1; i <= 100; i++ {
divBy3 := i%3 == 0
divBy5 := i%5 == 0
if divBy3 && divBy5 {
fmt.Println("FizzBuzz")
} else if divBy3 {
fmt.Println("Fizz")
} else if divBy5 {
fmt.Println("Buzz")
} else {
fmt.Println(i)
}
}
}
So, remember how I was saying I was getting a little bored? Enter brainfuck. Let me start by saying this language really serves its name. I am going to assume you don't know anything about it (because let's be honest, it's not taught in CS101), so I will tell you it only supports these operations: <>+_.,[]
. You have a registry of numbers (255 I think) and you can do left right on the registry, increment and decrement, print something and loop while a registry's value is not zero. Yep. That's it. That is literally everything that comes as part of a language.
So I took a small intermission to the problem solving to read these guides:
And then I thought, ah, let me see how I can print a list of numbers. Well, here is a page telling you how to do: x = 0
, x = y
, x = x < y
... Umm, ok. This approach might not work.
Fast forward a couple of hours, I figure out how to print Fizz. Here it is.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++.>
I change my approach for this one to:
At the time of writing this, I got through everything apart from the loop. So the code below will print Fizz
, Buzz
, FizzBuzz
or the numbers correctly, given a hardcoded number.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++><<<<<<<<>>>>>>>>>+++++++++++++++>>+++<<[>+>->+<[>]>[<+>-
]<<[<]>-]>>[-]>>+<[>-<[-]]>[<<<<<<<<<<<<<.>.>.>.<<<>>>>>>>>>>>>>-]<+++
++<<[>+>->+<[>]>[<+>-]<<[<]>-]>>[-]>>+<[>-<[-]]>[<<<<<<<<<<.>.>.>.<<<>
>>>>>>>>>-]<+++<<[>+>->+<[>]>[<+>-]<<[<]>-]>>[-]>>+<[>-<[-]]>[>>>>>>+<
<<<<<-]<+++++<<[>+>->+<[>]>[<+>-]<<[<]>-]>>[-]>>+<[>-<[-]]>[>>>>>>+<<<
<<<-]>>>>>><[>>[-]+<<[-]]>[>[-]+<[-]]>[<<+>>-]<<>+<[>-<[-]]>[<<<<<<<<<
[>>+>+<<<-]>>>[<<<+>>>-]<<+>[<->[>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<
<<<<]>[-]++++++++[<++++++>-]>[<<+>>-]>[<<+>>-]<<]>]<[->>++++++++[<++++
++>-]]<[.[-]<]<>>>>>>>>>-]<<<<<<<<<
Wow. I spent a good afternoon on this and I solved like 80% of the problem. 1435 characters. But I will say this, getting anything to work in brainfuck was amazing. Even printing something so simple as Fizz is incredibly rewarding, cause you have to build those ASCII codes yourself.
The best part though, in a normal language when you are stuck on something, you can easily google around for an answer. You look at some snippet of code and you're like: ah, I get it now. I was so silly. Doing the same for brainfuck just doesn't work. You look at how to do a modulus, you see [>+>->+<[>]>[<+>-]<<[<]>-]
and you're like: well, I (still) don't get it.
But it was fun! It also piqued my interest into the esoteric languages. Shakespeare and Chef also sounded interesting but didn't get to those yet.
I needed a quick break to keep me going after the brainfuck adventure and bash did the trick. Ugly syntax if you ask me but overall nothing special or unexpected.
#!/bin/bash
# GNU bash, version 4.3.46
for i in {1..100}
do
if [ `expr $i % 3` == 0 ] && [ `expr $i % 5` == 0 ]; then
echo "FizzBuzz"
elif [ `expr $i % 3` == 0 ]; then
echo "Fizz"
elif [ `expr $i % 5` == 0 ]; then
echo "Buzz"
else
echo $i
fi
done
A contributor later updated it to fix some linting issues, here is the latest version.
#!/bin/bash
# GNU bash, version 4.3.46
for i in {1..100}; do
if [[ $((i % 3)) == 0 ]] && [[ $((i % 5)) == 0 ]]; then
echo "FizzBuzz"
elif [[ $((i % 3)) == 0 ]]; then
echo "Fizz"
elif [[ $((i % 5)) == 0 ]]; then
echo "Buzz"
else
echo $i
fi
done
You know you're off to a good start when you see you can't do a for loop. I thought this might be the second brainfuck but it ended up being quite easy and enjoyable actually.
divBy3(X) :- 0 is mod(X, 3).
divBy5(X) :- 0 is mod(X, 5).
print_fizz_buzz(X) :-
(divBy3(X),divBy5(X))
-> write('FizzBuzz')
; divBy3(X)
-> write('Fizz')
; divBy5(X)
-> write('Buzz')
; write(X).
print_numbers(100) :- print_fizz_buzz(100), !.
print_numbers(X) :- print_fizz_buzz(X), nl, Next is X + 1, print_numbers(Next).
Now, at last, another esoteric language, LOLCODE. It looks weird, it's weird to write, it's weird to read and just weird all around.
HAI 1.2
IM IN YR loop UPPIN YR var TIL BOTH SAEM var AN 101
DIFFRINT 0 AN var
O RLY?
YA RLY
I HAS A by3 ITZ BOTH SAEM 0 AN MOD OF var AN 3
I HAS A by5 ITZ BOTH SAEM 0 AN MOD OF var AN 5
BOTH OF by3 AN by5
O RLY?
YA RLY
VISIBLE "FizzBuzz"
NO WAI
by3
O RLY?
YA RLY
VISIBLE "Fizz"
NO WAI
by5
O RLY?
YA RLY
VISIBLE "Buzz"
NO WAI
VISIBLE var
OIC
OIC
OIC
OIC
IM OUTTA YR loop
KTHXBYE
Featuring scala's beautiful for comprehension syntax, this is the solution from our contributors.
object FizzBuzz {
def main(args: Array[String]) {
for( x <- 1 to 100) {
val divBy3 : Boolean = x % 3 == 0
val divBy5 : Boolean = x % 5 == 0
if (divBy3 && divBy5) {
println("FizzBuzz")
} else if (divBy3) {
println("Fizz")
} else if (divBy5) {
println("Buzz")
} else {
println(x)
}
}
}
}
Perhaps this implementation could be made more functional using a pure function that returns the string, and the printing could happen outside of the loop, something like this.
object FizzBuzz {
def fizzbuzz(x: Int): String = {
val divBy3 : Boolean = x % 3 == 0
val divBy5 : Boolean = x % 5 == 0
if (divBy3 && divBy5) {
"FizzBuzz"
} else if (divBy3) {
"Fizz"
} else if (divBy5) {
"Buzz"
} else {
x.toString()
}
}
def main(): Unit = {
for( x <- 1 to 100) {
val string = fizzbuzz(x)
println(string)
}
}
}
I don't have a lot of experience using Clojure so I really cannot comment much about this implementation, but the syntax seems a bit intimidating.
(map println
(map #(do
(def divByThree (zero? (mod % 3)))
(def divByFive (zero? (mod % 5)))
(cond
(and divByThree divByFive) "FizzBuzz"
divByThree "Fizz"
divByFive "Buzz"
:else %
)
) (range 1 101)))
This is the ruby implementation, seems pretty straightfoward. Interestingly enough the file extension is .arb
so today I learned that Ruby uses both .rb
and .arb
extensions.
def fizzBuzz()
for n in 1...100
if n % 3 == 0 && n % 5 == 0
pp "FizzBuzz"
elsif n % 3 == 0
pp "Fizz"
elsif n % 5 == 0
pp "Buzz"
else
pp n
end
end
end
fizzBuzz()
The logic in this implementation is slightly different because it only prints a new line at the end and doesn't explicitly print "FizzBuzz" when the number is both divisible by 3 and 5, but it does seem to work.
#include <stdio.h>
#include <stdbool.h>
int main()
{
for(int i = 1; i < 101; i++) {
bool divByThree = i % 3 == 0;
bool divByFive = i % 5 == 0;
if (!divByThree && !divByFive) {
printf("%d", i);
} else {
if (divByThree) {
printf("Fizz");
}
if (divByFive) {
printf("Buzz");
}
}
printf("\n");
}
return 0;
}
The crystal syntax is very similar to Ruby, which I guess makes sense since it was inspired by it when the language was created.
(1..100).each do |n|
fizz = n % 3 == 0
buzz = n % 5 == 0
if fizz && buzz
puts "FizzBuzz"
elsif fizz
puts "Fizz"
elsif buzz
puts "Buzz"
else
puts n
end
end
The CSharp implementation uses LINQ with elegant functional syntax and pure function with printing at the end.
using System;
using System.Linq;
namespace fizzbuzz
{
class Program
{
static void Main(string[] args)
{
Enumerable.Range(1, 100)
.Select(x => {
var divBy3 = x % 3 == 0;
var divBy5 = x % 5 == 0;
if (divBy3 && divBy5)
return "FizzBuzz";
else if (divBy3)
return "Fizz";
else if (divBy5)
return "Buzz";
return x.ToString();
})
.ToList()
.ForEach(Console.WriteLine);
}
}
}
This is the D implementation. The syntax is quite similar to C and C++, so it's easy to understand what's going on.
import std.stdio;
void main(string[] args)
{
for (int x = 1; x <= 100; x++) {
bool divBy3 = x % 3 == 0;
bool divBy5 = x % 5 == 0;
if (divBy3 && divBy5) {
writeln("FizzBuzz");
} else if (divBy3) {
writeln("Fizz");
} else if (divBy5) {
writeln("Buzz");
} else {
writeln(x);
}
}
}
Here is the Dart implementation. Dart is often used for building apps, and the syntax is supposed to be inspired by C, which makes sense looking at the code.
void main() {
for(int i = 1; i < 101; i++) {
bool divByThree = i % 3 == 0;
bool divByFive = i % 5 == 0;
if (divByThree && divByFive) {
print("FizzBuzz");
} else if (divByThree) {
print("Fizz");
} else if (divByFive) {
print("Buzz");
} else {
print(i);
}
}
}
The Erlang implementation is very reminiscent of the Haskell one.
-module(fizzbuzz).
-export([fizzbuzz/0]).
get_fizzbuzz_str(N) when N rem 15 == 0 -> "FizzBuzz";
get_fizzbuzz_str(N) when N rem 3 == 0 -> "Fizz";
get_fizzbuzz_str(N) when N rem 5 == 0 -> "Buzz";
get_fizzbuzz_str(N) -> integer_to_list(N).
fizzbuzz() ->
[io:format("~s~n", [get_fizzbuzz_str(N)]) || N <- lists:seq(1,100)].
Here is the FizzBuzz implementation in Elixir.
defmodule FizzBuzz do
for x <- 1..100 do
case [rem(x, 3), rem(x, 5)] do
[0, 0] -> IO.puts("FizzBuzz")
[0, _] -> IO.puts("Fizz")
[_, 0] -> IO.puts("Buzz")
_ -> IO.puts(x)
end
end
end
Here is the FizzBuzz implementation in F#.
open System
[<EntryPoint>]
let main argv =
[1..100]
|> Seq.map (fun x ->
match x with
| x when x%5=0 && x%3=0 -> "FizzBuzz"
| x when x%3=0 -> "Fizz"
| x when x%5=0 -> "Buzz"
| x -> string x)
|> Seq.iter (printfn "%s")
0
Here is the HMTL implementation, I've omitted the last 95 <li>
tags because I feel like you sort of get the idea. I would argue this is more a CSS implementation but it's pretty cool nonetheless.
<style>
li:nth-child(3n),
li:nth-child(5n) {
list-style: none;
}
li:nth-child(3n):before {
content: 'Fizz';
}
li:nth-child(5n):after {
content: 'Buzz';
}
</style>
<ol>
<li></li>
<li></li>
<li></li>
<li></li>
....
</ol>
Here is the FizzBuzz implementation in XML, very similar to HTML.
<<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="fizzbuzz.xsl"?>
<!--
To "run" fizzbuzz.xsl, open this file via Firefox Browser directly. (does not work with Chrome unless the xml/xsl files are hosted on a webserver)
-->
<range>
<int>1</int>
<int>2</int>
<int>3</int>
<int>4</int>
<int>5</int>
...
</range>
Here is the FizzBuzz implementation in Java.
public class FizzBuzz{
public static void main(String []args){
for (int x = 1; x <= 100; x++)
{
boolean divBy3 = x % 3 == 0;
boolean divBy5 = x % 5 == 0;
if (divBy3 && divBy5)
{
System.out.println("FizzBuzz");
}
else if (divBy3)
{
System.out.println("Fizz");
}
else if (divBy5)
{
System.out.println("Buzz");
}
else
{
System.out.println(x);
}
}
}
}
Here is the FizzBuzz implementation in Julia.
for i = 1:100
divBy3 = i % 3 == 0
divBy5 = i % 5 == 0
if divBy3 && divBy5
println("FizzBuzz")
elseif divBy3
println("Fizz")
elseif divBy5
println("Buzz")
else
println(i)
end
end
Here is the FizzBuzz implementation in Kotlin.
// Run Kotlin script: kotlinc -script fizzbuzz.kts
for (i in 1..100) {
val divisibleBy3 = i % 3 == 0
val divisibleBy5 = i % 5 == 0
when {
divisibleBy3 && divisibleBy5 -> println("FizzBuzz")
divisibleBy3 -> println("Fizz")
divisibleBy5 -> println("Buzz")
else -> println(i)
}
}
Here is the FizzBuzz implementation in Lua.
for x=1,100 do
local divBy3, divBy5 = x % 3 == 0, x % 5 == 0
if divBy3 and divBy5 then
print("FizzBuzz")
elseif divBy3 then
print("Fizz")
elseif divBy5 then
print("Buzz")
else
print(x)
end
end
Here is the FizzBuzz implementation in ML.
let fizzbuzz x =
let div_by_3 x = x mod 3 = 0 in
let div_by_5 x = x mod 5 = 0 in
let div_by_both x = div_by_3 x && div_by_5 x in
if div_by_both x then "FizzBuzz"
else if div_by_3 x then "Fizz"
else if div_by_5 x then "Buzz"
else string_of_int x
let main =
let to_100 = List.init 100 ((+) 1) in
List.iter (fun x -> print_endline (fizzbuzz x)) to_100
Here is the FizzBuzz implementation in Perl.
for (1..100) {
my $divBy3 = $_ % 3 == 0;
my $divBy5 = $_ % 5 == 0;
if ($divBy3 && $divBy5) {
print "FizzBuzz\n";
}
elsif ($divBy3) {
print "Fizz\n";
}
elsif ($divBy5) {
print "Buzz\n";
}
else {
print "$_\n";
}
}
Here is the FizzBuzz implementation in Pug.
ul
- i = 0
while ++i <= 100
li
if i % 3 && i % 5
= i
if i % 3 == 0
| Fizz
if i % 5 == 0
| Buzz
Here is the FizzBuzz implementation in R.
fizzbuzz_nums = 1:100
output <- vector()
for (i in fizzbuzz_nums) {
if (i %% 3 == 0 && i %% 5 == 0) {
output[i] <- "FizzBuzz"
} else if (i %% 3 == 0) {
output[i] <- "Fizz"
} else if (i %% 5 == 0) {
output[i] <- "Buzz"
} else {
output[i] <- i
}
}
print(output)
Here is the FizzBuzz implementation in Scheme.
(define (fizzbuzz x)
(cond
[(and (= (mod x 3) 0) (= (mod x 5) 0)) 'FizzBuzz]
[(= (mod x 3) 0) 'Fizz]
[(= (mod x 5) 0) 'Buzz]
[else (number->string x)])
)
(map (lambda (x) (fizzbuzz x)) (iota 100 1))
Here is the FizzBuzz implementation in VBScript.
Option Explicit
Dim i
For i = 1 To 100
Dim divBy3: divBy3 = i mod 3 = 0
Dim divBy5: divBy5 = i mod 5 = 0
If divBy3 And divBy5 Then
WScript.Echo "FizzBuzz"
ElseIf divBy3 Then
WScript.Echo "Fizz"
ElseIf divBy5 Then
WScript.Echo "Buzz"
Else
WScript.Echo i
End If
Next
Here is the FizzBuzz implementation in XSL.
<?xml version="1.0" encoding="UTF-8"?>
<!--
This XSL file can not be run directly. To "run" this file, open fizzbuzz.xml file via Firefox Browser. (does not work with Chrome unless the xml/xsl files are hosted on a webserver)
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/range">
<html>
<body>
<h2>FizzBuzz Output</h2>
<xsl:for-each select="int">
<xsl:variable name="divBy3" select=". mod 3 = 0" />
<xsl:variable name="divBy5" select=". mod 5 = 0" />
<xsl:choose>
<xsl:when test="$divBy3 and $divBy5">
FizzBuzz
</xsl:when>
<xsl:when test="$divBy3">
Fizz
</xsl:when>
<xsl:when test="$divBy5">
Buzz
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
<br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
So, after implementing the first 10 languages myself and reviewing numerous contributions to the repo over the last few years, what have I learned?
First of all, most languages that we use every day really aren't that different from each other on a fundamental syntax level, they all have for loops and ifs and variables, etc.
Functional, declarative and low-level languages are fundamentally different from the rest and programming in them requires a complete mind shift (like operating on a list in haskell using recursion instead of a loop, or moving the register pointer around in brainfuck)
Language syntax has evolved somewhat from verbose to implicit and elegant (not sure if that is a good or bad thing, probably neither).
As a final thought, if you see a language not supported, feel free to submit a pull request to the repository on github.
Share something about this post.