FizzBuzz
Jump to navigation
Jump to search
A FizzBuzz program is essentially a counting program. It prints all integers in a certain range, generally 1 to 100, one-by-one. However, multiples of 3 are replaced with "Fizz", and multiples of 5 are replaced with "Buzz". Multiples of 15 (e.g., multiples of both 3 and 5) are replaced with "FizzBuzz".
In other words, the program outputs:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 ...
Examples
Befunge
>1+:3%!#v_>:5%!#v_v ^,*52.:_ `@#\"d" :< v"Fizz"< >v >,,,,:5% | v"Buzz"<< >,,,,v ^ ,*52< <
More Compact Version:
>1+:3%!#v_>:5%!#v_v ^,*52.:_ `@#\"d" :< v"Fizz"< >v >,,,,:5% | v,,,,"Buzz"<< ^,*52< <
BunnyBell
include lib/Math/Ops.bbe
func @main char @c int @i 0 label @loop beq ((:mod &i 15) 0)) FizzBuzz beq ((:mod &i 5) 0)) Buzz beq ((:mod &i 3) 0)) Fizz goto nonCase
label @Fizz out "Fizz\n" goto loopEnd
label @Buzz out "Buzz\n" goto loopEnd
label @FizzBuzz out "FizzBuzz\n" goto loopEnd
label @nonCase out &i out "\n"
label @loopEnd give &i 1 bleq &i 100 loop return
C
#include <stdio.h> int main(void) { int i; for(i=1; i<=100; i++) { if((i%15)== 0)//improved from "(i%3)&&(i%5)" printf("FizzBuzz\n"); else if((i%3)==0) printf("Fizz\n"); else if((i%5)==0) printf("Buzz\n"); else printf("%d\n",i); } return 0; }
A more cleaned up version:
#include <stdio.h> int main () { char *fizz; char *buzz; for (int i = 1; i <= 100; i++) { fizz = (i % 3 == 0) ? "Fizz" : ""; // Fizz on 3's buzz = (i % 5 == 0) ? "Buzz" : ""; // Buzz on 5's if (i % 3 == 0 || i % 5 == 0) { printf("%s%s\n", fizz, buzz); } else { printf("%d\n", i) } } return 0; }
DIVSPL
1..100 Fizz=3 Buzz=5
Fizzbuzz
Mathematics
(i.e. "Fizz")
(i.e. "Buzz")
gives
[1, 2, -4122, 4, -8022, -4122, 7, 8, -4122, -8022, 11, -4122, 13, 14, -41228022, 16, ...]
Python equivalent:
from math import floor, ceil phi = -4122 beta = -8022 fb = lambda x: x * ceil(x/3 - x//3) * ceil(x/5 - x//5) + beta * (1 + floor(x//5 - x/5)) + phi * (1 + floor(x//3 - x/3)) * (1 + (1 + floor(x//5 - x/5)) * 9999) [fb(i) for i in range(1, 31)]
Python 3
for i in range(1,101): if i%3==0: print("Fizz",end="") if i%5==0: print("Buzz",end="") if i%3 and i%5: print(i,end="") print()
TeX
\newcount\-\let~\advance\day0\loop~\-1~\day1~\mit\ifnum\-=3\-0Fizz\fi\ifnum\fam=5Buzz\rm\fi\ifvmode\the\day\fi\endgraf\ifnum\day<`d\repeat\bye
Thue
FizBuzz in Thue by Amb ::= >t|::=t> >f|::=f> >|::=|> >tf::=t||>fF >f?::=f||||>?B F|::=|F F?::=?F >?n::=?n>pp[sp] >?Fn::=?n>[fi[sp]zz] >?Bn::=?n>[bu[sp]zz] >?BFn::=?n>[fizz[sp]buzz] [sp]::=~ [fizz]::=~Fizz [buzz]::=~Buzz [fizzbuzz]::=~FizzBuzz pp0::=0p[0]p pp1::=1p[1]p pp2::=2p[2]p pp3::=3p[3]p pp4::=4p[4]p pp5::=5p[5]p pp6::=6p[6]p pp7::=7p[7]p pp8::=8p[8]p pp9::=9p[9]p [0]::=~0 [1]::=~1 [2]::=~2 [3]::=~3 [4]::=~4 [5]::=~5 [6]::=~6 [7]::=~7 [8]::=~8 [9]::=~9 pp$::=$ >0::=0> >1::=1> >2::=2> >3::=3> >4::=4> >5::=5> >6::=6> >7::=7> >8::=8> >9::=9> >$::=+$ 0+::=-1 1+::=-2 2+::=-3 3+::=-4 4+::=-5 5+::=-6 6+::=-7 7+::=-8 8+::=-9 9+::=+0 0-::=-0 1-::=-1 2-::=-2 3-::=-3 4-::=-4 5-::=-5 6-::=-6 7-::=-7 8-::=-8 9-::=-9 n+::=<n1 n-::=<n ?<::=<? f<::=<f t<::=<t |<::=<| ^<::=^> ::= ^>t||f||||?n1$
Vyxal
Ĥƛ3∻ı½¬*n5∻ı½∧*+n⟇
See also
- Project Euler Problem 1, a problem similar to FizzBuzz
- DIVSPL, a domain-specific language for solving FizzBuzz problems