151
\$\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.

  • Unlike our usual rules, 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\$
  • 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 '15 at 20:47
  • \$\begingroup\$ @AShelly Only when running \$\endgroup\$ – Beta Decay Sep 24 '15 at 20:48
  • \$\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 '15 at 23:28
  • 7
    \$\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 '15 at 0:50
  • 59
    \$\begingroup\$ A "vanilla fizzbuzz" sounds delicious. \$\endgroup\$ – Reinstate Monica iamnotmaynard Sep 25 '15 at 15:12

267 Answers 267

2
\$\begingroup\$

XQuery 3, 172 bytes

declare option output:method "text";string-join(for$x in 1 to 100 return if($x mod 15=0)then"FizzBuzz"else if($x mod 3=0)then"Fizz"else if($x mod 5=0)then"Buzz"else $x,"
")

There were only 7 XQuery answers on the whole site, I thought it could at least have its FizzBuzz ! Granted it's not very golfy, in particular when you need to add a 36 bytes preface so that it does not output an XML header.

I tested it with Saxon-HE's command-line XQuery tool (java net.sf.saxon.Query fizzbuzz.xq), with which I had to replace the w3-defined option declaration with declare option saxon:output "method=text";.

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

Javascript, 191 bytes

for(var i=1; i<=100; i++){
  var r = "";
  if( i%15 == 0 ? r = "FizzBuzz" : (i%5 == 0 ? r = "Buzz" : (i%3 == 0 ? r = "Fizz" : r = i)) ){
    console.log(r);
  }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Programming Puzzles & Code Golf. Nice first answer! But please wrap your code into a code-block (indent with 4 spaces or use the button in the editor). Also you should format the header correctly, the common template for this here is ##<language>, <byte count> bytes. Also you have some unnecessary whitespaces in your code which you should remove. \$\endgroup\$ – Denker Mar 10 '16 at 9:59
  • 1
    \$\begingroup\$ Nice answer, but the goal of this challenge is to make your answer as short as possible. You can save a ton of bytes by removing all whitespace, and renaming result to r. \$\endgroup\$ – ETHproductions Mar 11 '16 at 20:32
2
\$\begingroup\$

Tcl, 136 bytes

set f {Fizz 3 Buzz 5}
while {[incr n]<=100} {set s ""
foreach {m d} $f {if {$n%$d==0} {append s $m}}
if {$s eq ""} {puts $n} {puts $s}}

This solution, incidentally, is easily extensible to any combination of multiples. See The Smart Person's Mirage golf, where gnibbler posted the same idea (but in Python).

set iterations 100

set fizzies {
  Fizz 3
  Jazz 4
  Buzz 5
}

while {[incr n] <= $iterations} {
  set s ""
  foreach {name divisor} $fizzies {
    if {$n % $divisor == 0} {append s $name}
  }
  if {$s eq ""} {puts $n} {puts $s}
}
\$\endgroup\$
2
\$\begingroup\$

Oration, 98 bytes

literally, for i in range(1,100):x=""if i%3 else'Fizz';x+=""if i%5 else "Buzz";print x if x else i
\$\endgroup\$
2
\$\begingroup\$

ROOP, 187 bytes

1
V!         !<
(102)      1|
 e#r3##r5# a|
#H  #   # Y-<
   N   N  !
"Fizz""Buzz"

   mX  mX
### V-->  !
   A V---->
    #
  ' C
 V'e  "\n"
 |# M  #
 <V# #v
 C  A  C
  #X ##
    w
    O#

I will try to explain each section of code:

V!         !<  Add 1 to each number that goes to the left of the a
           1|  and sends it to the bottom of the V
           a|
           -<

(102)          The 102 falls to the left of the e and each number
 e             that passes over is compared to 102.
#H             If a number is equal then the H runs and ends the program

  #r3#         With each number that goes above the r
    #          the remainder of dividing by 3 is obtained.
   N           The N returns 1 if the number is 0, and 0 otherwise.
"Fizz"         The string "Fizz" falls and moves to the left of the m.
               The number is multiplied by the string
   mX          ("" or "Fizz" if it is 0 or 1 respectively)
###            The X removes the number when it moves to the right

               The same is done with 5 and "Buzz"

   mX  mX      
    V-->      Both strings are concatenated with the A
   A          getting "", "Fizz", "Buzz" or "FizzBuzz"
    #

  ' C         The C changes the direction of advance of string, to the left.
  'e          At the same time the "e" compares the string with the empty string.
  #           The single quotes are a vertical literal string.

 V            The V and pipes redirects the string to the right of the C
 |            that changes the direction again in order that comes to the left of the A
 <V
 C  A

          Y   The original number is converted to a string with Y.
          !



          !   Pipes and teleporters (!) Redirects the string to below the V
     V---->

    M         The string falls to the right of the M and multiplies
   # #        with the number previously obtained by the e
    A         The result is above the A

      "\n"   The string "\n" falls on the v which makes a copy
    M  #     whenever there is a space below.
      v      The C changes the direction of the string so it goes to the left.
    A  C     It waits to the right of the A

    A        The A concatenate all 3 strings, the result is on the w that sends it
  #X ##      to the O representing the output. At the same time the X deletes the string.
    w
    O#

I hope it is comprehensible, English is not my main language.

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

Nim, 100 76 73 bytes

for i in 1..100:echo max(["Fizz","",""][i%%3]&["Buzz",""][ord i%%5>0],$i)

Hm... still trying to learn Nim, and I'm thinking there's got to be a better way...

\$\endgroup\$
  • \$\begingroup\$ ["Fizz",""][i*i%%3] saves 1. Using ord on a boolean is a useful tip, thanks for that. \$\endgroup\$ – primo Jun 19 at 10:02
2
\$\begingroup\$

Befunge, 65 bytes

_1+::3%^>55+,:"c"`#@
>"zuB"vv.#,,:,,<
|!:%5\ _:!"ziF"^
<,,:,,<:|*

Try it online!

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

Javascript, 56 bytes

for(f=0;f++<100;alert(f%5?b||f:b+'Buzz'))b=f%3?'':'Fizz'

Assuming 100 alerts is an acceptable output method.

\$\endgroup\$
  • \$\begingroup\$ Nice answer, but it's already been posted (That one originally used alert as well, but it was changed to console.log because all the other JS answers do the same) \$\endgroup\$ – ETHproductions Mar 22 '17 at 21:46
2
\$\begingroup\$

Go, 162 158 145 143 142 139 bytes

package main;import."fmt";func main(){for i,p:=1,Println;i<101;i++{s:="";if i%3<1{s+="Fizz"};if i%5<1{s+="Buzz"};if s!=""{p(s)}else{p(i)}}}

Go Playground Link

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

Cardinal 217 bytes

%x>+ v  >+++++~M! 8# "buzz"
0    #~0#+++~>M! # V  "fizz"
+ ^jM<  ~        V>#
+       V      > #xV
= 0              V
t ~             >#x
t t       v      V
* =   > #}#}     /
> ^              >}/
                   .

Try it Online

Explanation

%x
0
+
+
= 0
t ~
t t
* =
> ^

Initializes a pointer with an inactive value of 100 and active value of 0

>+ v
   #
^jM<

Loops around, incrementing the active value of the pointer and sending out a duplicate until the active value is equal to the inactive value (100)

  >+++++~M!
~0#+++~>M!

Checks if the value mod 3 or mod 5 is equal to 0

                   # "buzz"
        #        # V  "fizz"
        ~        V>#
        V      > #xV
                 V
                >#x
          v      V
      > #}#}     /
                 >}/
                   .

Uses reflectors and splitters in order to print only numbers that are not divisible by 3 or 5. Printing fizz when divisible by 3 and buzz when divisible by 5.

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

Julia, 70 bytes

for i=1:100 println("$(i%3<1?"Fizz":"")$(i%5<1?"Buzz":i%3>0?i:"")")end

Julia has perl-like string formatting. Very nice for challenges like these.

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

Swift, 97 bytes

for i in 1...100{i%15==0 ?print("FizzBuzz"):i%3==0 ?print("Fizz"):i%5==0 ?print("Buzz"):print(i)}
\$\endgroup\$
2
\$\begingroup\$

q/kdb+, 58 56 49 bytes

Solution:

0{$[sum i:0=y mod 3 5;`Fizz`Buzz(&)i;y]}'1+(!)100

Example:

q)0{$[sum i:0=y mod 3 5;`Fizz`Buzz(&)i;y]}'1+(!)100
1
2
,`Fizz
4
,`Buzz
,`Fizz
7
8
,`Fizz
,`Buzz
11
,`Fizz
13
14
`Fizz`Buzz
16
...etc

Explanation:

0{$[sum i:0=y mod 3 5;`Fizz`Buzz where i;y]}'1+til 100  / ungolfed
 {                                         }'           / anonymous function that takes each-left and each-right
0                                                       / this would be parameter 'x' but we dont use it
                                               til 100  / til generates a list of 0..99
                                             1+         / adds 1 to every item in the list, thus 1..100
  $[                 ;                  ; ]             / switch, $[condition;true;false]
            y mod 3 5                                   / modulo operation on input for 3 and 5, mod[1;3 5] = 1 1
          0=                                            / is 0 equal to this result (basically a 'not' operation)
        i:                                              / save in i for later
    sum                                                 / add these, will get 0, 1 or 2. 0 is interpretted as false
                                 where i                / where gives indices where i is true
                      `Fizz`Buzz                        / 2 item list which gets indexed into (and implicitly returned)
                                     y                  / return the input if the condition was false

Notes:

This ^^ is pretty much a q version of the k solution, so I've written in a different way.. unfortunately it's about 50% slower :(

0{(`Fizz;`Buzz;y)(&)(0=a),all a:mod[y;3 5]}'1+(!)100

Here we are indexing into a list of Fizz, Buzz, based on the result of the modulo operation... The k solution style is better.

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

groovy, 55 bytes

Borrows idea from answer by @feersum https://codegolf.stackexchange.com/a/58623

100.times{println'Fizz'*(it%3/2)+'Buzz'*(it%5/4)?:it+1}
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! :) \$\endgroup\$ – Beta Decay Sep 24 '17 at 7:21
2
\$\begingroup\$

tinylisp repl, 130 bytes

(d f(q((n # %)(i(e n 100)(q Buzz)(i(disp(i #(i % n(q Buzz))(i %(q Fizz)(q FizzBuzz))))0(f(a n 1)(i #(s # 1)2)(i %(s % 1)4
(f 1 2 4

Try it online! (Note that the repl auto-completes parentheses at the ends of lines; these have been filled in on the TIO version.)

Explanation

It took me a while to realize I could do FizzBuzz in tinylisp, since the language doesn't have strings. However, it does have the Name type, which can be any run of non-whitespace, non-parenthesis characters; and it also has the disp command, which outputs a value followed by a newline. Together with some creative abuse of recursion, we can get the desired output.

We define a function f with three arguments:

  • n is the counter. We will start at 1 and recurse until 100.
  • # is the number of iterations till the next divisible-by-3 number. (Mnemonic: Shift-3)
  • % is the number of iterations till the next divisible-by-5 number. (Mnemonic: Shift-5)

The first thing f does is check whether n is 100. If so, we return Buzz. This value gets passed all the way up the call stack and printed at the end.

If not, we need to use disp to print either the number, Fizz, Buzz, or FizzBuzz, depending on the values of # and %:

  • If # and % are both nonzero (truthy), the number is not divisible by 3 or 5. Display the number, n.
  • If # is nonzero but % is zero, the number is divisible by 5 but not 3. Display Buzz.
  • If # is zero but % is nonzero, the number is divisible by 3 but not 5. Display Fizz.
  • If # and % are both zero, the number is divisible by 3 and 5. Display FizzBuzz.

Since tinylisp does not have an equivalent to Common Lisp's progn, disp'ing something and then returning a value requires a little trickery. Since disp always returns (), which is falsey, we can use the disp call as the condition of an if (i). The true branch will never be evaluated (0 is a convenient placeholder), and we can put the actual return value in the false branch.

This return value is a recursive call. We add 1 to n, and we subtract 1 from # and % unless they are zero, in which case we reset them to (3-1) or (5-1), respectively.

Ungolfed, using the standard library for long names of builtins (TIO):

(load library)

(def fizz-buzz
 (lambda (counter steps-till-fizz steps-till-buzz)
  (if (equal? counter 100)
   (q Buzz)
   (if
    (disp
     (if steps-till-fizz
      (if steps-till-buzz counter (q Buzz))
      (if steps-till-buzz (q Fizz) (q FizzBuzz))))
    (comment Return val of disp is always falsey, so the true branch is never executed)
    (fizz-buzz
     (add2 counter 1)
     (if steps-till-fizz (sub2 steps-till-fizz 1) 2)
     (if steps-till-buzz (sub2 steps-till-buzz 1) 4))))))

(fizz-buzz 1 2 4)
\$\endgroup\$
  • \$\begingroup\$ Ken Thompson: "Thompson:the original LISP... you know I think it's a horrible language. I really do. But, I was struck with the idea of defining very, very low level semantics,... it defines its own interpreter. It's always been a problem when you describe a language to say what constructs it recognizes and what they actually do and [Lisp] was the cleanest, simplest, most recursive, beautiful semantics of a language I've ever seen. Probably even to this day. But, unfortunately, what it describes I think is just a horrible language." tinylisp is a bit less scary IMHO! \$\endgroup\$ – roblogic Aug 28 at 9:41
2
\$\begingroup\$

SmileBASIC, 70 bytes

Printing "Fizz" and "Buzz" is easy, the slightly more difficult part is to only print the number when required. There are basically 2 ways to do this (and they end up being the same length)

1: Print the number when I isn't divisible by 3 or 5

FOR I=0TO 100A=I MOD 3B=I MOD 5?"Fizz"*!A;"Buzz"*!B;STR$(I)*(A&&B)NEXT

2: Print the number if the cursor is at column 0:

FOR I=1TO 100?"Fizz"*!(I MOD 3);"Buzz"*!(I MOD 3);
?STR$(I)*!CSRX
NEXT

In a previous version of SB, % was used for MOD, making the program shorter:

Petit Computer BASIC, 62 bytes

FOR I=0TO 100?"Fizz"*!(I%3);"Buzz"*!(I%5);
?STR$(I)*!CSRX
NEXT
\$\endgroup\$
  • 1
    \$\begingroup\$ The language name should probably be "SmileBASIC 2.x" or something. \$\endgroup\$ – snail_ Feb 6 '17 at 13:39
2
\$\begingroup\$

Excel VBA, 88 Bytes

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

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

Ungolfed

For i=1To 100                   ''  iterate from 1 to 100
    o=""                        ''  reset out var at each var
    o=IIf(i Mod 3,o,"Fizz")     ''  set the out var to `Fizz` if i Mod 3 = 0
    o=IIf(i Mod 5,o,o+"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, 97 Bytes

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

[A1:C100]=Array("=ROW()","=IF(MOD(A1,3),,""FIZZ"")&IF(MOD(A1,5),,""BUZZ"")","=IF(B1="""",A1,B1)")

Ungolfed

[A1:A100]="=ROW()"
[B:B]="=IF(MOD(A1,3),,""FIZZ"")&IF(MOD(A1,5),,""BUZZ"")"
[C:C]="=IF(B1="""",A1,B1)"

Subroutine Version, 110 Bytes

Declared Subroutine that takes no input and outputs to the VBE immediate window

Sub F
For i=1To 100
o=""
If i Mod 3=0Then o="Fizz
If i Mod 5=0Then o=o+"Buzz
Debug.?IIf(o="",i,o)
Next
End Sub
\$\endgroup\$
  • 1
    \$\begingroup\$ You can remove o="": and change your first iif to IIf(i Mod 3,"","Fizz") \$\endgroup\$ – seadoggie01 Sep 7 '18 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 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\$ – Taylor Scott Feb 26 at 21:30
2
\$\begingroup\$

KoopaScript, 250 characters

def i 1 if \%va is \%vu set a 1;setath b \%va %% 3;setath c \%va %% 5;setath e \%va %% 15;if \%vb is 0 if \%vc not 0 print Fizz;if \%vb not 0 if \%vc is 0 print Buzz;if \%ve is 0 print FizzBuzz;if \%vb not 0 if \%vc not 0 print \%va;setath a \%va + 1

Guide to reading: Don't. Either learn KoopaScript more or less entirely by looking at the code (my documentation isn't that great) or just take my word for it that it works. By the way, KoopaScript is an interpreted language I made (not specifically for this) that runs inside ActionScript 2, and doesn't actually have else statements, or... most of the stuff that makes other examples short. All functions are one line, so this was pretty easy. Here's the GitHub repo.

\$\endgroup\$
  • 1
    \$\begingroup\$ This looks like quite the painful programming language to use haha. Welcome to PPCG! :) \$\endgroup\$ – DJMcMayhem Mar 8 '18 at 19:55
  • \$\begingroup\$ Thanks! Yeah, it is a bit painful, but I can't really be bothered to add stuff like arrays. I made a pi approximator and a prime number generator (both very slow), both were quite painful because of the lack of useful functions. \$\endgroup\$ – Jhynjhiruu Rekrap Mar 8 '18 at 20:45
2
\$\begingroup\$

sed, 275 272 270 260 254 249 245 bytes

s/.*/t0u123456789/
:1
s/(tu?(.).*)/\1\n\2/
s/u(.)(t?)(.*)/\1\2u\3\1/
/9u/{s/t(.)/\1t/;s/u//;s/^/u/}
/99/!b1
s/$/\n10/
s/[0-9][05]/&Buzz/g
s/[0369]{2}/&Fizz/g
s/[147][258]/&Fizz/g
s/[258][147]/&Fizz/g
s/[0-9]+([FB])/\1/g
s/\n0/\n/g
s/[^\n]+\n//
q

Try it Online

This is a pure sed script which discards all input, if any, and then prints all the necessary output lines and quits.

Explanation

The script is divided into a sequence of three main parts: 1. Numeral generation; 2. "Fizz/Buzz/FizzBuzz" insertion; and 3. Formatting.

1. Numeral generation (lines 1 through 6)

The purpose of this part is to generate 99 lines containing the base 10 numerals corresponding to numbers 1 through 99. For that, we first set the entire pattern space to the following string:

t0u123456789

We will call the above string our state string. Next, we enter a loop in which each iteration goes like this:

  1. A newline is appended to the pattern space;
  2. A copy of the first numeral character after the "t" in the state string is appended to the pattern space;
  3. A copy of the first numeral character after the "u" in the state string is appended to the pattern space;
  4. The "u" in the state string is moved from its current position to the immediate right of the first numeral character after it or, if a "t" is already in said position, the "u" is instead moved to the immediate right of that "t";
  5. If the "u" in the state string is immediately at the right of the "9" in the state string, the "t" is moved from its current position to the immediate right of the first character after it, and the "u" is moved from its current position to the immediate left of the "0" in the state string;
  6. If there isn't a "99" anywhere in the pattern space (i.e., the loop has not finished its job of generating all the 99 lines), control goes back to line 2 (label :1) and so this enumeration of procedures is repeated from step 1; otherwise, control flow continues into the next line.

2. "Fizz/Buzz/FizzBuzz" insertion (lines 7 through 11)

The purpose of this part is to append "Fizz" immediately after each two-digit numeral in the pattern space which corresponds to a number which is a multiple of 3 but not of 5; to append "Buzz" immediately after each two-digit numeral in the pattern space which corresponds to a number which is a multiple of 5 but not of 3; and to append "FizzBuzz" after each two-digit numeral in the pattern space which corresponds to a number which is a multiple of both 3 and 5. This is how the computations go:

  1. We append a newline followed by "10" to the pattern space.
  2. Next, we search for all substrings of the pattern space formed by a digit between 0-9 on the left and either a 0 or a 5 on the right. We insert "Buzz" into the pattern space immediately after each such substring;
  3. Finally, we insert "Fizz" into the pattern space immediately after each substring which matches either of the following criteria:
    • Substrings formed by two digits which may be 0, 3, 6, or 9;
    • Substrings formed by a digit which is either 1, 4 or 7 on the left and a digit which is either 2, 5 or 8 on the right;
    • Substrings formed by a digit which is either 2, 5 or 8 on the left and a digit which is either 1, 4 or 7 on the right.

3. Formatting (lines 12 through 15)

This part is straight forward. Here we remove all of the following substrings from the pattern space:

  1. Every contiguous sequence of numeral characters on the immediate left of a "F" or a "B";
  2. All 0's on the immediate right of a newline character;
  3. All characters from the beginning of the pattern space up to and including the first newline character (remember that the state string is still there and there's a newline immediately before the first output numeral).

And then sed just prints the final contents of the pattern space and calls it quits.

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

///, 198 bytes

/%/!"
//$/
"!//#/
Fizz"
//"/Buzz//!/
Fizz
/1
2!4$7
8%11!13
14#16
17!19$22
23%26!28
29#31
32!34$37
38%41!43
44#46
47!49$52
53%56!58
59#61
62!64$67
68%71!73
74#76
77!79$82
83%86!88
89#91
92!94$97
98!"

Try it online! Try it interactively!

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

Ada (GNAT), 298 bytes

with Ada.Text_IO;use Ada.Text_IO;procedure T is begin for I in Integer range 1..100 loop if I mod 15 = 0 then Put_Line("FizzBuzz");elsif I mod 3 = 0 then Put_Line ("Fizz");elsif I mod 5 = 0 then Put_Line ("Buzz");else Put_Line (Integer'Image (I)(2 ..Integer'Image(I)'Last));end if; end loop; end T;

Try it online!

Ungolfed:

with Ada.Text_IO;use Ada.Text_IO;

procedure Test is begin
    for I in Integer range 1 .. 100 loop
        if I mod 15 = 0 then
            Put_Line ("FizzBuzz");
        elsif I mod 3 = 0 then
            Put_Line ("Fizz");
        elsif I mod 5 = 0 then
            Put_Line ("Buzz");
        else
            Put_Line (Integer'Image (I)(2 .. Integer'Image(I)'Last));
        end if;
    end loop;
end Test;

Pretty vanilla, but I didn't see an Ada solution yet. Probably because Ada might just be the worst real-world language to golf with!

\$\endgroup\$
  • \$\begingroup\$ Ada isn't bad, it's more that hardly anyone even knows about it \$\endgroup\$ – ASCII-only Apr 18 '18 at 22:06
  • \$\begingroup\$ Oh, I love Ada. It is definitely my favourite language. It just sucks for code golf. And yeah, I wish more people knew about it. :) \$\endgroup\$ – LambdaBeta Apr 18 '18 at 22:11
  • \$\begingroup\$ Well, it can't possibly be as bad as Java :P \$\endgroup\$ – ASCII-only Apr 18 '18 at 22:12
  • \$\begingroup\$ also on your profile: "am familiar with a majority of commonly used programming languages". which ones exactly? :P \$\endgroup\$ – ASCII-only Apr 18 '18 at 23:05
  • \$\begingroup\$ 207 \$\endgroup\$ – ASCII-only Apr 24 '18 at 9:56
2
\$\begingroup\$

Forth, 107 101 98 bytes

: f 101 1 do i 5 mod i 3 mod if dup if i . then else ." Fizz" then 0= if ." Buzz" then cr loop ; f

Ungolfed + close Python equivalent:

: f              \ def f():
  101 1 do       \     for i in range(1, 101):
    i 5 mod      \         a = i % 5  # Not an actual variable, pushed onto the stack
    i 3 mod      \         b = i % 3
    if           \         if b:      # b is popped
      dup        \             c = a
      if         \             if c:
        i .      \                 print(i, end='')
      then
    else         \         else:
      ." Fizz"   \             print('Fizz', end='')
    then 
    0=           \         a = (a == 0)
    if           \         if a:
      ." Buzz"   \             print('Buzz', end='')
    then
    cr           \         print()
  loop
; 
f                \ f()

Run it!

\$\endgroup\$
  • 1
    \$\begingroup\$ To Taylor Scott: sorry for deleting the edit. I thought adding another language would be more meaningful \$\endgroup\$ – Alex Apr 18 '18 at 23:25
  • 1
    \$\begingroup\$ Alex, while this is definitely a more interesting answer you should feel free to leave multiple responses to challenges. \$\endgroup\$ – Taylor Scott May 16 '18 at 14:18
2
\$\begingroup\$

Go, 130 129 134 bytes

package main;import."fmt";func main(){for i:=1;i<101;i++{o:="";if i%3<1{o+="Fizz"};if i%5<1{o+="Buzz"};if o==""{Print(i)};Println(o)}}

I wish i had ternary operators...

Edit: @Dust pointed out that I printed to stderr so my solution actually increased in size :(

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I think this prints to STDERR which he said is not allowed. \$\endgroup\$ – Dust Apr 19 '18 at 19:14
  • \$\begingroup\$ @Dust you are right, I'll have to revise \$\endgroup\$ – Kristoffer Sall-Storgaard Apr 20 '18 at 7:32
2
\$\begingroup\$

QB64, 102 94 bytes

FOR i=1TO 100
o$=MID$("Fizz",i*5MOD 15)+MID$("Buzz",i*5MOD 25)
IF""<o$THEN?o$ELSE WRITE i
NEXT

Doesn't work on actual QBasic; see below for why.

This program has one problem: QBasic/QB64 outputs to an 80x24 window, not a terminal, so the results can't be scrolled back. If you run the above code as-is, all you'll see is the lines from 78 onward. To prove that the code does 1 to 100 correctly, you can add the line SLEEP 1 right before NEXT for a 1-second delay on each iteration.

Ungolfed code and explanation

FOR i = 1 TO 100
    index = 5 * (i MOD 3)
    o$ = MID$("Fizz", index)
    index = 5 * (i MOD 5)
    o$ = o$ + MID$("Buzz", index)
    IF "" < o$ THEN
        PRINT o$
    ELSE
        WRITE i
    END IF
NEXT

On each iteration, we put the appropriate fizzes and buzzes into the string o$, check if it's empty, and output o$ or the number accordingly. The main question is how to get "Fizz" when i is divisible by 3 and "" otherwise. Here are the approaches I tried:

IF i MOD 3THEN o$=""ELSE o$="Fizz"
o$="":IF i MOD 3=0THEN o$="Fizz"
o$=MID$("Fizz",5*(i MOD 3))
o$=MID$("Fizz",i*5MOD 15)

The approach with MID$ is much shorter. This function takes 3 arguments--string, start index, and number of characters--and returns the appropriate substring. When the third argument is omitted, it takes everything from the start index to the end of the string. Here, when i is exactly divisible, the start index is 0 and we get the whole string; otherwise, it's something larger that's past the end of the string, so MID$ gives "".1

The other tricky part is printing numbers according to the spec. QBasic's PRINT command outputs positive numbers with leading spaces, which is occasionally useful but usually just annoying. The WRITE command, however, does not add a leading space--perfect for our purposes here.


1 Strings are 1-indexed in QBasic--i.e., in the string "abcd", a is at index 1 and d is at index 4. This is why I'm multiplying the mod result by 5: MID$("Fizz",4) gives "z". In actual QBasic, 0 isn't a legal index and gives Illegal function call; but in QB64, MID$("Fizz",0) happily returns the whole string instead of complaining.

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

Whitespace, 307 bytes

[S S S N
_Push_0][N
S S N
_Create_Label_LOOP][S S S T N
_Push_1][T  S S S _Add][S N
S _Duplicate][S N
S _Duplicate][S S S T   T   N
_Push_3][T  S T T   _Modulo][N
T   S T N
_Jump_to_Label_FIZZ_if_0][N
S S S N
_Create_Label_RETURN_FIZZ][S N
S _Duplicate][S N
S _Duplicate][S S S T   S T N
_Push_5][T  S T T   _Modulo][N
T   S T T   N
_Jump_to_Label_BUZZ_if_0][N
S S S S N
_Create_Label_RETURN_BUZZ][S S S T  T   S S T   S S N
_Push_100][T    S S T   _Subtract][N
T   S T T   T   N
_Jump_to_Label_EXIT_WITH_ERROR_if_0][N
S T T   T   T   T   N
_Call_Label_PRINT_INT][S S S T  S T S N
_Push_10][T N
S S _Print_as_character][N
S N
N
_Jump_to_Label_LOOP][N
S S T   
_Create_Label_FIZZ][S S S T S S S T T   S N
_Push_70][T N
S S _Print_as_character][S S S T    T   S T S S T   N
_Push_105][T    N
S S _Print_as_character][S S S T    T   T   T   S T S N
_Push_122][S N
S _Duplicate][T N
S S _Print_as_character][T  N
S S _Print_as_character][N
S N
S 
_Return_to_Label_FIZZ_RETURN][N
S S T   T   N
_Create_Label_BUZZ][S S S T S S S S T   S N
_Push_66][T N
S S _Print_as_character][S S S T    T   T   S T S T N
_Push_117][T    N
S S _Print_as_character][S S S T    T   T   T   S T S N
_Push_122][S N
S _Duplicate][T N
S S _Print_as_character][T  N
S S _Print_as_character][N
S N
S S N
_Return_to_Label_BUZZ_RETURN][N
S S T   T   T   T   N
_Create_Label_PRINT_INT][S N
S _Duplicate][S S S T   T   N
_Push_3][T  S T T   _Modulo][N
T   S S T   N
_Jump_to_Label_LOOP_if_0][S N
S _Duplicate][S S S T   S T N
_Push_5][T  S T T   _Modulo][N
T   S S T   N
_Jump_to_Label_LOOP_if_0][T N
S T _Print_as_integer][N
T   N
_Return][N
S S S T N
_Create_Label_RETURN][N
T   N
_Return]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Can definitely be golfed. If-checks are rather annoying in Whitespace, and I still have to get used to them some more.

EDIT: Fixed TIO version. Will try to golf it some more later.

General explanation in Pseudo-code:

Integer i = 0
Start LOOP
  Increase i by 1
  If i modulo 3 is 0: Call function FIZZ()
  If i modulo 5 is 0: Call function BUZZ()
  If i is 100: Stop program
  Call function PRINT_INT(i)
  Print new-line
  Go to next iteration of the LOOP

function FIZZ():
  Print "Fizz"
  Return

function BUZZ():
  Print "Buzz"
  Return

function PRINT_INT(integer i):
  If i modulo 3 is 0: Return
  If i modulo 5 is 0: Return
  Print i
  Return

Example run:

Command        Explanation                  Stack                 STDOUT    STDERR

SSSN           Push 0                       [0]
NSSN           Create Label_LOOP            [0]

SSSTN          Push 1                       [0,1]
TSSS           Add (0+1)                    [1]
SNS            Duplicate (1)                [1,1]
SNS            Duplicate (1)                [1,1,1]  
SSSTTN         Push 3                       [1,1,1,3]    
TSTT           Modulo (1%3)                 [1,1,1]
NTSTN          Jump to Label_FIZZ if 0      [1,1]
NSSSSN         Create Label_RETURN_FIZZ     [1,1]
SNS            Duplicate (1)                [1,1,1]
SNS            Duplicate (1)                [1,1,1,1]  
SSSTSTn        Push 5                       [1,1,1,1,5]
TSTT           Modulo (1%5)                 [1,1,1,1]
NTSTTN         Jump to Label_BUZZ if 0      [1,1,1]
NSSSSN         Create Label_RETURN_BUZZ     [1,1,1]
SSSTTSSTSSN    Push 100                     [1,1,1,100]
TSST           Subtract (1-100)             [1,1,-99]
NTSTTTN        Jump to Label_EXIT if 0      [1,1]
NSTTTTTN       Call Label_PRINT_INT         [1,1]
 NSSTTTTN      Create Label_PRINT_INT       [1,1]
 SNS           Duplicate (1)                [1,1,1]
 SSSTTN        Push 3                       [1,1,1,3]
 TSTT          Modulo (1%3)                 [1,1,1]
 NTSSTN        Jump to Label_RETURN if 0    [1,1]
 SNS           Duplicate (1)                [1,1,1]
 SSSTSTN       Push 5                       [1,1,1,5]
 TSTT          Modulo (1%5)                 [1,1,1]
 NTSSTN        Jump to Label_RETURN if 0    [1,1]
 TNST          Print as integer             [1]                      1
 NTN           Return                       [1]
SSSTSTSN       Push 10                      [1,10]
TNSS           Print as character           [1]                      \n
NSNN           Jump to Label_LOOP           [1]

SSSTN          Push 1                       [1,1]
TSSS           Add (1+1)                    [2]
SNS            Duplicate (2)                [2,2]
SNS            Duplicate (2)                [2,2,2]    
SSSTTN         Push 3                       [2,2,2,3]    
TSTT           Modulo (2%3)                 [2,2,2]
NTSTN          Jump to Label_FIZZ if 0      [2,2]
NSSSSN         Create Label_RETURN_FIZZ     [2,2]
SNS            Duplicate (2)                [2,2,2]
SNS            Duplicate (2)                [2,2,2,2]
SSSTSTn        Push 5                       [2,2,2,2,5]
TSTT           Modulo (2%5)                 [2,2,2,2]
NTSTTN         Jump to Label_BUZZ if 0      [2,2,2]
NSSSSN         Create Label_RETURN_BUZZ     [2,2,2]
SSSTTSSTSSN    Push 100                     [2,2,2,100]
TSST           Subtract (2-100)             [2,2,-98]
NTSTTTN        Jump to Label_EXIT if 0      [2,2]
NSTTTTTN       Call Label_PRINT_INT         [2,2]
 NSSTTTTN      Create Label_PRINT_INT       [2,2]
 SNS           Duplicate (2)                [2,2,2]
 SSSTTN        Push 3                       [2,2,2,3]
 TSTT          Modulo (2%3)                 [2,2,2]
 NTSSTN        Jump to Label_RETURN if 0    [2,2]
 SNS           Duplicate (2)                [2,2,2]
 SSSTSTN       Push 5                       [2,2,2,5]
 TSTT          Modulo (2%5)                 [2,2,2]
 NTSSTN        Jump to Label_RETURN if 0    [2,2]
 TNST          Print as integer             [2]                      2
 NTN           Return                       [2]
SSSTSTSN       Push 10                      [2,10]
TNSS           Print as character           [2]                      \n
NSNN           Jump to Label_LOOP           [2]

SSSTN          Push 1                       [2,1]
TSSS           Add (3+1)                    [3]
SNS            Duplicate (3)                [3,3]
SNS            Duplicate (3)                [3,3,3]   
SSSTTN         Push 3                       [3,3,3,3]    
TSTT           Modulo (3%3)                 [3,3,0]
NTSTN          Jump to Label_FIZZ if 0      [3,3]
NSST           Create Label_FIZZ            [3,3]
SNS            Duplicate (3)                [3,3,3]
SNS            Duplicate (3)                [3,3,3,3] 
 SSSTSSSTTSN   Push 70                      [3,3,3,3,70]
 TNSS          Print as character           [3,3,3,3]                F
 SSSTTSTSSTN   Push 105                     [3,3,3,3,122]
 TNSS          Print as character           [3,3,3,3]                i
 SSSTTTTSTSN   Push 122                     [3,3,3,3,122]
 SNS           Duplicate (122)              [3,3,3,3,122,122]    
 TNSS          Print as character           [3,3,3,3,122]            z
 TNSS          Print as character           [3,3,3,3]                z
 NSNS          Jump to Label_RETURN_FIZZ    [3,3,3,3]
NSSSSN         Create Label_RETURN_FIZZ     [3,3,3,3]
SSSTSTN        Push 5                       [3,3,3,3,5]
TSTT           Modulo (3%5)                 [3,3,3,3]
NTSTTN         Jump to Label_BUZZ if 0      [3,3,3]
NSSSSN         Create Label_RETURN_BUZZ     [3,3,3]
SSSTTSSTSSN    Push 100                     [3,3,3,100]
TSST           Subtract (3-100)             [3,3,-97]
NTSTTTN        Jump to Label_EXIT if 0      [3,3]
NSTTTTTN       Call Label_PRINT_INT         [3,3]
 NSSTTTTN      Create Label_PRINT_INT       [3,3]
 SNS           Duplicate (3)                [3,3,3]
 SSSTTN        Push 3                       [3,3,3,3]
 TSTT          Modulo (3%3)                 [3,3,0]
 NTSSTN        Jump to Label_RETURN if 0    [3,3]

  Stack contains additional leading [3, but we'll ignore it in this explanation
SSSTN          Push 1                       [3,1]
TSSS           Add (3+1)                    [4]
SNS            Duplicate (4)                [4,4]
SNS            Duplicate (4)                [4,4,4] 
SSSTTN         Push 3                       [4,4,4,3]    
TSTT           Modulo (4%3)                 [4,4,1]
NTSTN          Jump to Label_FIZZ if 0      [4,4]
NSSSSN         Create Label_RETURN_FIZZ     [4,4]
SNS            Duplicate (4)                [4,4,4]
SNS            Duplicate (4)                [4,4,4,4]   
SSSTSTN        Push 5                       [4,4,4,4,5]
TSTT           Modulo (4%5)                 [4,4,4,4]
NTSTTN         Jump to Label_BUZZ if 0      [4,4,4]
NSSSSN         Create Label_RETURN_BUZZ     [4,4,4]
SSSTTSSTSSN    Push 100                     [4,4,4,100]
TSST           Subtract (4-100)             [4,4,-96]
NTSTTTN        Jump to Label_EXIT if 0      [4,4]
NSTTTTTN       Call Label_PRINT_INT         [4,4]
 NSSTTTTN      Create Label_PRINT_INT       [4,4]
 SNS           Duplicate (4)                [4,4,4]
 SSSTTN        Push 3                       [4,4,4,3]
 TSTT          Modulo (4%3)                 [4,4,1]
 NTSSTN        Jump to Label_RETURN if 0    [4,4]
 SNS           Duplicate (4)                [4,4,4]
 SSSTSTN       Push 5                       [4,4,4,5]
 TSTT          Modulo (4%5)                 [4,4,4]
 NTSSTN        Jump to Label_RETURN if 0    [4,4]
 TNST          Print as integer             [4]                      4
 NTN           Return                       [4]
SSSTSTSN       Push 10                      [4,10]
TNSS           Print as character           [4]                      \n
NSNN           Jump to Label_LOOP           [4]

SSSTN          Push 1                       [4,1]
TSSS           Add (4+1)                    [5]
SNS            Duplicate (5)                [5,5]
SNS            Duplicate (5)                [5,5,5] 
SSSTTN         Push 3                       [5,5,5,3]    
TSTT           Modulo (5%3)                 [5,5,2]
NTSTN          Jump to Label_FIZZ if 0      [5,5]
NSSSSN         Create Label_RETURN_FIZZ     [5,5]
SNS            Duplicate (5)                [5,5,5]
SNS            Duplicate (5)                [5,5,5,5]   
SSSTSTN        Push 5                       [5,5,5,5,5]
TSTT           Modulo (5%5)                 [5,5,5,0]
NTSTTN         Jump to Label_BUZZ if 0      [5,5,5]
 NSSTTN        Create Label_BUZZ            [5,5,5]
 SSSTSSSSTSN   Push 66                      [5,5,5,66]
 TNSS          Print as character           [5,5,5]                  B
 SSSTTTSTSTN   Push 117                     [5,5,5,117]
 TNSS          Print as character           [5,5,5]                  u
 SSSTTTTSTSN   Push 122                     [5,5,5,122]
 SNS           Duplicate (122)              [5,5,5,122,122]    
 TNSS          Print as character           [5,5,5,122]              z
 TNSS          Print as character           [5,5,5]                  z
 NSNS          Jump to Label_RETURN_FIZZ    [5,5,5]
NSSSSN         Create Label_RETURN_BUZZ     [5,5,5]
SSSTTSSTSSN    Push 100                     [5,5,5,100]
TSST           Subtract (5-100)             [5,5,-95]
NTSTTTN        Jump to Label_EXIT if 0      [5,5]
NSTTTTTN       Call Label_PRINT_INT         [5,5]
 NSSTTTTN      Create Label_PRINT_INT       [5,5]
 SNS           Duplicate (5)                [5,5,5]
 SSSTTN        Push 3                       [5,5,5,3]
 TSTT          Modulo (5%3)                 [5,5,2]
 NTSSTN        Jump to Label_RETURN if 0    [5,5]
 SNS           Duplicate (5)                [5,5,5]
 SSSTSTN       Push 5                       [5,5,5,5]
 TSTT          Modulo (4%5)                 [5,5,0]
 NTSSTN        Jump to Label_RETURN if 0    [5,5]

... etc. etc.

SSSTN          Push 1                       [99,1]
TSSS           Add (99+1)                   [100]
SNS            Duplicate (100)              [100,100]
SNS            Duplicate (100)              [100,100,100] 
SSSTTN         Push 3                       [100,100,100,3]    
TSTT           Modulo (100%3)               [100,100,1]
NTSTN          Jump to Label_FIZZ if 0      [100,100]
NSSSSN         Create Label_RETURN_FIZZ     [100,100]
SNS            Duplicate (100)              [100,100,100]
SNS            Duplicate (100)              [100,100,100,100]   
SSSTSTN        Push 5                       [100,100,100,100,5]
TSTT           Modulo (100%5)               [100,100,100,0]
NTSTTN         Jump to Label_BUZZ if 0      [100,100,100]
 NSSTTN        Create Label_BUZZ            [100,100,100]
 SSSTSSSSTSN   Push 66                      [100,100,100,66]
 TNSS          Print as character           [100,100,100]            B
 SSSTTTSTSTN   Push 117                     [100,100,100,117]
 TNSS          Print as character           [100,100,100]            u
 SSSTTTTSTSN   Push 122                     [100,100,100,122]
 SNS           Duplicate (122)              [100,100,100,122,122]    
 TNSS          Print as character           [100,100,100,122]        z
 TNSS          Print as character           [100,100,100]            z
 NSNS          Jump to Label_RETURN_FIZZ    [100,100,100]
NSSSSN         Create Label_RETURN_BUZZ     [100,100,100]
SSSTTSSTSSN    Push 100                     [100,100,100,100]
TSST           Subtract (100-100)           [100,100,0]
NTSTTTN        Jump to Label_EXIT if 0      [100,100]                          error
\$\endgroup\$
2
\$\begingroup\$

Julia, 87 bytes

z(i)=(f=i .%[3,5] .==0;sum(f)>0 ? foldl(*,["Fizz","Buzz"][f]) : i)
println.(z.(1:100))

Or we could kind of cheat to drop 3 bytes and use show instead of println. Julia 83 bytes

z(i)=(f=i .%[3,5] .==0;sum(f)>0 ? foldl(*,["Fizz","Buzz"][f]) : i)
show(z.(1:100))

or a more legible FP style, poor performing, 91 bytes

F(x)=foldl(*,["Fizz","Buzz"][x .%[3,5] .==0])
N(y)=F(y)=="" ? y : F(y)
println.(N.(1:100))

Julia isn't really made for this, but I had fun thinking about this a bit :).

\$\endgroup\$
  • \$\begingroup\$ prod is better than foldr(* \$\endgroup\$ – H.PWiz Nov 23 at 23:45
  • \$\begingroup\$ Nice! I didn't think that'd work. Either way my solution is 10s of bytes away from the best Julia one :). \$\endgroup\$ – caseyk Nov 23 at 23:51
  • \$\begingroup\$ Sure. My score is here \$\endgroup\$ – H.PWiz Nov 24 at 12:25
  • \$\begingroup\$ Woah! How'd you do it? Or is that a secret? :) We have a thread that divulged into codegolf on julia discourse! \$\endgroup\$ – caseyk Nov 24 at 13:10
  • \$\begingroup\$ Sort of a secret. Although I have discussed julia golfing in a chat room on this site. \$\endgroup\$ – H.PWiz Nov 24 at 21:56
1
\$\begingroup\$

Hassium, 160 Bytes

Here's it in Hassium. Surprised there's never been a FizzBuzz challenge before. There's also some lengthier (but more interesting) FizzBuzz examples here

func main(){foreach(x in range(1,100)){if(x%15==0){println("fizzbuzz");}else if(x%3==0){println("fizz");}else if(x%5==0){println("buzz");}else println(x);}}

Run online and see expanded version here

\$\endgroup\$
  • \$\begingroup\$ @FryAmTheEggman Duly noted and added. Thanks. \$\endgroup\$ – Jacob Misirian Sep 25 '15 at 2:19
  • 2
    \$\begingroup\$ 1. The capitalization of Fizz and Buzz is wrong. 2. <1 is shorter than ==0. 3. All curly braces ({}) can be eliminated. \$\endgroup\$ – Dennis Sep 25 '15 at 5:29
1
\$\begingroup\$

Frink, 131 bytes

This is still to be golfed, but because the docs are bare bones, it will be golfed through experimentation

for x=1 to 100
{
if x%15==0
{
println["FizzBuzz"]
} else
{
if x%3==0
{
println["Fizz"]
} else
{
if x%5==0
{
println["Buzz"]
} else 
{
println[x]
}
}
}
}
\$\endgroup\$
  • \$\begingroup\$ By my reading of the linked page only the for loop's braces are necessary. \$\endgroup\$ – Neil Sep 29 '15 at 14:56
  • \$\begingroup\$ @Neil Then you'd need then which would increase the byte count \$\endgroup\$ – Beta Decay Sep 29 '15 at 16:01
  • \$\begingroup\$ No, that's only if you want the controlled statement on the same line. \$\endgroup\$ – Neil Sep 29 '15 at 16:13
  • \$\begingroup\$ I might have misread so you might still need the braces on the outer else clauses too. \$\endgroup\$ – Neil Sep 29 '15 at 16:36
1
\$\begingroup\$

Groovy, 71 bytes

(1..100).each{i->println i%15<1?'FizzBuzz':i%5<1?'Buzz':i%3<1?'Fizz':i}
\$\endgroup\$
1
\$\begingroup\$

Swift, 77 bytes

for i in 1...100{print(i%15<1 ?"FizzBuzz":i%3<1 ?"Fizz":i%5<1 ?"Buzz":"\(i)")}
\$\endgroup\$
  • \$\begingroup\$ @RichardG.Nielsen It's considered poor etiquette to edit some else's golfed code. Suggesting it in a comment is fine, or you could post your own answer since you came up with it independently. \$\endgroup\$ – feersum Oct 27 '15 at 11:43

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.