# FizzFizzFizzBuzz!

Given a positive integer n, output the "FizzFizzFizzBuzz" sequence for that number: print the decimal representations of the numbers 1 through n (inclusive on both ends), separated by newlines, however for each number in the sequence:

• If the number is divisible by both 3 and 5, instead of the number output Fizz the number of times it can divide by 3 without leaving a remainder, then Buzz the number of times it can divide by 5 without leaving a remainder. For example, 45 and 90 output FizzFizzBuzz, 75 outputs FizzBuzzBuzz, 135 outputs FizzFizzFizzBuzz, and 225 outputs FizzFizzBuzzBuzz.
• Else if the number is divisible by 3, instead of the number output Fizz the number of times it can divide by 3 without leaving a remainder. For example, 9 and 18 output FizzFizz, and 27 and 54 output FizzFizzFizz.
• Else if the number is divisible by 5, instead of the number output Buzz the number of times it can divide by 5 without leaving a remainder. For example, 25 outputs BuzzBuzz, 35 outputs Buzz, and 125 and 500 output BuzzBuzzBuzz.

Example to 200:

1
2
Fizz
4
Buzz
Fizz
7
8
FizzFizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
FizzFizz
19
Buzz
Fizz
22
23
Fizz
BuzzBuzz
26
FizzFizzFizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
FizzFizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzFizzBuzz
46
47
Fizz
49
BuzzBuzz
Fizz
52
53
FizzFizzFizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
FizzFizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
FizzFizz
73
74
FizzBuzzBuzz
76
77
Fizz
79
Buzz
FizzFizzFizzFizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzFizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
FizzFizz
BuzzBuzz
101
Fizz
103
104
FizzBuzz
106
107
FizzFizzFizz
109
Buzz
Fizz
112
113
Fizz
Buzz
116
FizzFizz
118
119
FizzBuzz
121
122
Fizz
124
BuzzBuzzBuzz
FizzFizz
127
128
Fizz
Buzz
131
Fizz
133
134
FizzFizzFizzBuzz
136
137
Fizz
139
Buzz
Fizz
142
143
FizzFizz
Buzz
146
Fizz
148
149
FizzBuzzBuzz
151
152
FizzFizz
154
Buzz
Fizz
157
158
Fizz
Buzz
161
FizzFizzFizzFizz
163
164
FizzBuzz
166
167
Fizz
169
Buzz
FizzFizz
172
173
Fizz
BuzzBuzz
176
Fizz
178
179
FizzFizzBuzz
181
182
Fizz
184
Buzz
Fizz
187
188
FizzFizzFizz
Buzz
191
Fizz
193
194
FizzBuzz
196
197
FizzFizz
199
BuzzBuzz


This is , so fewest bytes wins!

• May we instead just take N and output the Nth line? Commented Apr 10 at 1:17
• Did you mean 225?
– Neil
Commented Apr 10 at 7:37
• @noodleman No, you can't. Commented Apr 10 at 9:33
• Why three rules? That spec doesn't look golfed. Commented Apr 10 at 17:22
• RIght now, all of the examples given in the spec have the special property that 3 and/or 5 are the only primes dividing the number. It would remove a potential confusion to change some of those examples to numbers like 18 and 55 and 90. Commented Apr 12 at 0:24

# Vyxalj, 74 bitsv2, 9.25 bytes

ƛ35fǑkF½*∑∴


Try it Online!

Bitstring:

00000111010011110100011111010010001101011001000110010010101010001001010100


The FizzBuzz formula strikes once again.

## Explained

ƛ35fǑkF½*∑∴­⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁪‏‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁣⁡⁪‏‏​⁡⁠⁡‌⁢⁢​‎‎⁪⁡⁪⁠⁪⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁪‏‏​⁡⁠⁡‌­
ƛ            # ‎⁡To each number n in the range [1, input]:
35fǑ        # ‎⁢  How many times do 3 and 5 cleanly divide n? Call this X
kF½*∑∴  # ‎⁣  standard vyxal fizzbuzz
kF½     # ‎⁤    ["Fizz", "Buzz"]
*    # ‎⁢⁡     Repeated by each number in X, vectorised by zipping and reduction
∑∴  # ‎⁢⁢      Sum that, and get the biggest of n and that
💎


Created with the help of Luminespire.

