FizzBuzz

From Esolang
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 lcm(3, 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
...

There also exist variants where the multipliers are different from 3 and 5, eg. 4 and 7 are common. FizzBuzz is also played as a non-computer educational game where a circle of children say one line each in order. FizzBuzz often appears in anecdotes about how most applicants to programming jobs are so incompetent that they cannot write a FizzBuzz program.

Examples

brainfuck

>>>>>>>>>>>>>>>[-]+>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+<<<[-]<[-]>>>[<<<+>>>-]+<[<<->+>-]<[>+<-]<[>>>-<<<[-]]>[-]>>[>-<<<+>>-]<<[>>+<<-]>>>[>[-]<<<[>>>+<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>-]<<<<<<<<<<<<<<<[>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<-]>>>>>>>>>>>>>>>>>>[<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>-]<<<<<<<<<<<<<<<+++<<[>+>->+<[>]>[<+>-]<<[<]>-]>[-]>[-]>[>>>>>>>>>>>>>>+<<<<<<<<<<<<<<-]>>>>>>>>>>>>>>>[-]<<<<[>>>>+<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>-]<<<<<<<<<<<<<<<[>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<-]>>>>>>>>>>>>>>>>>>>[<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>-]<<<<<<<<<<<<<<<<+++++<<[>+>->+<[>]>[<+>-]<<[<]>-]>[-]>[-]>[>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<-]>>>>>>>>>>>>>>>>[-]>[-]<<<[>>+>+<<<-]>>[<<+>>-]+>[>>[-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>[<<<<+>>>>-]+>[<<<<<<<<<[<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>+>-]<<<<<<<<<<<<<<[-]>[-]+>[-]+<[>[-<-<<[->+>+<<]>[-<+>]>>]++++++++++>[-]+>[-]>[-]>[-]<<<<<[->-[>+>>]>[[-<+>]+>+>>]<<<<<]>>-[-<<+>>]<[-]++++++++[-<++++++>]>>[-<<+>>]<<]<[.[-]<]<[-]>>>>>>>>>>>>>>[>+<-]>>>>>>>>>->[-]]<[<<<<<<<<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]>>>>>>>>>-]<<<->[-]]<[>>>>>>[-]>[-]<<<<<<<<[>>>>>>>+>+<<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]+>[<<<<<<<<<<<<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]>>>>>>>>>>>>->[-]]<[<<<<<<<<<<<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]>>>>>>>>>>>>-]<<<<<<-]<<<<<<<[-]++++++++++.[-]>>+>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+<<<[-]<[-]>>>[<<<+>>>-]+<[<<->+>-]<[>+<-]<[>>>-<<<[-]]>[-]>>[>-<<<+>>-]<<[>>+<<-]>>>]

Compiled using BFFuck, source code here.

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;
}

Chinese

定义 甲 为 整数, 赋值为 1
重复执行直到 (甲 < 100) 不满足
{
    如果 (甲 取模 3 等于 0) 则: 
    {
        打印 "Fizz"
    }
    如果 (甲 取模 5 等于 0) 则:
    {
        打印 "Buzz"
    }
    如果 (甲 取模 3 不等于 0 且 甲 取模 5 不等于 0) 则:
    {
        打印 甲
    }
    打印 "\n"
}


DIVSPL

1..100
Fizz=3
Buzz=5

Empty

Sl+∅1^∅TlTfTuTiM∅5^∅4+∅5^∅4+∅5^∅;∅f∅7;∅8^∅iSf#Fizz:∅7;∅8^∅nSiM∅5^∅4+∅5^∅4+∅5^∅4+∅5^∅4+∅5^∅;∅b∅7;∅8^∅uTbSb"B+uzz:∅7;∅8^∅nSu:#TnSn
∅9^∅9•∅l

Fizzbuzz


GotoScript

# Variables
1 max := 100
2 n := 0
3 to_print := ""

# Conditionals
4 GOTO 8 IF n % 15 = 0
5 GOTO 9 IF n % 3 = 0
6 GOTO 10 IF n % 5 = 0

# Print options
7 to_print := n
8 to_print := 'FizzBuzz'
9 to_print := 'Fizz'
10 to_print := 'Buzz'

# Output
11 GOTO 12 WHEN to_print != ""
12 PRINT to_print

# Continue loop
13 n += 1
14 GOTO 3

# End loop
15 GOTO WHEN n > max

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()

Satarcrimp

KW)(C$ki9kuhm4otjy[90ju{)($%K)W($LV:}
IMW$(C}MTW{#IJT
W$ FIZZBUZZ![0u,IWL_
VL
_L{
Li0
[MSI)
NTSIRP<SROPHI8op94769y8oUW)_KV}_$LVW)4]


Standard modern vernacular

定义整型变量 A ,然后初始化为 1 。
重复执行以下代码块直到 A <= 100 这个条件不满足为止:
    如果 A%3=0 则:
        输出"Fizz"。
    如果 A%5=0 则:
        输出"Buzz"。
    如果 A%3!=0 且 A%5!=0 则:
        输出 A 。
    输出一个换行符。
    将 A 加上 1。

Stringle

i "!"
f "!"
b "!"
z 1
z
p ""
#f 3 p "Fizz"
#f 3 f ""
#b 5 p p "Buzz"
#b 5 b ""
#p !0 $ p
#p 0 $ #i
#i +100 z 0
i i "!"
f f "!"
b b "!"
z

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⟇

wenyan

It may output as Chinese. Fizz is replaced by 黃河流水鳴濺濺。, and Buzz is replaced by 燕山胡騎鳴啾啾。.

有數一。名之曰「戊」。恆為是。

	除「戊」以三。所餘幾何。變其。名之曰「三餘」。
	除「戊」以五。所餘幾何。變其。名之曰「五餘」。

	夫「三餘」「五餘」中有陽乎。變其。名之曰「寂」。
	若「寂」者。吾有一言。曰「戊」。書之。
	若非。
		吾有一言。名之曰「聲」。
		若「三餘」者。加「聲」以「「黃河流水鳴濺濺。」」。昔之「聲」者。今其是矣。云云。
		若「五餘」者。加「聲」以「「燕山胡騎鳴啾啾。」」。昔之「聲」者。今其是矣。云云。
		吾有一言。曰「聲」。書之。
	云云。

	若「戊」等於一百者乃止也。
	加一以「戊」。昔之「戊」者。今其是矣。
云云。

And Correct Version(If you're in Wenyan Online IDE, Please uncheck [Print Hanzi]):

有數一。名之曰「戊」。恆為是。

	除「戊」以三。所餘幾何。變其。名之曰「三餘」。
	除「戊」以五。所餘幾何。變其。名之曰「五餘」。

	夫「三餘」「五餘」中有陽乎。變其。名之曰「寂」。
	若「寂」者。吾有一言。曰「戊」。書之。
	若非。
		吾有一言。名之曰「聲」。
		若「三餘」者。加「聲」以「「Fizz」」。昔之「聲」者。今其是矣。云云。
		若「五餘」者。加「聲」以「「Buzz」」。昔之「聲」者。今其是矣。云云。
		吾有一言。曰「聲」。書之。
	云云。

	若「戊」等於一百者乃止也。
	加一以「戊」。昔之「戊」者。今其是矣。
云云。

See also

External resources