| Bytes | Lang | Time | Link |
|---|---|---|---|
| 049 | Ruby | 241027T082107Z | Fred |
| 071 | Raku Perl 6 rakudo | 240721T155624Z | bb94 |
| 120 | Setanta | 240721T153857Z | bb94 |
| 092 | C# Hussy.Net | 240415T010516Z | Taco |
| 124 | Rust | 240422T134516Z | Dornteuf |
| 094 | Python 3 | 240410T075442Z | Jitse |
| 113 | PowerShell | 240417T092003Z | Mark Har |
| 065 | sed 4.2.2 + coreutils factor | 240418T185127Z | Digital |
| 060 | Perl 5 | 240418T010432Z | Sisyphus |
| 157 | Python 3 | 240415T160943Z | cnln |
| 076 | Perl 5 | 240411T202146Z | Martin K |
| 074 | JavaScript | 240410T012656Z | Matthew |
| 143 | JavaScript Node.js | 240411T225628Z | 2pichar |
| 085 | Ruby | 240411T070528Z | G B |
| 076 | APL+WIN | 240410T110718Z | Graham |
| 084 | sed E | 240410T105512Z | Philippo |
| 109 | Haskell | 240411T040453Z | totallyh |
| 042 | Uiua 0.10.3 | 240410T200954Z | Tbw |
| 075 | Perl 5 | 240410T211402Z | Xcali |
| 235 | OCaml | 240410T190412Z | sporkl |
| 087 | Retina 0.8.2 | 240410T152533Z | Neil |
| 092 | R 4.1 and above | 240410T113250Z | Martin M |
| nan | In my solutions | 240410T122625Z | virolino |
| 018 | Jelly | 240410T132624Z | Jonathan |
| 031 | Charcoal | 240410T112504Z | Neil |
| 026 | 05AB1E | 240410T082537Z | Kevin Cr |
| 925 | Vyxal j | 240409T234210Z | lyxal |
Ruby 49 char
101.times{|n|puts'FizzBuzz '[i=n**4%-15,i+13]||n}
Raku (Perl 6) (rakudo), 76 71 bytes
Returns a list of the strings.
{map {[~] map {.base($^c)~~/(0)*$/;$^b x$0},<Fizz 3 Buzz 5>or$_},1..$_}
Setanta, 120 bytes
gniomh(n){le i idir(1,n+1){s:=""nuair-a i%3<1{s+="Fizz"i/=3}nuair-a i%5<1{s+="Buzz"i/=5}scriobh((s==""&go_teacs(i))|s)}}
C# (Hussy.Net), 92, 90, 92 bytes
Gr(N).E(i=>{string e=null;F(i,Dvb3,Div3,_=>e+="Fizz");F(i,Dvb5,Div5,_=>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.
Div3, // Divide the iterator by 3.
_=>e+="Fizz"); // Append fizz to e.
// Same as previous chunk but for buzz.
F(i, Dvb5, Div5, _ => 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)
C# (.NET Core), 130 bytes
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}");}
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!();}
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!()}
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)]
-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)
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){$_}}}
PowerShell, 120 bytes
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){$_}}}
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/:.*//
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)
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)
Perl 5, 76 bytes
sub c{$_%$_[0]?0:($_/=$_[0],1+&c)}say'Fizz'x c(5).'Buzz'x c(3)||$_ for 1..<>
(I was tempted to post eval<> (6 bytes) with some carefully crafted input, but I suspect there's already a rule against that.)
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),g`Fizz`(3)+g`Buzz`(5)||n)+`
`
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
Ruby, 85 bytes
->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}}
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
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.
It could be optimised down to 78 bytes, if FizzBuzzFizz would be allowed for 45 and the like.
Haskell, 109 bytes
n#d|n`mod`d<1=(last$"Fizz":["Buzz"|d>4])++(n`div`d)#d|1>0=""
l=[s++do[0|s<" "];show i|i<-[1..],s<-[i#3++i#5]]
...I don't like it either.
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
)
(replacing /+⍉⊞= with ∊ gives normal FizzBuzz)
Perl 5, 75 bytes
map{$r='';$_/=3,$r.=Fizz until$_%3;$_/=5,$r.=Buzz until$_%5;say$r||$_}1..<>
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))))
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
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.
R 4.1 and above, 108 97 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="")
More bytes can be removed if just returning an array of strings without newlines is OK (ATO).
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);}
Jelly, 18 bytes
ọ3,5“¡Ṭ4“Ụp»ẋ"Fȯ)Y
A full program that accepts a positive integer and prints the FizzFizzFizzBuzz sequence.
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
Charcoal, 31 bytes
EN∨ΣE⪪FiBu²×⁺λzz⌕X⁰⮌↨⊕ι⁺³⊗μ⁰I⊕ι
Try it online! Link is to verbose version of code. Explanation:
N Input `n`
E Map over implicit range
FiBu Literal string `FiBu`
⪪ ² Split into pairs
E Map over pairs
λ Current pair
⁺ Concatenated with
zz Literal string `zz`
× Repeated by
⌕ Index of
⁰ Literal integer `0` in
⁰ Literal integer `0`
X 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
I Cast to string
Implicitly print
05AB1E, 27 26 bytes
ENDÓ0Þ«2Lè”FizzÒÖ”#×JD?`þ,
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:
- 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.
- 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\$. - The
END...D?`þ,is to print the number if theJresults in an empty string""and print the string otherwise. An alternative that's 1 byte shorter would beEN...N‚éθ,orEND...‚éθ,, but unfortunately those are limited to \$n\leq1001\$ and \$n\leq10001\$ respectively.
Vyxal j, 74 bitsv2, 9.25 bytes
ƛ35fǑkF½*∑∴
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.