• It needs the flags "j=" to give the correct output, shouldn't that count in the length? Commented Apr 11 at 14:59
• @Nowhereman by site consensus, flags do not count in the score Commented Apr 11 at 22:25
• codegolf.meta.stackexchange.com/q/14337/78850 Commented Apr 11 at 22:30
• What is the "💎" at the end of the explanation? And - sorry, but after reading the linked documentation and related Meta Q&A, and having intimate familiarity with compression algorithms generally, I still don't understand* the encoding scheme at all. What character set is used, and how does it represent the 2-bit remainder? If the code has been bit-packed and compressed, how can it be "explained" in a way that shows each part with a specific subsequence of characters? Commented Apr 12 at 12:44
• @KarlKnechtel the 💎 is to indicate that the explanation was generated using the Luminespire tool. The explanation uses the unpacked/uncompressed version of the program. Commented Apr 12 at 12:46

# JavaScript, 81 78 74 bytes

-4 bytes thanks to myself...
-3 bytes thanks to Arnauld.

f=n=>n&&f(n-1)+(g=s=>h=d=>n%d?'':s+h(d,n/=d),gFizz(3)+gBuzz(5)||n)+



Attempt This Online!

• @Arnauld Thank you! I honestly don't see why the reassignment of n doesn't break in the second call to g Commented Apr 10 at 23:12
• Since $3$ and $5$ are both prime (and a fortiori co-prime numbers), you can safely divide $n$ by one and then the other. Commented Apr 10 at 23:19
• Nice spot, by similar logic it's not necessary to reassign n Commented Apr 10 at 23:24
• Indeed! If $n$ has been modified, then we have at least one fizz or one buzz and don't need the original value anymore. Commented Apr 11 at 6:25

# Python 3, 94 bytes

f=lambda n,d=0:d>1>n%d and-~f(n/d,d)or n>d<1and[f(n-1),print(f(n,3)*'Fizz'+f(n,5)*'Buzz'or n)]


Try it online!

-1 byte thanks to Mukundan314

-1 byte thanks to cnln

If we can just return the result for a single number:

# Python 3, 72 bytes

lambda n:g(n,3)*'Fizz'+g(n,5)*'Buzz'or n
g=lambda i,d:i%d<1and-~g(i/d,d)


Try it online!

• You can save a byte by using / instead of // in g
– cnln
Commented Apr 17 at 13:37

# R 4.1 and above, 10897 92 bytes

5 bytes down thanks to giuseppe.

\(x)(1:x~1:x)<"
"
<=paste0
i=ifelse
~=\(x,y="")i(x%%3,i(x%%5,y,"Buzz"<~x/5),"Fizz"<~x/3)


This is a function, to call and print you need

cat(f(200),sep="")


Attempt it online

More bytes can be removed if just returning an array of strings without newlines is OK (ATO).

• Nice solution (+1), but you're not allowed to take the input as a variable; this should be either a function taking n as an argument, or a full program reading n from STDIN or command line args. See Defaults for Code Golf. Also, just FYI there's a site called Attempt This Online! (ATO) which is essentially a TIO clone that is maintained regularly and has newer languages; the creator of TIO has not been active on the internet for several years. Commented Apr 10 at 12:11
• @noodleman Thanks, converted to function call (and saved one byte while at it :-) Commented Apr 10 at 13:16
• welcome fellow R golfer :) We have some nice tips for golfing in R; I think this could get into the 90-byte range with a few improvements. Commented Apr 10 at 13:44
• @MartinModrák I was able to get it to 92 bytes, really only by three bytes, though, as the f= can be removed. Commented Apr 10 at 14:57
• The "array of strings without newlines" can be 86 though! Commented Apr 10 at 14:57

# sed-E, 87 84 bytes

This one takes the number of input lines for the number of lines to output. It outputs numbers as unary (if not Fizz or Buzz):

g
s/z*/zx/
h
:1
s/z(x+)\1\1$/zFizz\1/ t1 :2 s/z(x+)\1{4}$/zBuzz\1/
t2
s/z//
//s/x//g


It turned out three bytes could be saved by adding a z to the start of the line to avoid (^|z) plus backreference, when at the same time we add one x instead of incrementing newlines plus conversion.

Try it online!

It could be optimised down to 78 bytes, if FizzBuzzFizz would be allowed for 45 and the like.

# 05AB1E, 27 26 bytes

