🇺🇸 Fizz Buzz

There is a rather simple problem that sometimes is asked in job interviews:

Print out the numbers from 1 to 100. If the number if divisible by 3, print “Fizz” instead of the numbers. If the number is divisible by 5, print “Buzz”. And if it is divisible by both, print “FizzBuzz”.

This is not a particularly hard problem, but it shows control structures like looks and branches. I have implemented it in various programming languages to see how different they are.

Bash

Integer arithmetics is built-in to Bash, though it is not exactly pretty.

set -e
set -u

for (( i = 1; i <= 100; ++i )); do
    local printed=false

    if (( i % 3 == 0 )); then
        echo -n 'Fizz'
        printed=true
    fi
    if (( i % 5 == 0 )); then
        echo -n 'Buzz'
        printed=true
    fi

    if [[ "$printed" == false ]]; then
        echo -n $i
    fi

    echo
done

C++

#include <iostream>

int main() {
    for (int i = 1; i <= 100; ++i) {
        bool printed = false;

        if (i % 3 == 0) {
            std::cout << "Fizz";
            printed = true;
        }
        if (i % 5 == 0) {
            std::cout << "Buzz";
            printed = true;
        }

        if (!printed) {
            std::cout << i;
        }

        std::cout << "\n";
    }
}

C

#include <stdio.h>

int main() {
    for (int i = 1; i <= 100; ++i) {
        int printed = 0;

        if (i % 3 == 0) {
            printf("Fizz");
            printed = 1;
        }
        if (i % 5 == 0) {
            printf("Buzz");
            printed = 1;
        }

        if (!printed) {
            printf("%d", i);
        }

        printf("\n");
    }
}

Fish

The syntax of Fish is a tad easier than the one of Bash, but it makes the program more complicated. Especially all those calls to bc make it hard to read.

for i in (seq 1 100)
    set printed false

    if test (echo "$i % 3" | bc) -eq 0
        echo -n 'Fizz'
        set printed true
    end
    if test (echo "$i % 5" | bc) -eq 0
        echo -n 'Buzz'
        set printed true
    end

    if test "$printed" != true
        echo -n "$i"
    end

    echo
end

Haskell

Haskell has neither variables or loops. However, if you can read some of the syntax, it actually is the prettiest implementation.

fizzbuzz :: Int -> String
fizzbuzz i
    | i `mod` 3 == 0 && i `mod` 5 == 0 = "FizzBuzz"
    | i `mod` 3 == 0 = "Fizz"
    | i `mod` 5 == 0 = "Buzz"
    | otherwise = show i

join :: [[Char]] -> [Char]
join [x] = x
join (x:xs) = x ++ "\n" ++ join xs

main = putStrLn $ join $ map fizzbuzz [1..100]

Java

For some reasons everything has to be a class in Java.

public class FizzBuzz {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; ++i) {
            boolean printed = false;

            if (i % 3 == 0) {
                System.out.print("Fizz");
                printed = true;
            }
            if (i % 5 == 0) {
                System.out.print("Buzz");
                printed = true;
            }

            if (!printed) {
                System.out.print(i);
            }

            System.out.println();
        }
    }
}

JavaScript

In JavaScript, one has to prefix variables with var such that they do not become global. Whatever …

for (var i = 1; i <= 100; ++i) {
    var printed = false;

    if (i % 3 == 0) {
        print("Fizz");
        printed = true;
    }
    if (i % 5 == 0) {
        print("Buzz");
        printed = true;
    }

    if (!printed) {
        print(i);
    }

    print("\n");
}

PHP

<?php

for ($i = 1; $i <= 100; ++$i) {
    $printed = false;

    if ($i % 3 == 0) {
        echo 'Fizz';
        $printed = true;
    }
    if ($i % 5 == 0) {
        echo 'Buzz';
        $printed = true;
    }

    if (!$printed) {
        echo "$i";
    }

    echo "\n";
}

Python

This is rather nice procedural code:

for i in range(1, 101):
    printed = False

    if i % 3 == 0:
        print('Fizz', end='')
        printed = True
    if i % 5 == 0:
        print('Buzz', end='')
        printed = True

    if not printed:
        print(i, end='')

    print()

R

R is not exactly made for this, but it can also do it.

for (i in 1:100) {
    printed <- FALSE

    if (i %% 3 == 0) {
        cat("Fizz")
        printed = TRUE
    }
    if (i %% 5 == 0) {
        cat("Buzz")
        printed = TRUE
    }

    if (!printed) {
        cat(i)
    }

    cat('\n')
}