FizzBuzz

A collection of ways to solve the FizzBuzz problem

Simple Rules

To replace multiples of 3 with Fizz, multiples of 5 with Buzz, and multiples of both 3 and 5 with FizzBuzz...

awk

seq 1 100 | awk '$0=NR%15?NR%5?NR%3?$0:"Fizz":"Buzz":"FizzBuzz"'

seq 1 100 | awk '($0%3 > 0) && ($0%5 > 0) {print; next} $0%3 == 0 {printf "fizz"} $0%5 == 0 {printf "buzz"} {printf "\n"}'  

sed

seq 1 100 | sed '0~3s/.*/Fizz/;0~5s/[0-9]*$/Buzz/'

This is actually a bit of a cheat as it just prints Fizz or Buzz based on position not actual number. I.e. if you start the sequence from any number other than 1, it doesn't work.

seq 1 100 | ./fizzbuzz.sed

bash

seq 1 100 | while read -r i;do((i%3))&&x=||x=Fizz;((i%5))||x+=Buzz;echo ${x:-$i};done

seq 1 100 | ./fizzbuzz.sh

seq 1 100 | ./fizzbuzz.py

seq 1 100 | ./fizzbuzz1.py

PL/SQL

SQL

Alternate Rules

When we played FizzBuzz, there was an additional rule... not just multiples of 3 and 5, but also any numbers containing 3 or 5...

bash

seq 1 100 | ./fizzbuzz2.sh

seq 1 100 | ./fizzbuzz2.py

Bibliography