ENDÓ0Þ«2Lè”FizzÒÖ”#×JD?þ,


Try it online.

Explanation:

E                           # Loop N in the range [1, (implicit) input]:
N                          #  Push the current N
D                         #  Duplicate N
Ó                        #  Pop and push a list of exponents of its prime factorization
0Þ«                     #  Merge an infinite list of 0s to it
2L                   #  Push [1,2]
è                  #  0-based index this 1 and 2 into the list of exponents
”FizzÒÖ”          #  Push dictionary string "Fizz Buzz"
#         #  Split it on spaces: ["Fizz","Buzz"]
×        #  Repeat both the exponents amount of times
J       #  Join this pair of strings together
D      #  Duplicate it
?     #  Pop and print this copy without newline
#  Pop and push its characters to the stack
#  (if the string was empty, it pops without pushing anything)
þ   #  Only leave the digits of the top item
#  ("z" will become ""; numbers remain unchanged)
,  #  Then pop and print it with trailing newline as well


See this 05AB1E tip of mine (section How to use the dictionary?) to understand why ”FizzÒÖ” is "Fizz Buzz".

Some minor notes:

1. The 0-based $$\1^{st}\$$ and $$\2^{nd}\$$ exponents of the prime factorization are those for primes $$\3\$$ and $$\5\$$, a.k.a. the amount of times the number is evenly divisible by $$\3\$$ or $$\5\$$ respectively.
2. The 0Þ« is necessary because 05AB1E has modular indexing. So for the lists with only a single or two items instead of $$\\geq3\$$, it would incorrectly retrieve that single or first item when indexing with the modular 0-based $$\1\$$ or $$\2\$$.
3. The END...D?þ, is to print the number if the J results in an empty string "" and print the string otherwise. An alternative that's 1 byte shorter would be EN...N‚éθ, or END...‚éθ,, but unfortunately those are limited to $$\n\leq1001\$$ and $$\n\leq10001\$$ respectively.

# Jelly, 18 bytes

ọ3,5“¡Ṭ4“Ụp»ẋ"Fȯ)Y


A full program that accepts a positive integer and prints the FizzFizzFizzBuzz sequence.

Try it online!

#### How?

ọ3,5“¡Ṭ4“Ụp»ẋ"Fȯ)Y - Main Link: positive integer, N
)  - for each n in [1..N]:
3,5               -   [3, 5]
ọ                  -   {n} divisibility counts {[3,5]}
“¡Ṭ4“Ụp»       -   ["Fizz", "Buzz"] (compressed list of lists of characters)
ẋ"     -   zip {["Fizz", "Buzz"]} with repeat {divisibility counts}
F    -   flatten
ȯ   -   logical OR {n}
Y - join with newline characters
- implicit, smashing print


In my solutions, the function is found verbatim in the full program.

# C (gcc), 137 114 bytes (just function)

f(N){while(i++<N){for(j=i;j%3<1;j/=3)printf("Fizz");for(;j%5<1;j/=5)printf("Buzz");i-j||printf("%d",j);puts("");}}


# C (gcc), 153 133 bytes

i,j;f(N){while(i++<N){for(j=i;j%3<1;j/=3)printf("Fizz");for(;j%5<1;j/=5)printf("Buzz");i-j||printf("%d",j);puts("");}}main(){f(200);}


Try it online!

• Welcome to Code Golf SE, and nice solution! Commented Apr 10 at 12:31
• 141 bytes for the full program: Try it online! or 127 bytes for the function: Try it online! Commented Apr 10 at 12:41
• See also Tips for Golfing in C, everybody can learn a lot from that page :) Commented Apr 10 at 12:47
• Actually, you can save an extra byte on each revision I suggested. 126 for the function: Try it online! and 140 for the program: Try it online! Commented Apr 10 at 13:05
• 136 bytes Commented Apr 10 at 13:18

# Retina 0.8.2, 87 bytes

