160
\$\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\$
  • 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
  • 8
    \$\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
  • 62
    \$\begingroup\$ A "vanilla fizzbuzz" sounds delicious. \$\endgroup\$ – Reinstate Monica -- notmaynard Sep 25 '15 at 15:12

285 Answers 285

1
6 7 8 9
10
0
\$\begingroup\$

Clean, 148 + 2 = 150 bytes

+2 for the -b compiler flag

module m
import StdEnv
f[0,0,_]="FizzBuzz"
f[a,b,n]|a==0="Fizz"|b==0="Buzz"=fromInt n
Start=flatlines[fromString(f[n rem 3,n rem 5,n])\\n<-[1..100]]

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

MY-BASIC, 116 bytes

A response.

For i=1 To 100
S=""
If i Mod 3=0 Then S="Fizz"
If i Mod 5=0 Then S=S+"Buzz"
If S="" Then Print i; Else Print S;
Next

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ada (GNAT), 196 bytes

procedure GNAT.IO.F is begin for I in Integer range 1..100 loop Put((if I mod 3=0 then"Fizz"else"")&(if I mod 5=0 then"Buzz"else""));if I mod 3*I mod 5/=0 then Put(I);end if;New_Line;end loop;end;

Try it online!

189 bytes if extraneous whitespace is allowed:

procedure GNAT.IO.F is begin for I in Integer range 1..100 loop Put(if I mod 3=0 then(if I mod 5=0 then"FizzBuzz"else"Fizz")else(if I mod 5=0 then"Buzz"else I'Image));New_Line;end loop;end;

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Pyth, 39 bytes

VS100J+?q0%N3"Fizz"k?q0%N5"Buzz"k?qJkNJ

Try it online!

There's probably an easier and shorter way. Also is(polishNotation, annoying).

This is roughly equivalent to:

for x in range(1,100):
a = "" + "Fizz" if x%3==0 else "" + "Buzz" if x%5==0 else ""
print(a if a!="" else x)
| improve this answer | |
\$\endgroup\$
0
\$\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!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Java 8, 129 bytes

interface I{static void main(String[]s){for(int i=0;++i<101;)System.out.println(i%15<1?"FizzBuzz":i%3<1?"Fizz":i%5<1?"Buzz":i);}}

Try it online

| improve this answer | |
\$\endgroup\$
0
\$\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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks @JoKing I'm still getting back into OOP from procedural (college class) \$\endgroup\$ – Ryan Knutson Mar 8 '19 at 1:11
  • \$\begingroup\$ Thanks @taylor-scott \$\endgroup\$ – Ryan Knutson 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
0
\$\begingroup\$

Scheme, 118 bytes

(for-each(lambda(i)(printf"~a~a~%"(if(=(mod i 3)0)'Fizz"")(if(=(mod i 5)0)'Buzz(if(=(mod i 3)0)""i))))(cdr(iota 101)))

Try it online!

Ungolfed:

(for-each
 (lambda (i)
   (printf
    "~a~a~%"
    (if (= (mod i 3) 0) 'Fizz "")
    (if (= (mod i 5) 0) 'Buzz 
        ;; else
        (if (= (mod i 3) 0) "" i))))
 (cdr (iota 101)))
| improve this answer | |
\$\endgroup\$
0
\$\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)))))
| improve this answer | |
\$\endgroup\$
0
\$\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)
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Minefriff, 241 bytes

0,`
>I1,+:a*a1,=?;`~             o,aC            <  
>:f,%?`Ca*c2,:a*b7,7*e,a*c2,:f*7,a*7,oooooooo^
>:5,%?`Ca*c2,:a*b7,6*b,oooo                  ^
>:3,%?`Ca*c2,:f*7,a*7,oooo                   ^
>:o                                          ^ 

There isn't a TIO interpreter link for Minefriff, but you can run it online here.

What it actually looks like:

300 px version Bigger

| improve this answer | |
\$\endgroup\$
0
\$\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)

| improve this answer | |
\$\endgroup\$
  • \$\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
0
\$\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

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Stax, 27 bytes

éeO├φ☻mRàzΦ╛`φ#2àáÿ²øΔ=L←§b

Run and debug it

Unpacked and Uncompressed

A2#F~;3%z"Fizz"?;5%z"Buzz"?+cz=,a?P
| improve this answer | |
\$\endgroup\$
0
\$\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.

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Kevin Cruijssen Mar 24 at 16:34
1
6 7 8 9
10

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.