176
\$\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 '15 at 20:47
  • 1
    \$\begingroup\$ @AShelly Only when running \$\endgroup\$
    – Beta Decay
    Sep 24 '15 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 '15 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 '15 at 0:50
  • 68
    \$\begingroup\$ A "vanilla fizzbuzz" sounds delicious. \$\endgroup\$ Sep 25 '15 at 15:12

358 Answers 358

1
8 9 10
11
12
1
\$\begingroup\$

JavaScript, 73 71 65 bytes

for(i=1;i<101;i++)console.log((i%3?"":"Fizz")+(i%5?"":"Buzz")||i)
\$\endgroup\$
7
  • \$\begingroup\$ This was my first answer, what was the problem with the format? \$\endgroup\$ Sep 25 '15 at 13:57
  • \$\begingroup\$ Generally, language and size headers should be a header. Also, your current code doesn't seem to work, but you can fix it by replacing console.info with writeln (I tested it here and it worked.) \$\endgroup\$ Sep 25 '15 at 13:59
  • \$\begingroup\$ Do I need to specify the size or only the language? writeln is not valid in JavaScript, console.info is like console.log; both are valid. \$\endgroup\$ Sep 25 '15 at 14:08
  • \$\begingroup\$ Ah, OK, didn't realize that about writeln. You have to specify both the language and the size, on the same line. Also, I would reccommend using console.log if possible, that'll save you an extra byte. \$\endgroup\$ Sep 25 '15 at 14:13
  • \$\begingroup\$ Also, welcome to PPCG! \$\endgroup\$ Sep 25 '15 at 14:13
1
\$\begingroup\$

JavaScript (Node.js), 71 bytes

for(i=0;++i<101;)console.log((l=i%3<1?"Fizz":'')+(i%5<1?"Buzz":l?"":i))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript, 127 122 81 79 bytes

for(e=0;++e<101;)console.log(e%3||e%5?e%3==0?"Fizz":e%5==0?"Buzz":e:"FizzBuzz")

Another way, based on Taylor Scott's solution, is only 78 72 bytes

for(e=0;++e<101;)s=e%3?"":"Fizz",s+=e%5?"":"Buzz",console.log(""==s?e:s)

Try it online! - First Solution

Try it online! - Second Solution

\$\endgroup\$
3
  • \$\begingroup\$ Thanks @JoKing I'm still getting back into OOP from procedural (college class) \$\endgroup\$ Mar 8 '19 at 1:11
  • \$\begingroup\$ Thanks @taylor-scott \$\endgroup\$ Mar 8 '19 at 1:58
  • 2
    \$\begingroup\$ Stuff in the format a==0?b:c can be a?c:b \$\endgroup\$
    – Jo King
    Mar 8 '19 at 2:30
1
\$\begingroup\$

C++ 123 122 bytes -1 thanks to JonathanFrech

#import<iostream>
#define s std::cout<<
main(){for(int i;100-i++;s(i%3?"":"Fizz")<<(i%5?"":"Buzz")<<'\n')i%3*i%5?s i:s"";}
\$\endgroup\$
2
  • \$\begingroup\$ You can move the declaration of i inside the loop. Furthermore, is this variables initial value guaranteed to be zero? \$\endgroup\$ Mar 22 '19 at 14:36
  • \$\begingroup\$ @JonathanFrech I read somewhere that global ints are automatically initialized to 0 \$\endgroup\$
    – Yoris
    Mar 22 '19 at 20:53
1
\$\begingroup\$

Racket, 107 Bytes

(for{[x 100]}(define(f a b n)(if(=(modulo(+ 1 x)n)0)a b))(printf"~a~a\n"(f'Fizz""3)(f'Buzz(f""(+ 1 x)3)5)))

Try it online!

The inspiration for my answer comes from Luca H's post.

All I did afterwards was factor out the repetitive calls to (if (modulo ...) ...). Sadly, there are two copies of (+ 1 x) left that I couldn't factor out using less characters, so they remain.

Ungolfed (including removing f):

