Skip to content

Instantly share code, notes, and snippets.

@flq
Last active June 18, 2020 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save flq/5669209 to your computer and use it in GitHub Desktop.
Save flq/5669209 to your computer and use it in GitHub Desktop.
Fizzbuzzes in many colours.

A collection of FizzBuzz implementations over many languages. The specification goes something like this...

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

The expected outcome is something like:

1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,
11,Fizz,13,14,FizzBuzz,16,17,Fizz,19,Buzz,
Fizz,22,23,Fizz,Buzz,26,Fizz,28,29,FizzBuzz,
31,32,Fizz,34,Buzz,Fizz,37,38,Fizz,Buzz,
41,Fizz,43,44,FizzBuzz,46,47,Fizz,49,Buzz,
Fizz,52,53,Fizz,Buzz,56,Fizz,58,59,FizzBuzz,
61,62,Fizz,64,Buzz,Fizz,67,68,Fizz,Buzz,
71,Fizz,73,74,FizzBuzz,76,77,Fizz,79,Buzz,
Fizz,82,83,Fizz,Buzz,86,Fizz,88,89,FizzBuzz,
91,92,Fizz,94,Buzz,Fizz,97,98,Fizz,Buzz

Honorable mentions:

REM courtesy of https://github.com/nixha
@echo off
for /L %%I IN (1, 1, 100) DO (
call :OUTPUT %%I
)
pause
exit
:OUTPUT
set /a A=%1 %% 3
set /a B=%1 %% 5
if "%A%" == "0" (
if "%B%" == "0" (
echo Fizzbuzz
) else (
echo Fizz
)
) else (
if "%B%" == "0" (
echo Buzz
) else (
echo %1
)
)
goto :eof
;http://alan.dipert.org/post/172774481/fizzbuzz-in-scala-and-clojure
(use '[match.core :only (match)])
(doseq [n (range 1 101)]
(println (match [(mod n 3) (mod n 5)]
[0 0] "FizzBuzz"
[0 _] "Fizz"
[_ 0] "Buzz"
:else n)))
// Courtesy of Paul G.
for (int i = 1; i <= 100; i++)
Console.Write(i % 15 == 0 ? "FizzBuzz" : i % 3 == 0 ? "Fizz" : i % 5 == 0 ? "Buzz" : i.ToString());
-module(fizzbuzz).
-export([main/0]).
main()
-> [do(Value) || Value <- lists:seq(1,100)].
do(FizzBuzz) when FizzBuzz rem 15 == 0
-> io:format("FizzBuzz~n");
do(Fizz) when Fizz rem 3 == 0
-> io:format("Fizz~n");
do(Buzz) when Buzz rem 5 == 0
-> io:format("Buzz~n");
do(Number)
-> io:format("~p~n", [ Number ]).
%% Another implementation at http://en.literateprograms.org/FizzBuzz_(Erlang)
let (wr : string -> unit) = System.Console.WriteLine
let (|Fizz|Buzz|FizzBuzz|Other|) input =
if input % 15 = 0 then FizzBuzz
elif input % 3 = 0 then Fizz
elif input % 5 = 0 then Buzz
else Other
[1..100] |> Seq.iter (fun n ->
match n with
| Fizz -> wr "Fizz"
| Buzz -> wr "Buzz"
| FizzBuzz -> wr "FizzBuzz"
| Other -> wr(n.ToString())
)
// Taken and then indented from http://play.golang.org/p/0fG1oKhdB9
package main
func main(){
for i:=1;i<101;
i++{n,m:=i%3,i%5
if n*m>0 {
print(i)
} else {
if n==0 {print("Fizz")}
if m==0 {print("Buzz")}
}
println()}
}
100,{)6,{.(&},{1$1$%{;}{4*35+6875*25base{90\-}%}if}%\or}%n*
# Test it at https://golfscript.apphb.com/
-- taken from http://freshbrewedcode.com/calvinbottoms/2012/02/25/fizzbuzz-in-haskell/
[max(show x)(concat[n|(f,n)<-[(3,"Fizz"),(5,"Buzz")],mod x f==0])|x<-[1..100]]
-- a more readable solution, taken from https://gist.github.com/NeilRobbins/5190365
divides d n = rem n d == 0
fizzbuzz n | divides 15 n = "fizzbuzz"
| divides 3 n = "fizz"
| divides 5 n = "buzz"
| otherwise = show n
map fizzbuzz [0..]
// courtesy of https://github.com/nixha
public static void main(String[] args) {
for (int i = 0; i < 100; System.out.println(++i % 3 == 0 ? i % 5 == 0 ? "Fizzbuzz" : "Fizz" : i % 5 == 0 ? "Buzz" : i));
}
// Inspired by http://www.globalnerdy.com/2020/06/18/programmer-interview-challenge-2-part-4-using-watchers-to-play-fizzbuzz-properly/
const wordWatcher = (interval, word) => {
let count = 0;
return () => {
count++;
if (count === interval) {
count = 0;
return word;
}
return "";
}
}
const fizzWatcher = wordWatcher(3, "Fizz");
const buzzWatcher = wordWatcher(5, "Buzz");
for (number of Array(100).keys()) {
const potentialFizzBuzz = `${fizzWatcher()}${buzzWatcher()}`;
console.log(potentialFizzBuzz ? potentialFizzBuzz : number + 1);
};
// taken from https://gist.github.com/abreslav/5203175
fun Int.divides(d: Int) = this % d == 0
fun fizzbuzz(n: Int) = when {
n divides 15 -> "fizzbuzz"
n divides 3 -> "fizz"
n divides 5 -> "buzz"
else -> "$n"
}
fun result(max: Int) = (0..max) map { n -> fizzbuzz(n) }
// Optionally, to print it out
fun main(args: Array<String>) {
println(result(100))
}
@(1..100) | % {
if ($_ % 15 -eq 0) {"FizzBuzz"}
elseif ($_ % 5 -eq 0) {"Buzz"}
elseif ($_ % 3 -eq 0) {"Fizz"}
else { $_ }
}
(1..100).each do |n|
mod3, mod5 = n % 3, n % 5
if (mod3 * mod5 > 0)
puts n
else
out = ""
out += "Fizz" if (mod3 == 0)
out += "Buzz" if (mod5 == 0)
puts out
end
end
// http://alan.dipert.org/post/172774481/fizzbuzz-in-scala-and-clojure
(1 to 100) map { x =>
(x % 3, x % 5) match {
case (0,0) => "FizzBuzz"
case (0,_) => "Fizz"
case (_,0) => "Buzz"
case _ => x toString
}
} foreach println
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment