184
\$\begingroup\$

Introduction

In our recent effort to collect catalogues of shortest solutions for standard programming exercises, here is PPCG's first ever vanilla FizzBuzz challenge. If you wish to see other catalogue challenges, there is "Hello World!" and "Is this number a prime?".

Challenge

Write a program that prints the decimal numbers from 1 to 100 inclusive. 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”.

Output

The output will be a list of numbers (and Fizzes, Buzzes and FizzBuzzes) separated by a newline (either \n or \r\n). A trailing newline is acceptable, but a leading newline is not. Apart from your choice of newline, the output should look exactly like this:

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

The only exception to this rule is constant output of your language's interpreter that cannot be suppressed, such as a greeting, ANSI color codes or indentation.

Further Rules

  • This is not about finding the language with the shortest approach for playing FizzBuzz, this is about finding the shortest approach in every language. Therefore, no answer will be marked as accepted.

  • Submissions are scored in bytes in an appropriate preexisting encoding, usually (but not necessarily) UTF-8. Some languages, like Folders, are a bit tricky to score--if in doubt, please ask on Meta.

  • Nothing can be printed to STDERR.

  • Feel free to use a language (or language version) even if it's newer than this challenge. If anyone wants to abuse this by creating a language where the empty program generates FizzBuzz output, then congrats for paving the way for a very boring answer.

    Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language.

  • If your language of choice is a trivial variant of another (potentially more popular) language which already has an answer (think BASIC or SQL dialects, Unix shells or trivial Brainfuck derivatives like Alphuck and ???), consider adding a note to the existing answer that the same or a very similar solution is also the shortest in the other language.

  • Because the output is fixed, you may hardcode the output (but this may not be the shortest option).

  • You may use preexisting solutions, as long as you credit the original author of the program.

  • Standard loopholes are otherwise disallowed.

As a side note, please don't downvote boring (but valid) answers in languages where there is not much to golf; these are still useful to this question as it tries to compile a catalogue as complete as possible. However, do primarily upvote answers in languages where the authors actually had to put effort into golfing the code.

Catalogue

var QUESTION_ID=58615;var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";var COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk";var OVERRIDE_USER=30525;var answers=[],answers_hash,answer_ids,answer_page=1,more_answers=true,comment_page;function answersUrl(index){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(index,answers){return"https://api.stackexchange.com/2.2/answers/"+answers.join(';')+"/comments?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:true,success:function(data){answers.push.apply(answers,data.items);answers_hash=[];answer_ids=[];data.items.forEach(function(a){a.comments=[];var id=+a.share_link.match(/\d+/);answer_ids.push(id);answers_hash[id]=a});if(!data.has_more)more_answers=false;comment_page=1;getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:true,success:function(data){data.items.forEach(function(c){if(c.owner.user_id===OVERRIDE_USER)answers_hash[c.post_id].comments.push(c)});if(data.has_more)getComments();else if(more_answers)getAnswers();else process()}})}getAnswers();var SCORE_REG=/<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;var OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(a){return a.owner.display_name}function process(){var valid=[];answers.forEach(function(a){var body=a.body;a.comments.forEach(function(c){if(OVERRIDE_REG.test(c.body))body='<h1>'+c.body.replace(OVERRIDE_REG,'')+'</h1>'});var match=body.match(SCORE_REG);if(match)valid.push({user:getAuthorName(a),size:+match[2],language:match[1],link:a.share_link,});else console.log(body)});valid.sort(function(a,b){var aB=a.size,bB=b.size;return aB-bB});var languages={};var place=1;var lastSize=null;var lastPlace=1;valid.forEach(function(a){if(a.size!=lastSize)lastPlace=place;lastSize=a.size;++place;var answer=jQuery("#answer-template").html();answer=answer.replace("{{PLACE}}",lastPlace+".").replace("{{NAME}}",a.user).replace("{{LANGUAGE}}",a.language).replace("{{SIZE}}",a.size).replace("{{LINK}}",a.link);answer=jQuery(answer);jQuery("#answers").append(answer);var lang=a.language;lang=jQuery('<a>'+lang+'</a>').text();languages[lang]=languages[lang]||{lang:a.language,lang_raw:lang.toLowerCase(),user:a.user,size:a.size,link:a.link}});var langs=[];for(var lang in languages)if(languages.hasOwnProperty(lang))langs.push(languages[lang]);langs.sort(function(a,b){if(a.lang_raw>b.lang_raw)return 1;if(a.lang_raw<b.lang_raw)return-1;return 0});for(var i=0;i<langs.length;++i){var language=jQuery("#language-template").html();var lang=langs[i];language=language.replace("{{LANGUAGE}}",lang.lang).replace("{{NAME}}",lang.user).replace("{{SIZE}}",lang.size).replace("{{LINK}}",lang.link);language=jQuery(language);jQuery("#languages").append(language)}}
body{text-align:left!important}#answer-list{padding:10px;width:290px;float:left}#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="language-list"> <h2>Shortest Solution by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr> </thead> <tbody id="languages"> </tbody> </table> </div> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr> </thead> <tbody id="answers"> </tbody> </table> </div> <table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table>

\$\endgroup\$
12
  • 1
    \$\begingroup\$ Nothing can be printed to STDERR. Is this true only when running, or also when compiling (assuming that is a separate step?) \$\endgroup\$
    – AShelly
    Sep 24, 2015 at 20:47
  • 1
    \$\begingroup\$ @AShelly Only when running \$\endgroup\$
    – Beta Decay
    Sep 24, 2015 at 20:48
  • 1
    \$\begingroup\$ I’m not sure I like the fact that you hardcoded the 100 into the challenge. That way, a program that just generates the expected output is a valid entry, but is not interesting for this challenge. I think the challenge should expect the program to input the number of items to output. \$\endgroup\$
    – Timwi
    Sep 24, 2015 at 23:28
  • 9
    \$\begingroup\$ @Timwi While I agree that it would make it (only slightly) more interesting, I've very often seen FizzBuzz as strictly 1 to 100 (on Wikipedia and Rosetta Code, for example). If the goal is to have a "canonical" FB challenge, it makes sense. \$\endgroup\$
    – Geobits
    Sep 25, 2015 at 0:50
  • 70
    \$\begingroup\$ A "vanilla fizzbuzz" sounds delicious. \$\endgroup\$ Sep 25, 2015 at 15:12

379 Answers 379

1 2 3
4
5
13
4
\$\begingroup\$

Seriously, 36 bytes

2╤R`;;3@%Y"Fizz"*)5@%Y"Buzz"*(+;I`Mi

Explanation:

2╤   push the value 10**2 (100)
R       pop a: push range(1,a+1)
`       start function literal
  ;;      duplicate the top of the stack twice
  3       push the value 3
  @       swap the top 2 values
  %       pop a,b: push a%b
  Y       pop a: push 1 if a==0, else 0
  "Fizz"  push the string "Fizz"
  *       pop a,b: push a*b (in this case, "Fizz" repeated b times)
  )       rotate the stack right by one ([a,b,c] -> [c,a,b])
  5@%Y"Buzz"*   Do the same thing as above, but with divisibility testing for 5 and using "Buzz"
  (       rotate the stack left by one
  +       pop a,b: push a+b (string concatenation here)
  ;       dupe top of stack
  I       pop a,b,c: push b if a is truthy, else c (here, a and b are the same string, either "", "Fizz", "Buzz", or "FizzBuzz", and c is the original integer)
`       end function literal
M       pop f,[a]: using each element of [a] as a temporary stack, evaluate f, and push the result
i       flatten [a] (push each value in [a] to the stack, starting from the end to preserve order)

Try it online.

\$\endgroup\$
2
  • \$\begingroup\$ Methinks you need a logical NOT in Seriously. \$\endgroup\$
    – lirtosiast
    Nov 10, 2015 at 23:11
  • \$\begingroup\$ @ThomasKwa Maybe. I was wishing it had one while I was writing this. Seriously has ~, which is unary bitwise negation, which is not quite the same thing. I might add one. \$\endgroup\$
    – user45941
    Nov 10, 2015 at 23:13
4
\$\begingroup\$

Javascript, 64 bytes

for(i=0;++i<101;)console.log((i%5?'':'fizz')+(i%3?'':'buzz')||i)
\$\endgroup\$
3
  • \$\begingroup\$ I'm not sure, but I think we allow alert for js output which would save you a few bytes. Nice answer btw. \$\endgroup\$
    – Maltysen
    May 6, 2016 at 1:43
  • 1
    \$\begingroup\$ We do allow alert. \$\endgroup\$
    – Riker
    May 6, 2016 at 1:48
  • 1
    \$\begingroup\$ All of the other JS answers on this particular challenge use console.log, except for one that outputs the entire text at once. \$\endgroup\$ Nov 26, 2016 at 15:09
4
\$\begingroup\$

8086 machine code, 70 68 62 bytes

00000000  31 c0 40 50 89 c2 89 e5  68 0a 24 d4 05 75 06 68  |1.@P....h.$..u.h|
00000010  7a 7a 68 42 75 89 d0 d4  03 75 06 68 7a 7a 68 46  |zzhBu....u.hzzhF|
00000020  69 89 d0 83 fc fa 75 08  d4 0a 86 c4 0d 30 30 50  |i.....u......00P|
00000030  b4 09 89 e2 cd 21 89 ec  58 3c 64 75 c5 c3        |.....!..X<du..|
0000003e

How it works:

            |   org 0x100
            |   use16
31 c0       |       xor ax, ax
40          |   aa: inc ax
50          |       push ax
89 c2       |       mov dx, ax
89 e5       |       mov bp, sp
68 0a 24    |       push 0x240a
d4 05       |       aam 5
75 06       |       jnz @f
68 7a 7a    |       push 0x7a7a
68 42 75    |       push 0x7542
89 d0       |   @@: mov ax, dx
d4 03       |       aam 3
75 06       |       jnz @f
68 7a 7a    |       push 0x7a7a
68 46 69    |       push 0x6946
89 d0       |   @@: mov ax, dx
83 fc fa    |       cmp sp, -6
75 08       |       jne @f
d4 0a       |       aam 10
86 c4       |       xchg al, ah
0d 30 30    |       or ax, 0x3030
50          |       push ax
b4 09       |   @@: mov ah, 0x09
89 e2       |       mov dx, sp
cd 21       |       int 0x21
89 ec       |       mov sp, bp
58          |       pop ax
3c 64       |       cmp al, 100
75 c5       |       jne aa
c3          |       ret
\$\endgroup\$
1
  • \$\begingroup\$ This is very nice and very clean work! Using the stack is quite elegant! Technically PUSH immediate was not available on the 8086, so this is really 80186+ machine code. Also, not sure if it's allowed or not, but this does show leading 0's on single digit numbers (01 02 Fizz 03, etc). \$\endgroup\$
    – 640KB
    Aug 28, 2019 at 1:04
4
\$\begingroup\$

Fortran, 188 bytes

do i=1,100
if(mod(i,3)==0.and.mod(i,5)==0)then;print'(a)','fizzbuzz'
elseif(mod(i,3)==0)then;print'(a)','fizz'
elseif(mod(i,5)==0)then;print'(a)','buzz'
else;print'(i0)',i
end if;enddo;end
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Aug 3, 2017 at 15:32
  • 1
    \$\begingroup\$ ...change print'(a)' etc. to print* to save 16 bytes \$\endgroup\$
    – roblogic
    Apr 5, 2019 at 14:49
  • \$\begingroup\$ I never used print* as it prints the characters with a space or two before hand, thus it doesn't match the required format. \$\endgroup\$
    – lewisfish
    Apr 15, 2019 at 14:09
4
\$\begingroup\$

APL, 41 bytes

⊣{⎕←∊(z,⍱/z←0=3 5|⍵)/'Fizz' 'Buzz'⍵}¨⍳100

Tested on GNU-APL (ver 1.7/980M)

\$\endgroup\$
4
  • \$\begingroup\$ Do you need the space between ' and ? \$\endgroup\$
    – Adalynn
    Aug 3, 2017 at 20:34
  • \$\begingroup\$ Oh, I haven't used GNU APL in a while, is the needed, since you display directly to STDOUT? \$\endgroup\$
    – Adalynn
    Aug 4, 2017 at 22:15
  • \$\begingroup\$ @Zacharý In GNU-APL monadic 'left tack' discards the value of the right argument. I read it as return nothing (since it points to left). I hope this is the correct way to phrase it. on the other hand, I do not have experience with Dyalog or a ready-setup to play with. \$\endgroup\$ Aug 6, 2017 at 18:45
  • \$\begingroup\$ Oh, for some reason I was thinking the return value was not written to STDOUT by default. \$\endgroup\$
    – Adalynn
    Aug 6, 2017 at 19:05
4
\$\begingroup\$

TeX, 304 bytes

\documentclass[9pt,a4paper]{article}\pagestyle{empty}\begin{document}\count0=0\count1=0\count3=3\count5=5\loop\advance\count1 by1\count0=0
\ifnum\count1=\count3 Fizz\advance\count3 by3\count0=1\fi\ifnum\count1=\count5 Buzz\advance\count5 by5\count0=1\fi\the\count1

\ifnum\count1<100\repeat\end{document}

Somehow count0 is necessary but I didn't check whether it is zero. It works and I have no idea why.

\$\endgroup\$
2
  • \$\begingroup\$ I'm assuming that extra newline is there for a reason, but what's the reason? \$\endgroup\$
    – Stephen
    Aug 20, 2017 at 0:06
  • 1
    \$\begingroup\$ @StepHen two newlines is a new paragraph \$\endgroup\$
    – Leaky Nun
    Aug 20, 2017 at 0:06
4
\$\begingroup\$

Symbolic Python, 324 bytes

I think I fried my brain making this...

_____=_;_=-~(_==_);___=_**(_*_+_);____=___+___/_+_+_
___=(('%'+`'¬'`[-_])*_)%(___+_,___*_-_*_-_)
___=`_>_`[_>_]+`__`[_*_+_]+___[~-_]*_,___[-_]+`__`[_]+___[~-_]*_
_=_>_
__('____=""'+(';_=-~_;____+=((_%-~-~(_==_)<(_==_))*___[_>_]+(_%-~-~-~-~(_==_)<(_==_))*___[_==_]'+`_____`[_==_]+`_==_`[_==_]+'`_`)+'+`"""
"""`)*____)
_=____

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Befunge-93, 61 bytes

1+::::3%|>.#_:"c"`#@_55+,
,,:,,0\v>"ziF"
,:,,$01>>5%#v_"zuB",

Try it online!

Decided to come back to this and make it conform to specification while shaving some bytes off. Look, no extra spaces!

\$\endgroup\$
0
4
\$\begingroup\$

Brain-Flak, 474 470 446 438 420 412 bytes

-18 bytes thanks to Nitroden!

-8 thanks to Wheat Wizard

(((()()()()()){}){}){({}[(()())]((((())))))}{}{({}<>)<>({}<>)<>({}()()<>)<>}<>{}{}{([{}]())({()<(({}())()){(<{}>)((((()()()())())((((({}{}){})[()]){}){}()))){({}<>)<>}}{}{(<{}>)(((((()()()())({})){}())(({})({}())({}{})))){({}<>)<>}}>}{}[()]){([](<>))<>((()()()()()){}){(({}<({}())>)){({}[()])<>}{}}{}<>([{}()]{}<>)<>{({}<>)(<>)}}{}(<>)<>}<>{}{{({}((((()()()){}){}){}){}<>)<>}({}(()()()()()){}<>)<>}<>{({}<>)<>}<>

Try it online!

Gosh, it's nice to finally check this off my to-do list.

Explanation:

Brain-Flak is obviously not very good at getting the modulo of numbers, so I bypassed this by pushing all the elements first.

(((()()()()()){}){}) Push 20
{ Loop 20 times
    ({}
    [(()())]    Push a 2 to represent a Buzz
    ((((()))))  Push 4 1s
    ) And decrement loop counter
}{} Pop the excess 0
{ Loop over the list of numbers
    ({}<>)<>({}<>)<> Transfer two of the elements to the other stack
    ({}()()<>)<>     And add 2 to the last one
}<>{}{} Pop the excess two elements

Now 1 represents normal numbers, 2 represents Buzz, 3 is Fizz and 4 represents FizzBuzz. Initially I just pushed the values that repeat every 15 numbers 7 times and popped the excess 5, but this way turned out to be slightly shorter.

{ Loop over each element
    ([{}]()) Subtract one from the current element
    ({ Fizz and/or Buzz if num is not 1
        <(({}())())  Subtract 1 and push, twice
        { Push Buzz if num was not 3
            (<{}>)((((()()()())())((((({}{}){})[()]){}){}()))){({}<>)<>}
        }{}
        { Push Fizz if num is not 2
            (<{}>)(((((()()()())({})){}())(({})({}())({}{})))){({}<>)<>}
        }
        >
        ()
    }{}[()]) Push -1 if neither Fizz nor Buzz were pushed
    {
        ([](<>)) Push length of list to other stack
        <>((()()()()()){}) Push 10 as the mod
        Div/mod algorithm
        {(({}<({}())>)){({}[()])<>}{}}{}<>([{}()]{}<>)<>
        Pushes n%10 to output stack and n/10 to the list stack
        { If div is not 0
            ({}<>) Push it to the other stack
            (<>)   Push 0
        }
    }{} Pop excess 0
    (<>)<> Push 0 to other stack to represent a newline
}<>{} Pop extra newline
{ Loop over values
    {
        ({}((((()()()){}){}){}){}<>)<>  Add 48 to every value
    }
    ({}(()()()()()){}<>)<> Turn 0s into newlines
}  Until there's two 0s in a row
<>{({}<>)<>}<> Reverse output

\$\endgroup\$
2
4
\$\begingroup\$

Word VBA, 124 189 bytes

Sub f()
For i=1 To 100
r=""
If i Mod 3=0 Then r="Fizz"
If i Mod 5=0 Then r=r & "Buzz"
If r="" Then r=i
Debug.?r
Next
End Sub

The code breakdown is fairly simple (yay, BASIC).

  1. Loop from 1 to 100

    For i=1 To 100
    
  2. Set a variable to be an empty string

    r=""
    
  3. Check the value on the counter to see if we should set the variable to Fizz

    If i Mod 3=0 Then r="Fizz"
    
  4. Check the counter to see if we need to add Buzz (adding it to an empty string is the same as setting that variable to Buzz)

    If i Mod 5=0 Then r=r & "Buzz"
    
  5. Check to see if the variable is still empty and therefore needs to be set to the counter value

    If r="" Then r=i
    
  6. Prints the results to the immediate window

    t = t & r & vbCr
    

EDIT: Used @Taylor-Scott's suggestions to tighten it up. Relies on the meta discussion about counting characters when your IDE forces whitespace. Specifically the conclusion that if you can paste the code from the answer into the IDE and run it without issues, then you don't have to count the results of autoformatting.

\$\endgroup\$
9
  • \$\begingroup\$ Actually, the spacing after paragraphs is just a display thing. The plain text output will not have this spacing. :-) If you could add a code breakdown and explanation, this would have my upvote. \$\endgroup\$
    – wizzwizz4
    Apr 22, 2016 at 16:47
  • \$\begingroup\$ Yes, those are the words I was looking for. Ta! \$\endgroup\$
    – phrebh
    Apr 22, 2016 at 17:57
  • \$\begingroup\$ +1, as promised! I have however noticed some irritating spaces around certain operators; can those be removed, or is it a language "feature"? \$\endgroup\$
    – wizzwizz4
    Apr 22, 2016 at 19:15
  • \$\begingroup\$ @wizzwizz4 Unfortunately, those spaces are a limitation of the language. The IDE forces them in there and you can't get around using the IDE. One of the many, many reasons that VBA is not a good candidate for code golf. \$\endgroup\$
    – phrebh
    Apr 25, 2016 at 19:03
  • \$\begingroup\$ @phrebh can you write in a non-IDE e.g. notepad++, textedit, etc. and run as VB? \$\endgroup\$
    – Riker
    May 5, 2016 at 21:09
4
\$\begingroup\$

SQL (Oracle), 112 108 bytes

SELECT NVL(DECODE(MOD(LEVEL,3),0,'Fizz')||DECODE(MOD(LEVEL,5),0,'Buzz'),LEVEL)FROM DUAL CONNECT BY LEVEL<101

db<>fiddle here

\$\endgroup\$
1
  • 1
    \$\begingroup\$ ''||LEVEL can be simple replaced with level \$\endgroup\$
    – Dr Y Wit
    Aug 28, 2019 at 14:15
4
\$\begingroup\$

Spaghetti, 522 bytes

main:0"n"goto store goto l l:100"n"goto retrieve goto areEqual"EOF"goto jumpIfTrue"n"goto retrieve 1 2 goto add"n"goto store 15"n"goto retrieve 2 goto modulus 0 goto areEqual"f"goto jumpIfTrue 3"n"goto retrieve 2 goto modulus 0 goto areNotEqual"b"goto jumpIfTrue"Fizz"1 goto print goto b t:3"n"goto retrieve 2 goto modulus 0 goto areEqual"l"goto jumpIfTrue"n"goto retrieve 1 goto print goto l b:5"n"goto retrieve 2 goto modulus 0 goto areNotEqual"t"goto jumpIfTrue"Buzz"1 goto print goto l f:"FizzBuzz"1 goto print goto l

Requires a newline at the end, cause otherwise the interpreter throws a hissy fit.

Spaghetti is a stack based language that promotes using goto extensively. That means, every single operation you use requires a goto statement along with it.

That being said, this took a while.

Commented version is at the Spaghetti examples.

Try it on the online interpreter! (Code must be pasted in)

\$\endgroup\$
4
\$\begingroup\$

LolCode, 392 383 353 bytes

This LolCode follows the 1.3 standard used by the lci interpreter.

Unfortunately, no online interpreter.

Not the best language to golf with, but it's fun!

Update 1: changed for loop to while loop

Update 2: removed newlines in favor of commas (soft command break)

HAI 1.3,CAN HAS STDIO?,I HAS A v ITZ 1,IM IN YR s,BOTH SAEM 0 AN MOD OF v AN 15,O RLY?,YA RLY,VISIBLE "FizzBuzz",NO WAI,BOTH SAEM 0 AN MOD OF v AN 3,O RLY?,YA RLY,VISIBLE "Fizz",NO WAI,BOTH SAEM 0 AN MOD OF v AN 5,O RLY?,YA RLY,VISIBLE "Buzz",NO WAI,VISIBLE v,OIC,OIC,OIC,BOTH SAEM v AN 100,O RLY?,YA RLY,GTFO,OIC,v R SUM OF v AN 1,IM OUTTA YR s,KTHXBYE
\$\endgroup\$
1
  • \$\begingroup\$ Nice first post, welcome to the site! \$\endgroup\$ Nov 3, 2020 at 15:32
4
\$\begingroup\$

Kakoune, 57 bytes

!seq 100
%s[05]$
Ab<esc>%s(\w+\n){3}
<a-h>s\d+
cFizz<esc>%s\d*b
cBuzz

asciicast

Explanation:

!seq 100               Call the external sh command `seq 100`, insert the output (numbers 1-100)
d                      ! inserts a trailing newline, delete it
 %s[05]$               Select all lines ending with 0 or 5, which is equivalent to being divisible by 5
Ab<esc>                Append a b to these lines, signifying that they should be buzzed later on
       %s(\d+b?\n){3}  Select every third line
<a-h>s\d+              Select every third number (unselecting the b's)
cFizz<esc>             Replace with Fizz
          %s\d*b       Select (including 0) digits followed by a b
cBuzz                  Replace with Buzz
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome back to CGCC! \$\endgroup\$
    – Razetime
    Nov 11, 2020 at 13:21
4
\$\begingroup\$

Java, 100 98 95 94 Bytes (only loop, full code is 136 bytes, see bottom)

for(int i=0;i++<100;){var s=i%3<1?"Fizz":"";s=i%5<1?s+"Buzz":s;System.out.println(s==""?i:s);}

Try it here. Probably could be a lot smaller. I just started to learn java a few days ago (although I do have experience in other languages), this is the best I can do. Breakdown of how it works. Sorry if I get some terms wrong or explain stuff poorly:

for(int i=0;i++<100;) 

Just the loop - starts at 1 goes to 100:

{var s=i%3<1?"Fizz":"";

Making a variable called "s", equal to a value, if "i" is divisible by 3 with a remainder of less than 1 (1 character/byte smaller than checking ==0), set "s" equal to "buzz". If it isn't, set it equal to "".

s=i%5<1?s+"Buzz":s;

Set s equal to a value: if i is divisible by 5 with a remainder of less than 1, set s equal to s + "buzz", if it isn't, set it equal to itself.

System.out.println(s==""?i:s);}

System.out.println is just a simple print statement. Inside it, check if s is equal to "" (if s wasn't divisible by 3 or 5, it would be "") print i (the number), otherwise print s.

Feedback is greatly appreciated. Edit: 94 bytes for just the loop, below is what I think the full script would be, 136 bytes : (

interface f{static void main(String[]a){for(int i=0;i++<100;){var s=i%3<1?"Fizz":"";s=i%5<1?s+"Buzz":s;System.out.println(s==""?i:s);}}}
\$\endgroup\$
1
4
+400
\$\begingroup\$

GNU sed 4.2.2, 126 bytes

This answer is in response to the bounty offered by @user41805 to golf further his sed script for this challenge.

s/^/@@,/;h
:
y/@123456789';,/123456789@;,'/
/@.$/!{x;G;s/..\n.//}
h
s/[@5].$/&Buzz/
s/.*,/Fizz/
s/.*\WB/B/
s/\W*//gp
g;/@@/d
t

Try it online!

The trick was to notice that no '0' will be printed, so I replaced it with '@' to be able at line 9 to refer to the extra characters [0';] as \W, thus shaving 3 bytes. Please check his description on how the code works.

\$\endgroup\$
4
\$\begingroup\$

Excel VBA, 81 Bytes

Anonymous VBE immediate window function that takes no input and outputs to the VBE immediate window.

For i=1To 100:o=IIf(i Mod 3,"","Fizz")+IIf(i Mod 5,"","Buzz"):?IIf(o="",i,o):Next

Ungolfed

For i=1To 100                   ''  iterate from 1 to 100
    o=IIf(i Mod 3,"","Fizz")    ''  set the out var to `Fizz` if i Mod 3 = 0, else empty
     +IIf(i Mod 5,"","Buzz")    ''  append `Buzz` to out var if i Mod 5 = 0
    ?IIf(o="",i,o)              ''  If out var is non-empty, output the out var
                                ''      else output i
Next

Worksheet Version, 95 Bytes

Anonymous VBE immediate window function that takes no input and outputs to the range [A1:A100]

 [A1]="=Let(r,Row(1:100),s,If(Mod(r,3),"""",""FIZZ"")&If(Mod(r,5),"""",""BUZZ""),If(s="""",r,s))
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can remove o="": and change your first iif to IIf(i Mod 3,"","Fizz") \$\endgroup\$ Sep 7, 2018 at 20:53
  • \$\begingroup\$ For the Subroutine version, the () after F isn't optional, and gets added in automatically. Same with the ? in debug. \$\endgroup\$
    – Selkie
    Feb 26, 2019 at 18:14
  • 1
    \$\begingroup\$ @Selkie, the behavior you are describing is known as autoformatting, and it is generally accepted in this community for VBA and for all languages that exhibit autoformatting, one may post their code from before it is autoformatted. in this case, that would mean that a couple of spaces may be removed, terminal "s dropped, changing print to ? and removal of the () from the Sub declaration. I suggest that you take a look at the Tips for Golfing in VBA page for more info \$\endgroup\$ Feb 26, 2019 at 21:30
4
\$\begingroup\$

Rattle, 54 44 bytes

Fizz&Buzz|!I=[g+bs%3[0b0b^0]g%5[0b1b^0]B]100

Try it Online!

This [was] my first answer in my new programming language! (This answer has since been golfed, and works on my new online interpreter)

Eventually, this programming language might have a more concise way to solve this challenge.

^this ended up being true - after a couple updates, 10 bytes can be shaved off the original answer (without implementing trivial built-ins)

Explanation

Fizz&Buzz         a variable containing the text Fizz and Buzz
|                 signals the end of the input
!                 is a flag to disable implicit printing at EOF
I                 splits the variable into parts and stores it in consecutive memory slots
=                 sets top of stack to 0
[                 start outer loop
g+bs              gets value at slot 3 (starts at zero), increments, appends it to a buffer, saves it to slot 3
%3                takes the current value on stack and pushes the value mod 3 to stack
[0b0b^0]          if the value on stack is equal to 0, concatenates value from memory slot 0 ("Fizz") to a buffer and nullifies the 0th element of the buffer
g%5               pushes value from slot 3 to stack, takes the value and pushes the value mod 5 to stack
[0b1b^0]          if the value on stack is equal to 0, concatenates the value from memory slot 1 ("Buzz") to the buffer and nullifies the 0th element of the buffer if not null already
B                 if the buffer is non-empty, prints buffer
]100              end outer loop - repeats 100 times
\$\endgroup\$
4
\$\begingroup\$

Fact, 11 bytes (SBCS)

ef̀|3 5^ŹFh̃õ

Explanation

e            1e2 (i.e. 100)
 f̀           for i in 1:100
  |3 5           Divisibility by [3, 5] (vectorizes)
      ^          String repeat by (vectorizes):
       ŹF            "FizzBuzz"
         h̃           Halved (i.e. ["Fizz", "Buzz"])
          õ      Logical OR with i
                 (in Fact, lists are false when all items are "" or 0
                  or when the list is empty)
Implicit grid (smash inner lists and join outer list by newline)

Fork and test

Fact, 15 bytes

ef̀|3 5^'b̀M̀b̀Ò'h̃õ

No "FizzBuzz" builtin, simply uses string compression

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome back old friend! \$\endgroup\$
    – lyxal
    Aug 30, 2021 at 2:01
  • 1
    \$\begingroup\$ Welcome to Code Golf, new user! winks \$\endgroup\$ Aug 30, 2021 at 2:01
  • 1
    \$\begingroup\$ Thank you. :) Hate to say that, but this is going to be my last post before a 4-month hiatus. I'll return as soon as possible \$\endgroup\$
    – user106728
    Aug 30, 2021 at 2:07
4
\$\begingroup\$

HTML & CSS, 502 bytes

body{counter-reset:n}
p::after{counter-increment:n;content:counter(n)}
p:nth-child(3n)::after{content:"Fizz"}
p:nth-child(5n)::after{content:"Buzz"}
p:nth-child(15n)::after{content:"FizzBuzz"}
<p><p><p><p><p><p><p><p><p><p>
<p><p><p><p><p><p><p><p><p><p>
<p><p><p><p><p><p><p><p><p><p>
<p><p><p><p><p><p><p><p><p><p>
<p><p><p><p><p><p><p><p><p><p>
<p><p><p><p><p><p><p><p><p><p>
<p><p><p><p><p><p><p><p><p><p>
<p><p><p><p><p><p><p><p><p><p>
<p><p><p><p><p><p><p><p><p><p>
<p><p><p><p><p><p><p><p><p><p>

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Nice answer! You don't need the <div> elements and you can skip the closing </p> tags as well, it should still render fine. \$\endgroup\$
    – emanresu A
    Nov 22, 2021 at 2:53
  • \$\begingroup\$ Thanks @emanresuA. I still needed an element to apply the counter-reset to but it could just have been the body. Removing the closing </p> helped shave off a lots of bytes. \$\endgroup\$ Nov 22, 2021 at 9:02
4
\$\begingroup\$

Vyxal, Hj, 25 19 12 bytes

-6 bytes thanks to Aaron
-7 bytes thanks to lyxal

Flagless 15 bytes
Longer than lyxal's answer, but uses a different technique

ƛ₃kf*n₅kb*+∴               stack is preset to a 100 because of the `H` flag

ƛ                          lambda map with variable n
 ₃                         push 1 if n%3 == 0 (we'll call the return a)
  kf                       push constant Fizz to the stack
    *                      push a*kF
     n                     push n
      ₅                    push 1 if n%5 == 0 (we'll call it b)
       kb                  push Buzz to the stack
         *                 push b*kB
          +                add last two elements of the stack ""/Fizz/Buzz
           ∴               push the maximum of n and ""/Fizz/Buzz/FizzBuzz

Vyxal prints out the last element of the stack by default (the `j` flag joins the list with new lines)

Try it Online!

\$\endgroup\$
6
  • 1
    \$\begingroup\$ You can use the register instead of variables, and the H flag, for 19 bytes \$\endgroup\$ Jun 27, 2021 at 17:30
  • \$\begingroup\$ Try it Online! for 16 bytes using the register, H flag and string multiplication instead of if statements \$\endgroup\$
    – lyxal
    Jun 27, 2021 at 23:01
  • \$\begingroup\$ Try it Online! for 12 \$\endgroup\$
    – lyxal
    Jun 28, 2021 at 1:34
  • \$\begingroup\$ @lyxal and Aaron Thanks! \$\endgroup\$
    – mathcat
    Jun 28, 2021 at 14:35
  • \$\begingroup\$ @lyxal would you please check the explanation? I didn't get the last part so i guessed \$\endgroup\$
    – mathcat
    Jun 28, 2021 at 15:34
4
\$\begingroup\$

Python, 108 102 bytes

Thanks to caird coinheringaahing for -6 bytes!

for x in range(1,101):
    y=""
    if x%3<1:y="Fizz"
    if x%5<1:y+="Buzz"
    y=y or x
    print(y)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can use <1 instead of ==0 for -1 byte, and instead of if y=="":y=x", you can do y=y or x. Also, this just outputs the numbers from 1 to 100: Try it online! This is 82 bytes \$\endgroup\$ Jan 30 at 18:10
4
\$\begingroup\$

Quipu, 101 bytes

1&0&'F0&'B1&\n
++[]'i[]'u[]/\
1%3&'z5&'z6&0&
1&%%'z%%'z==??
%%3&/\5&/\0&
1&>>  >>6&[]
>>      ??/\
::

Try it online!

Ungolfed:

" 0  1  2  3  4  5  6"
"--------------------"
 1& 0& 'F 0& 'B 1& \n
 ++ [] 'i [] 'u [] /\
 1% 3& 'z 5& 'z 6& 0&
 1& %% 'z %% 'z == ??
 %% 3& /\ 5& /\ 0&
 1& >>    >> 6& []
 >>          ?? /\
 ::

Thread 0 increments the accumulator (acc) every iteration, stopping execution if acc == 101, then jumps to thread 1.

Thread 1 checks if (acc % 3) > 0. If so, it jumps to thread 3, otherwise it jumps to thread 2.

Thread 2 prints Fizz, then jumps to thread 3.

Thread 3 checks if (acc % 5) > 0. If so, it jumps to thread 5, otherwise it jumps to thread 4.

Thread 4 prints Buzz, then jumps to thread 6.

Thread 5 looks back to thread 1 to see if (acc % 3) == 0. If so, it jumps to thread 6, otherwise it prints acc, then jumps to thread 6.

Thread 6 prints a newline, then jumps back to thread 0.

I think this could probably be refactored to golf away some of the whitespace, but I'm not doing that since if I do, I'll likely get sucked in and do nothing else for ~8 hours or so. :/

\$\endgroup\$
1
  • \$\begingroup\$ Two less whitespace by adding four more threads. I gave this a go before looking at your answer and ended up at 102 bytes through a different method \$\endgroup\$
    – Jo King
    Sep 6, 2021 at 8:11
3
\$\begingroup\$

Mouse, 75 bytes

1I:(I.101<^0J:I.3\0=["Fizz"J.1+J:]I.5\0=["Buzz"J.1+J:]J.0=[I.!]"!"I.1+I:)$

Ungolfed:

1 I:               ~ Start a loop index at 1
( I. 101 < ^       ~ While I < 101...
  0 J:             ~ Begin a divisibility indicator at 0
  I. 3 \ 0 = [     ~ If I % 3 == 0
    "Fizz"         ~ Print "Fizz" to STDOUT
    J. 1 + J:      ~ Increment J
  ]
  I. 5 \ 0 = [     ~ If I % 5 == 0
    "Buzz"         ~ Print "Buzz" to STDOUT
    J. 1 + J:      ~ Increment J
  ]
  J. 0 = [         ~ If neither 3 nor 5 divides I
    I. !           ~ Print I to STDOUT
  ]
  "!"              ~ Print a newline
  I. 1 + I:        ~ Increment I
)
$
\$\endgroup\$
3
  • 1
    \$\begingroup\$ I'm gonna upvote if you add a cat-program. \$\endgroup\$
    – flawr
    Sep 24, 2015 at 20:16
  • 1
    \$\begingroup\$ @flawr You should submit your own cat program. It will be a game of cat and mouse. \$\endgroup\$
    – Alex A.
    Sep 24, 2015 at 20:44
  • 1
    \$\begingroup\$ @flawr Not quite cat, but I did add a zcat program. \$\endgroup\$ Sep 25, 2015 at 2:23
3
\$\begingroup\$

Processing, 74 bytes

This is based on the Java answer by Geobits. I converted it into Processing and since Processing is similar to Java, the code is a lot similar to Geobits'.

for(int i=0;i++<100;)println((i%3<1?"Fizz":"")+(i%5<1?"Buzz":i%3<1?"":i));
\$\endgroup\$
1
  • 9
    \$\begingroup\$ Ah, Processing. Java's willful son, stubbornly insisting that he's going to grow up to be an artist :D \$\endgroup\$
    – Geobits
    Sep 24, 2015 at 20:24
3
\$\begingroup\$

awk, 62

END{for(x="Fizz";i<100;y="Buzz")print++i%15?i%5?i%3?i:x:y:x y}

Pretty sure there's no surprises here.

Call

awk 'END{for(x="Fizz";i<100;y="Buzz")print++i%15?i%5?i%3?i:x:y:x y}'

then press Ctrl-D to signal end of input.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'm curious if the Ctrl-D should be included in the byte-count. Using a BEGIN block and rearranging the operators a bit adds 1 byte, i.e. BEGIN{for(x="Fizz";i<100;y="Buzz")print++i%3?i%5?i:y:i%5?x:x y} \$\endgroup\$ May 30, 2017 at 16:19
3
\$\begingroup\$

Common Lisp, 121

(dotimes(x 100)(flet((d(n)(= 0(mod(1+ x)n))))(princ(cond((d 15)"FizzBuzz")((d 3)"Fizz")((d 5)"Buzz")(t(1+ x)))))(terpri))

Readable:

(dotimes (x 100)
  (flet ((d (n) (= 0 (mod (1+ x) n))))
    (princ (cond ((d 15) "FizzBuzz")
                 ((d 3) "Fizz")
                 ((d 5) "Buzz")
                 (t (1+ x)))))
  (terpri))
\$\endgroup\$
3
\$\begingroup\$

Ruby, 59

I wanted to make a Ruby version using the slick modulo/integer-division/string-multiplication trick from feersum's Python answer (though unfortunately Ruby doesn't handle string multiplication the same way, so I spent some bytes on that):

100.times{|i|puts "#{i+1}\r"+"Fizz"*(i%3/2)+"Buzz"*(i%5/4)}

Note that this uses a carriage return \r without a newline. I don't know how portable this is; it works on my Linux and should work on Linux in general, as well as on Mac, but I'm not sure how Windows handles it. Without that, here's a 61-byte version:

100.times{|i|puts ("Fizz"*(i%3/2)+"Buzz"*(i%5/4))[/.+/]||i+1}
\$\endgroup\$
2
  • \$\begingroup\$ You don't need space after puts. \$\endgroup\$
    – Vasu Adari
    Dec 7, 2015 at 3:20
  • \$\begingroup\$ Your first answer always prints the number even when it should only print the string \$\endgroup\$
    – Jo King
    Nov 21, 2021 at 23:25
3
\$\begingroup\$

Commodore Basic, 87 bytes

1F┌I=1TO100:F=F+1:B=B+1:IFF=3T|?"FIZZ";:F=0
2IFB=5T|?"BUZZ";:B=0
3IFF>0A/B>0T|?I;
4?:N─

Or in "shifted mode" to get both lower- and upper-case letters, but with the byte values of lowercase and uppercase swapped relative to ASCII-1967 (press COMMODORE+SHIFT):

1fOi=1to100:f=f+1:b=b+1:iff=3tH?"Fizz";:f=0
2ifb=5tH?"Buzz";:b=0
3iff>0aNb>0tH?i;
4?:nE

Usual PETSCII-to-Unicode substitutions: = SHIFT+O, | = SHIFT+H, / = SHIFT+N, = SHIFT+E

Commodore Basic doesn't have a "modulus" operation, so I need to use alternate methods to figure out when to print what: keeping a pair of counters turns out to be fewer bytes than dividing and checking for integer-ness. It also doesn't have a true logical "and" (despite the manual saying otherwise), so I need to do an explicit comparison against zero to decide if I should print the plain number.

\$\endgroup\$
1
  • \$\begingroup\$ @DLosc, you can switch into "shifted mode" which gives access to both upper- and lower-case, but with the character positions reversed relative to the ASCII-1967 which people are familiar with (eg. character #65 is lowercase "a" rather than uppercase). \$\endgroup\$
    – Mark
    Sep 28, 2015 at 2:27
3
\$\begingroup\$

Gol><>, 40 bytes

`e2RFL5%zR"zzuB"L3%zR"zziF"lQlRoaoC|LN|;

Updated for 0.4.0! I'm still tinkering with loops and trying to figure out how to do things best, but this is looking good so far.

Try it online.

Explanation

`e            Push 'e', or 101
2RF ... |     Execute F for loop twice - the first time activates the loop, and the
              second time updates it. This effectively makes the loop start from 1
L5%z          Push 1 if loop counter % 5 is 0, else 1
R"zzuB"       Push "Buzz" (top of stack) number of times
L3%z          Push 1 if loop counter % 3 is 0, else 1
R"zziF"       Push "Fizz" (top of stack) number of times
lQ ... |      If the stack is not empty...
  lRo         Output stack
  ao          Output newline
  C           Continue for loop
LN            Otherwise, print loop counter with newline

;             Terminate program

As we can see, there's a lot of abuse of R, which pops the top of the stack and executes the next instruction that many times.

\$\endgroup\$
1
  • \$\begingroup\$ That's a smart idea! Mind if I use it? \$\endgroup\$ Oct 28, 2015 at 12:16
1 2 3
4
5
13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.