(for {[x 100]}
  (printf "~a~a\n"
          (if (= (modulo (+ x 1) 3) 0) 'Fizz "")
          (if (= (modulo (+ x 1) 5) 0) 'Buzz
              (if (= (modulo (+ x 1) 3) 0) "" (+ x 1)))))
\$\endgroup\$
1
\$\begingroup\$

33, 60 bytes

1asz'Fizz'{tlot}t[3rznpn1cztsl5rz"Buzz"npn1tlaz''nqtl1aztsi]

Explanation:

1asz          (Initialise the counter with 1)
'Fizz'{tlot}t (Create function 'Fizz' to print current number)
[             (Start of loop)
3rz           (Check if divisible by 3)
np            (If so, print 'Fizz')
n1czts        (Store result for later)
l             (Load the counter back)
5rz           (Check if divisible by 5)
"Buzz"np      (If so, print 'Buzz')
n1tlaz        (Check if divisible by neither by retrieving the value from earlier)
''nqt         (If neither 'Fizz' nor 'Buzz' was printed, print the number)
l1azts        (Restore our counter and increment it)
i]            (Print a newline and repeat from the start of the loop)
\$\endgroup\$
1
\$\begingroup\$

Julia, 87 Bytes

for i in 1:100;println(i%3<1 ? "Fizz"*(i%5<1 ? "Buzz" : "") : (i%5<1 ? "Buzz" : i));end

More readable:

for i in 1:100
    if i%3 < 1
        print("Fizz")
        if i%5 < 1
            println("Buzz")
        else
            println()
        end
    elseif i%5 < 1
        println("Buzz")
    else
        println(i)
    end
end

Not sure if there's already a Julia submission here. Also my first time golfing c:

Indirect thanks to @Najkin because I saw their reply to a C# submission saying that you can use <1 instead of ==0 and that helped me save 3 bytes over my previous 90-byte solution!

BTW if you see any "unnecessary" spaces, they're actually necessary as Julia would throw a syntax error without them. (Why?!)

\$\endgroup\$
1
  • \$\begingroup\$ oh I just realized there's a 72-byte Julia submission lol \$\endgroup\$ Aug 28 '19 at 14:02
1
\$\begingroup\$

Hoon, 115 bytes

This is a Hoon say generator which can be used to write the output to a file from the dojo.

turn is Hoon's map, it takes a list from 1..100 generated via (gulf 1 100) and uses string interpolation and the 'null check' conditional rune ?~ to generate either "Fizz", "Buzz" or "FizzBuzz" according to the value of x. This is paired with the value of x as a string <x> and then - and + are used to select the fizzbuzz string if not empty, otherwise the number string from the pair.

:-
%say
|=
*
:-
%txt
%+
turn
(gulf 1 100)
|=
x=@
=>
"{?~((mod x 3) "Fizz" ~)}{?~((mod x 5) "Buzz" ~)}"^<x>
?~
-
+
-
\$\endgroup\$
1
\$\begingroup\$

Wren, 78 bytes

It turns out that this is pretty hard to golf.

for(i in 1..100)System.print(i%15==0?"FizzBuzz":i%5==0?"Buzz":i%3==0?"Fizz":i)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Scala, 65 64 Bytes

1.to(100).map(n=>{print(s"\n$n"+(if(n%3==0)"fizz")+(if(n%5==0)"buzz"))})

I've just started golfing so any tips are appreciated (yes I have read this)

\$\endgroup\$
2
  • \$\begingroup\$ Not exactly. You have to output n only for those which are neither “fizz” or “buzz”. \$\endgroup\$
    – manatwork
    Dec 11 '19 at 18:36
  • \$\begingroup\$ @manatwork Okay good to know will fix \$\endgroup\$
    – gregam3
    Dec 11 '19 at 18:54
1
\$\begingroup\$

Visual Basic Script, 123 bytes

For i=1To 100
S=""
If i Mod 3=0Then
S="Fizz"
End If
If i Mod 5=0Then
S=S+"Buzz"
End If
If S=""Then
S=i
End If
MsgBox S
Next

Sorry @Taylor Scott but I basically copied your MY-BASIC solution

\$\endgroup\$
1
\$\begingroup\$

Symja, 140 134 128 bytes

For(i=1,i<101,i++,If(Mod(i,5)==0,s=s<>"Fizz");If(Mod(i,3)==0,s=s<>"Buzz");If(Mod(i,5)*Mod(i,3)!=0,s=s<>ToString(i));s=s<>"\n");s

Y'all can try it online here

Just a standard fizzbuzz approach. This is tweetable BTW, so there's that too.

\$\endgroup\$
1
  • \$\begingroup\$ 123 bytes (not sure how to save a link in that Symja compiler). What is golfed: both ==0 are <1; the !=0 is >0 and the For(i=1,i<101,i++, is For(i=0,++i<101,. PS: You may want to add a s=""; in your linked version, since I received an error because s was overflowing at first. \$\endgroup\$ Mar 24 '20 at 16:34
1
\$\begingroup\$

Erlang (escript), 166 bytes

i(A,B,C,D)->if A==B->C;1<2->D end.
f(X)->i(X rem 3,0,"Fizz","")++i(X rem 5,0,"Buzz","").
z(0)->"";z(X)->z(X-1)++"~n"++i(f(X),"",integer_to_list(X),f(X)).
z()->z(100).

Try it online!

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 37 bytes

т>GN3ÖUi"Fizz"?}N5ÖVi”ÒÖ”?}XY~_iN?}¶?