.+
$*¶$.
1A
%(.+
$* +^(\D*(1+))\2{4}$
Buz$1 +^(\D*(1+))\2\2$
Fiz$1 z1* zz 1+$.&


Try it online! Explanation:

.+
$*¶$.
1A
%(


Loop over all the integers from 1 to n.

.+
$*  Convert to unary. +^(\D*(1+))\2{4}$
Buz$1  Prefix Buz for each time the integer can be divided by 5. +^(\D*(1+))\2\2$
Fiz$1  Prefix Fiz for each time the integer can be divided by 3. z1* zz  Double the zs and delete the 1s from lines that had at least one factor. 1+$.&


Convert the remaining lines back to decimal.

# Uiua 0.10.3, 42 bytes SBCS

∵(&p◌⍥:±⧻.♭▽:"Fizz"_"Buzz"/+⍉⊞=3_5°/×.)+1⇡


## Explanation

+1⇡ # range [1 ... n]
∵(  # for each
°/×.            # prime factorization
⊞=3_5           # table with 1s where 3s and 5s are
/+⍉             # get number of factors equal to 3 and 5
▽:"Fizz"_"Buzz" # duplicate Fizz and Buzz that many times
♭               # flatten into one string
⍥:±⧻.           # if non-empty, flip stack
&p◌             # drop the top of stack and print
)


Try on Uiua Pad!

(replacing /+⍉⊞= with ∊ gives normal FizzBuzz)

# APL+WIN, 79 76 bytes.

Multi-line function shorter than one liner

Prompts for integer n

n←⎕
:for i :in ⍳n
((0=⍴t)↑i),t←∊(∊+/0=1|i÷(⊂3 5)*¨⍳⌈i*.5)/'Fizz' 'Buzz'
:end


Try it online! Thanks to Dyalog Classic

# Charcoal, 31 bytes

ＥＮ∨ΣＥ⪪FiBu²×⁺λzz⌕Ｘ⁰⮌↨⊕ι⁺³⊗μ⁰Ｉ⊕ι


Try it online! Link is to verbose version of code. Explanation:

 Ｎ                              Input n
Ｅ                               Map over implicit range
FiBu                      Literal string FiBu
⪪    ²                     Split into pairs
Ｅ                           Map over pairs
λ                  Current pair
⁺                   Concatenated with
zz                Literal string zz
×                    Repeated by
⌕               Index of
⁰    Literal integer 0 in
⁰             Literal integer 0
Ｘ              Vectorised raised to power
ι         Outer value
⊕          Incremented
↨           Converted to base
μ     Inner index
⊗      Doubled
⁺        Plus
³       Literal integer 3
⮌            Reversed
Σ                            Concatenate the results
∨                             Logical Or
ι Current value
⊕  Incremented
Ｉ   Cast to string
Implicitly print


# OCaml, 236 235 bytes

let rec d a b=if a mod b=0 then 1+d(a/b)b else 0
let rec r s i=if i=0 then""else s^(r s(i-1))
let f n=String.concat"\n"(List.init n(fun x->if((x+1)mod 3)*((x+1)mod 5)>0 then string_of_int(x+1)else(r"Fizz"(d(x+1)3))^(r"Buzz"(d(x+1)5))))


Attempt This Online!

First time golfing in a while, hoping to get more familiar with OCaml. First line counts clean divisions, second line implements string repetition, third line is pretty standard FizzBuzz.

• -1 byte by not doing let y=x+1

map{$r='';$_/=3,$r.=Fizz until$_%3;$_/=5,$r.=Buzz until$_%5;say$r||$_}1..<>  Try it online! # Haskell, 109 bytes n#d|nmodd<1=(last$"Fizz":["Buzz"|d>4])++(ndivd)#d|1>0=""
l=[s++do[0|s<" "];show i|i<-[1..],s<-[i#3++i#5]]


Try it online!

...I don't like it either.

# Python 3, 157 bytes

This is my best go without looking at the other solutions. Thanks to @SectorCorrupter for their tip on using * for string manipulation. My solution makes use of the fact that both 3 and 5 are prime, so dividing by one does not remove the other as a factor.

for x in range(1,n+1):
y=z=0;s=f'{x}'*bool(x%3and x%5)
while x%3<1and x>0:x//=3;y+=1
while x%5<1and x>0:x//=5;z+=1
print(s+'Fizz'*y+'Buzz'*z)


Try it online!

With the lambda function from @Jitse's solution, I can get it down to 126 125 bytes (-1 byte thanks to @Hazel へいぜる!):

g=lambda i,d:i%d<1and-~g(i/d,d)
for x in range(1,n+1):
s=f'{x}'*bool(x%3and x%5)
print(s+'Fizz'*g(x,3)+'Buzz'*g(x,5)


Try it online!

• You can squeeze an additional byte out of lambda solution with i/d instead of i//d. Commented Apr 16 at 3:19

->n{(1..n).map{|x|a,b=[3,5].map{|d|x.to_s(d)[/0*$/].size};a+b>0?"Fizz"*a+"Buzz"*b:x}}  Try it online! # JavaScript (Node.js), 143 bytes f=n=>{n-1&&f(n-1);console.log(n%3&&n%5?n:!(n%3)?'Fizz'.repeat(g(n,3,0)):''+!(n%5)?'Buzz'.repeat(g(n,5,0)):'')} g=(n,f,i)=>!(n%f)?g(n/f,f,++i):i  Attempt This Online! # Perl 5, 76 bytes sub c{$_%$_[0]?0:($_/=$_[0],1+&c)}say'Fizz'x c(5).'Buzz'x c(3)||$_ for 1..<>


Try it online!

(I was tempted to post eval<> (6 bytes) with some carefully crafted input, but I suspect there's already a rule against that.)

• I'm not sure exactly which standard loophole covers this as there are two that are similar; one gives the example of giving eval (or its equivalent) as your program, while the other disallows input other than that given by the question.
– Neil
Commented Apr 11 at 23:55

# C# (Hussy.Net), 92 bytes

Gr(N).E(i=>{string e = null;F(i,Dvb3,Dv3,_=>e+="Fizz");F(i,Dvb5,Dv5,_=>e+="Buzz");W(e,i);});


## Explanation

Gr(N)                            // Generate a range from 1 to N.
.E(i => {                     // Iterate over each element in the range.

string e = null;             // Create a variable to store output in.
F(                           // Method imitating a for loop.
i,                         // Start at the current value for i.
Dvb3,                    // Iterate while i % 3 == 0.
Dv3,                // Divide the iterator by 3.
_=>e+="Fizz");  // Append fizz to e.

// Same as previous chunk but for buzz.
F(i, Dvb5, Dv5, _ => e += "Buzz");

W(e, i);                     // Write e if it is not null, otherwise i.
});


# Python 3, 124 bytes

for i in range(1,n+1):
f=b=0;x=y=i
while x%3<1:f+=1;x/=3
while y%5<1:b+=1;y/=5
print('Fizz'*f+'Buzz'*b or i)


Try it online!

for(int i=1;i<=n;i++){int f=i,b=i;string e=null;for(;f%3<1;f/=3)e+="Fizz";for(;b%5<1;b/=5)e+="Buzz";Console.WriteLine(e??$"{i}");}  Try it online! # Rust, 153 bytes for i in 1..=200{let mut f=i;while f%3==0{print!("Fizz");f/=3;}let mut b=i;while b%5==0{print!("Buzz");b/=5;}if i%3>0&&i%5>0{print!("{}",i);}println!();}  Try it online! • 149 bytes Commented Apr 21 at 7:01 • 110 bytes Commented Apr 22 at 16:11 # sed 4.2.2 + coreutils factor, 65 bytes s/^/factor /e s/ 3\b/Fizz/g s/ 5\b/Buzz/g /z/s/[0-9 :]//g s/:.*//  Try it online! # PowerShell, 113 bytes function F($N){1..$N|%{$i=$_;for($j=$i;$j%3-eq0;$j/=3){'Fizz'};for(;($j%5)-eq0;$j/=5){'Buzz'};if($i-$j-eq0){$_}}}


Try it online!

function FizzBuzz($N){1..$N|%{$i=$_;for($j=$i;$j%3-eq0;$j/=3){'Fizz'};for(;($j%5)-eq0;$j/=5){'Buzz'};if($i-$j-eq0){$_}}}  Try it online! • You could rename the function FizzBuzz to F to save some bytes Commented Apr 17 at 17:33 • @noodleman duuuuuuhhh thought I'd done that! Cheers :-) Commented Apr 22 at 9:53 # Rust, 124 bytes |n|for mut i in 1..=n{if i%3*i%5>0{print!("{i}")}while i%3<1{i/=3;print!("Fizz")}while i%5<1{i/=5;print!("Buzz")}println!()}  Attempt This Online! • Welcome to CGCC, nice first submission! Commented Apr 22 at 14:54 # Perl 5, 60 bytes @x[++$q*3,$q*5]=(Fizz.$_,$_.Buzz),print$_||$q,$/for@x[1..<>]
`

Try it online!