Try it online!

т>GN3ÖUi"Fizz"?}N5ÖVi”ÒÖ”?}XY~_iN?}¶?  # full program
  G                                    # for N in [1, 2, ...,
т                                      # ..., 99...
 >                                     # plus 1]...
       i                               # if...
   N                                   # variable...
     Ö                                 # is divisible by...
    3                                  # literal...
              ?                        # then output without trailing newline...
        "Fizz"                         # literal
               }                       # end if
                    i                  # if...
                N                      # variable...
                  Ö                    # is divisible by...
                 5                     # literal...
                         ?             # then output without trailing newline...
                     ”ÒÖ”              # "Buzz"
                          }            # end if
                               i       # if...
      U            V       XY          # N is...
                              _        # not...
      U            V       XY          # divisible by...
                             ~         # either...
      U                    X           # 3...
                             ~         # or...
                   V        Y          # 5...
                                 ?     # then output without trailing newline...
                                N      # variable
                                  }    # end if
                                    ?  # output without trailing newline...
                                   ¶   # newline
                                       # implicit end loop

Alternatively, you can replace ¶? with õ,: Try it online!

                                    ,  # output with trailing newline...
                                   õ   # empty string
\$\endgroup\$
1
\$\begingroup\$

FALSE, 86 bytes

[\100*\/$100$@\/*-0=]m:1[$100>~][0\$3m;![\%1\"Fizz"]?$5m;![\%1\"Buzz"]?\0=[$.]?10,1+]#

Try it online!

Explanation

[\100*\/$100$@\/*-0=]m: Define Modulo Function
    [\100*\/ Divide number * 100 by the divisor to get decimals
    $100$@/* Get truncated result
    -0= Subtract to get decimal, if equal to 0, then the modulo is zero and return true. If not, return false as we don't care about the result
1 Put counter on stack
[$100>~] Check if number is greater than 100
[0\$3m;![\%1\"Fizz"]?$5m;![\%1\"Buzz"]?\0=[$.]?10,1+]# Do FizzBuzz if not
    [0\ Number to see if both % 3 and % 5 failed
    $3m;! Number % 3
    [\%1\"Fizz"]? If yes, do what is in brackets
        [\%1\ Set number to 1
        "Fizz"]? Print Fizz
    $5m;! Number % 5
    [\%1\"Buzz"]? If yes, do what is in brackets
        [\%1\ Set number to 1
        "Buzz"]? Print Buzz
    \0= Check if both modulo functions failed
    [$.]? If yes, do what is in brackets
        [$ Duplicate our counter so it doesn't get destroyed
        .]? Print the counter
    10, Print ASCII character 10 (line break)
    1+]# Increment counter

Could most likely be way better.

\$\endgroup\$
1
\$\begingroup\$

Canvas, 31 bytes

A2^{w3%0≡Fizz×y5%0≡Buzz×+:?O]yO

Try it here!

Okay seriously guys why is it that both ascii-art focused languages didn't already have a FizzBuzz? This uses my favourite FizzBuzz approach found in the Vyxal answer.

\$\endgroup\$
1
\$\begingroup\$

BRASCA, 81 bytes

1b1[x0aB:b3%0=[x`zziF`oooo1a0]xB:b5%0=[x`zzuB`oooo0A1+a]xA0=[xB:bn0]xB1+:bH1+<lo]

Try it online!

There'll be an explanation coming soon. The tio link is the interpreter split over the header and footer in such a way that the program can be entered into the code box.

\$\endgroup\$
1
\$\begingroup\$

Mouse-83/Mouse-2002, 73 bytes

1F:(F.Z4*1+<^0A:F.3\0=["Fizz"1A:]F.5\0=["Buzz"A.1+A:]A.0=[F.!]"!"F.1+F:)$

Try it online! Yet another fizzbuzz. Polyglots both versions listed and beats the current answer. The funny thing is that I came up with this independently of the other answer and managed to use the same approach.

\$\endgroup\$
1
\$\begingroup\$

Javascript, 99 bytes

This isn't a good, or small solution, but it works:

for(let i=1;i<101;i++){console.log((i%3==0&&i%5==0)?"FizzBuzz":(i%3==0)?"Fizz":(i%5==0)?"Buzz":i);}

Explanation:

for (let i=1;i<101;i++) // Loop through, until reaching 100
{
    console.log(
    (i%3==0 // If I is a multiple of 3
    &&      // And
    i%5==0  // If I is a multiple of 5
    )?"FizzBuzz": // Print "FizzBuzzz"
    (i%3==0)?"Fizz":  // If I isn't either of those, check whether I is a multiple of 3, if so, print "Fizz"
    (i%5==0)?"Buzz" // If I isn't a multiple of 3, check whether I is a multiple of 5, if so, print "Buzz"
    :i // Else, just print i
    );
}
\$\endgroup\$
1
\$\begingroup\$

Phooey, 53 bytes

[100+1>&<@@%3{"Fizz">&1<}&%5{"Buzz">&1<}&>{<$i>}"
"<]

Try it online!

Outgolfed the creator at his own language. 😏


[100            while cell is not 100
   +1             increment cell
   >&<            set fizzed flag - empty stack is zero
   @@             push two copies to the stack
   %3             set cell to cell mod 3
   {              if cell is not zero
     "Fizz"         print fizz
     >&1<           set fizzed flag
   }              endif
   &              pop original value from stack
   %5{"Buzz">&1<} repeat for buzz
   &              pop again
   >{             if fizzed flag is zero 
     <$i>           print number
   }              endif 
   "\n"          print newline
   <              return to number cell
]               end

I may make fun of the interpreter, but it is a pretty nice language.

\$\endgroup\$
1
\$\begingroup\$

Myddin, 157 bytes

use std
var p=std.put
const main={
for var i=1;i<101;i++
match(i%3,i%5)
|(0,0):p("FizzBuzz\n")
|(0,_):p("Fizz\n")
|(_,0):p("Buzz\n")
|(_,_):p("{}\n",i)
;;;;}
\$\endgroup\$
1
\$\begingroup\$

ThumbGolf, 49 bytes

Machine code (little endian pairs):

2100 3101 0008 dec0 d003 b908 a303 de03
004a de6a b922 a305 de03 e003 6946 7a7a
b100 de21 de3a 2964 d1eb 4770 7542 7a7a
00

Commented assembly:

        // Include the ThumbGolf wrapper macros
        .include "thumbgolf.inc"
        .globl main
        .thumb_func
main:
        // Start the counter with zero and increment first.
        // It aligns better than incrementing at the bottom.
        movs    r1, #0
.Lloop:
        adds    r1, #1
.Lthree:
        // r0 = r1 % 3
        movs    r0, r1
        umodi   r0, 3   // udf #0300; .short 0xd003
        // if r0 == 0
        cbnz    r0, .Lten
        // if so print "Fizz"
        adr     r3, .Lfizz
        puts    r3      // udf #0003
.Lten:
        // do the same for Buzz
        // r2 = (r1 * 2) % 10, a.k.a. r1 % 5
        // we do this because umod.10 is a special narrow instruction :)
        lsls    r2, r1, #1
        umod.10 r2      // udf #0152
        cbnz    r2, .Lnum
        adr     r3, .Lbuzz
        puts    r3      // udf #0003
        // jump to the newline
        b       .Lnoprint

        // Normally, "Fizz" and "Buzz" are 5 byte strings due to the null
        // terminator, which SUCKS. This is because Thumb instructions MUST
        // be 2 byte aligned, and pool loads that are not 4 byte aligned are
        // annoying wide instructions.
        //
        // However, I have a trick up my sleeve.
        //
        // CBZ is encoded as so:
        //    |15 14 13 12|11|10| 9| 8| 7  6  5  4  3 |2  1  0|
        //    | 1  0  1  1| 0| 0| i| 1|      imm5     |  Rn   |
        // Let's focus on bits 0-7, which, since ARM is little endian, appear
        // first.
        //
        // imm5 is the offset in opcodes relative to 4 bytes after the current
        // instruction (a.k.a. where the PC is for dumb reasons). Technically,
        // there is a 6th bit in bit 9, but that is only for larger offsets.
        //
        // .Lnoprint is exactly 4 bytes after the instruction, so the offset
        // is zero:
        //   0 0 0 0 0 r r r
        // Now, for the register argument, if we set it to r0...
        //   0 0 0 0 0 0 0 0
        // Behold. A free null terminator.
        //
        // This is great, as it not only lets us cheat a byte in Fizz, but also,
        // it makes everything align perfectly so we don't need any wide adr
        // instructions. We can just put Buzz at the bottom of the file and it
        // will be 4 byte aligned as well.
.Lfizz:
        .ascii  "Fizz"  // null terminated by cbz encoding
.Lnum:
        // was r1 % 3 not zero (meaning did we not print Fizz?)
        cbz     r0, .Lnoprint
        // if so, print r1 as an integer
        puti    r1      // udf #0041
.Lnoprint:
        // putspc is a narrow instruction that prints one of 8 special characters,
        // one of which includes \n.
        putspc  '\n'    // udf #0072
        // loop while r1 != 100
        // replace with a register for variable length
        cmp     r1, #100
        bne     .Lloop
        // return
        bx      lr
.Lbuzz:
        .asciz  "Buzz"

My second submission in my brand new WIP ThumbGolf language, an ISA extension to Thumb-2.

It shows off both the new I/O instructions and the modulo instructions (with the special narrow encoding for 10).

That little hack by null terminating with cbz saves a lot, and it was really fun to figure out.

As mentioned in my cat program, this can be run on ARM Linux or QEMU by linking the ThumbGolf runtime. I don't have an online interpreter yet (and I doubt I could afford hosting one lul)

\$\endgroup\$
3
  • \$\begingroup\$ Actually, you should have based it on x86 machine code... So is ThumbGolf a RISC or a CISC? \$\endgroup\$
    – user99151
    Feb 5 at 8:05
  • \$\begingroup\$ ThumbGolf is what I envision to be a "RISC golfing language". I am sticking to this limitation, even if it isn't gonna win any contests. \$\endgroup\$
    – EasyasPi
    Feb 5 at 11:19
  • \$\begingroup\$ As I personally find golfing languages that let you answer a complex challenge in one byte ruin the whole point of golfing. 🤷‍♂️ \$\endgroup\$
    – EasyasPi
    Feb 5 at 11:26
1
\$\begingroup\$

JavaScript: 76 bytes

for(i=1;i<101;i++)console.log(i%3<1?'fizz'+(i%5<1?'buzz':''):i%5<1?'buzz':i)

Not the best solution we have, but not the worst either... The "nested" ternary operator adds either buzz or nothing to the fizz part depending on the results of the divisibility test.

\$\endgroup\$
1
\$\begingroup\$

///, 235 bytes

/(/\/\///%/111()/\\\\(p/1\/(x/%%%1(t/xxxxxxxxxx(>p^=)=^!)=$)vvt&@
^^!)=^=)=^^$v)v^|^^v)v1^v)av^^v)av^v)v^>(^/\(/>(>t/%&/&%(1&/1(%11@/@%11(1@/1(&p&(@p@(&/Fizz(@/Buzz(|p001(|(/0x/1\0(0p_1(0_/_(1p2(22/4(42/6(44/8(2p3(4p5(6p7(8p9(_(/=\=(==

Try it online!

There is already a /// answer, but it is pretty boring. I thought I would try to make a more algorithmic one, even though it is a little longer ;)

Ungolfed: Try it online!

\$\endgroup\$
1
\$\begingroup\$

CSASM v2.1.2.1, 325 bytes

func main:
push 1
pop $a
.lbl a
clf.o
push $a
push 15
rem
push 0
comp
push $f.o
brtrue e
push $a
push 3
rem
push 0
comp
push $f.o
brtrue b
push $a
push 5
rem
push 0
comp
push $f.o
brtrue d
push $a
print.n
br c
.lbl b
push "Fizz"
print.n
br c
.lbl d
push "Buzz"
print.n
br c
.lbl e
push "FizzBuzz"
print.n
.lbl c
clf.o
inc $a
push $a
push 101
comp
push $f.o
brfalse a
ret
end

Commented and Ungolfed:

func main:
    ; Initilize the counter in the accumulator
    push 1
    pop $a

    .lbl loop
        ; Reset the Comparison flag
        clf.o

        ; $a % 15 == 0
        push $a
        push 15
        rem
        push 0
        comp

        ; Jump to label "printFizzBuzz" if the Comparison flag is true
        push $f.o
        brtrue printFizzBuzz

        ; $a % 3 == 0
        push $a
        push 3
        rem
        push 0
        comp

        ; Jump to label "printFizz" if the Comparison flag is true
        push $f.o
        brtrue printFizz

        ; $a % 5 == 0
        push $a
        push 5
        rem
        push 0
        comp

        ; Jump to label "printBuzz" if the Comparison flag is true
        push $f.o
        brtrue printBuzz

        ; None of the above were true.  Just print the number itself
        push $a
        print.n
        br checkCounter

    .lbl printFizz
        ; Print "Fizz"
        push "Fizz"
        print.n
        br checkCounter

    .lbl printBuzz
        ; Print "Buzz"
        push "Buzz"
        print.n
        br checkCounter

    .lbl printFizzBuzz
        ; Print "FizzBuzz"
        push "FizzBuzz"
        print.n

    .lbl checkCounter
        ; Clear the Comparison flag
        clf.o

        inc $a

        ; $a == 101
        push $a
        push 101
        comp
        push $f.o

        ; Keep looping until the above is false
        brfalse loop
    ret
end
```
\$\endgroup\$
1
\$\begingroup\$

Pxem, Filename: 96 81 bytes + Content: 0 bytes = 96 81 bytes.

Thanks, Neil, for providing a workaround for -15 bytes!

  • Filename (some are escaped): \020.z.t.m\005.%\001.yXXbuzz.a.m\003.%\001.yXXfizz.a.c.c.z.m\017.-.nXX@.a.c@.z.pXX.a.s\n.o.m\001.+.ct.a
  • Content: empty.

Try it online! (with pxem.posixism)

With comments

XX.z
.a\020.zXX.z # push 16; while :; do
  .a.tXX.z # heap = pop!
  .a.m\005.%\001.yXXbuzz.aXX.z # while 1>heap%5; do push "buzz"; break; done
  .a.m\003.%\001.yXXfizz.aXX.z # while 1>heap%3; do push "fizz"; break; done
  .a.c.c.z.m\017.-.nXX@.aXX.z # if empty?; then print(heap-15); push "@"; fi
    # this is how while empty?; do something; done works:
    # ".c.c.z bla bla bla .a"
    # NOTE: Fail on pxemi dot 7z and RPxem
    # They need some patches before installing
    # Implementation needs to be document-compliant
    # PS. RPxem v0.0.7 fixed dotC 
  .a.c@.z.pXX.aXX.z # if top!="@"; then print pop all!; fi
  .a.sXX.z # pop!
  .a\n.oXX.z # print "\n"
  .a.m\001.+XX.z # push heap; add one to it
.a.ct.aXX.z # break if equal to 116
.a

Note

  • In Pxem, unlike other stack-based languages, subtraction and division does NOT rely on positions of two items; .- does push(abs(pop-pop)), .$ and .% stand for x=pop; y=pop; push(int(x>y?x/y:y/x)) and x=pop; y=pop; push(int(x>y?x%y:y%x)) respectively. Thus, when you try to do something like 13/17 or 13%17, you need to check which are greater first.
  • To avoid the specification, the program loops from 16 to 115.

Old version

1

\$\endgroup\$
2
  • 1
    \$\begingroup\$ To work around that division, could you not loop from 16 to 116, but subtract 15 before printing? \$\endgroup\$
    – Neil
    Mar 20 at 21:57
  • \$\begingroup\$ @Neil thank you! \$\endgroup\$ Mar 21 at 0:14
1
\$\begingroup\$

Stax, 21 bytes

åS╬╕▌≡º$)ö;▀├Xè¶XΔ£≈♀

Run and debug it

Not sure why recursive's solution from the tutorial hasn't been posted here yet. Probably the most minimal fizzbuzz possible in stax.

\$\endgroup\$
1
\$\begingroup\$

Excel, 82 79 Bytes

=LET(x,ROW(1:100),a,IF(MOD(x,3),"","Fizz")&IF(MOD(x,5),"","Buzz"),IF(a="",x,a))

This wasn't valid when the question was asked.

Spreadsheet

\$\endgroup\$
1
\$\begingroup\$

M4, 115 bytes

Also TIL that ifelse can have more than four arguments.

define(f,`ifelse($1,101,,`ifelse(eval($1%15),0,fizzbuzz,eval($1%5),0,buzz,eval($1%3),0,fizz,$1)
f(incr($1))')')f(1)

Try it online!


M4, 143 bytes, SUSv2-compatible.

define(f,`ifelse(`$1',101,,`g(`$1',ifelse(eval($1%3),0,fizz)`'ifelse(eval($1%5),0,buzz))
f(incr($1))')')define(g,`ifelse(len($2),0,$1,$2)')f(1)

Try it online!

I am not familiar with M4, but I tried some bests. What quotations can be removed?

With comments

dnl def f(n): return "" if n==101 else g(n,("fizz" if n%3==0 else "")+("buzz" if n%5==0 else "")+"\n"+f(n+1))
define(f,`ifelse(`$1',101,`',`g(`$1',ifelse(eval($1%3),0,fizz)`'ifelse(eval($1%5),0,buzz))
f(incr($1))')')dnl
dnl def g(n,s): return n if len(s)==0 else n
define(g,`ifelse(len($2),0,$1,$2)')dnl
f(1)
\$\endgroup\$
1
\$\begingroup\$

Pinecone, 90 bytes

i:1|i<101|i:i+1@(i%3+i%5=0?print:"FizzBuzz"|i%3=0?print:"Fizz"|i%5=0?print:"Buzz"|print:i)
\$\endgroup\$
1
8 9 10
11
12

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.