184
\$\begingroup\$

Introduction

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

Challenge

Write a program that prints the decimal numbers from 1 to 100 inclusive. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Output

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

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

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

Further Rules

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

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

  • Nothing can be printed to STDERR.

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

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

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

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

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

  • Standard loopholes are otherwise disallowed.

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

Catalogue

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

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

382 Answers 382

1
4 5
6
7 8
13
3
\$\begingroup\$

Javastack, 123 bytes

100 times context 1 add "Buzz"context 1 add 5 mod 0 equal repeat "Fizz"context 1 add 3 mod 0 equal repeat add logicor print

Try it online!

Context is everything. This is different to Wasif's Javastack answer because it uses a loop instead of a map, as well as extensive use of the context variable (totally not inspired by a certain golfing language made by a certain cg user with Flowey as his pfp).

Roughly equivalent to:

100ʁ ( n 1+ `Fizz` n 1+ 3 ḋt ¬ẋ `Buzz` n 1+ 5 ḋt ¬ẋ + ⟇, ) 

in vyxal.

You don't know how nice it was to write a Javastack answer without restrictions after 13 rounds of C&R.

\$\endgroup\$
1
  • \$\begingroup\$ I see. Don't worry, there'll be a #10... \$\endgroup\$
    – emanresu A
    Aug 8, 2021 at 19:56
3
\$\begingroup\$

Aya, 37 bytes

[100,Y""y3%0="Fizz"?y5%0="Buzz"?+:P];

I recently discovered Aya (so this code is likely not optimal), but really want to spread the word.

How it works

First, here are all the relevant operators:

[<number>,<expression>]  | performs a list comprehension
                         | by taking all numbers from 1 to <number> (inclusive)
                         | and applying the expression to that number

Y                  | copies the value at the top of the stack into y
""                 | puts an empty string on the stack
<num a> <num b> %  | pop the topmost two values, store a mod b on stack
<num a> <num b> =  | pop the topmost two values, store a equals b on stack
<any a> <any b> ?  | pop the topmost two values, if a is truthy: keep b on stack
<any a> <any b> +  | pop the topmost two values, store concatenation of a and b on stack
<any a> :P         | pop the topmost value, format a as string, perform println
;                  | pop the topmost value

The main "gimmick" here, is that + only operates on the topmost two values.
So by placing [y ""] on the stack first, any addition to the stack will push y out of the scope of +.

Conditionally adding "Fizz" and "Buzz" to the stack leaves us with 4 possible compositions when + is executed:

  • [y ""] - when y is not divisible by 3 or 5
  • [y "" "Fizz"] - when y is divisible by 3 but not 5
  • [y "" "Buzz"] - when y is not divisible by 3 but is divisible by 5
  • [y "" "Fizz" "Buzz"] - when y is divisible by 3 and 5

The list comprehension captures anything that remains on the stack as elements of the list, so we can drop all of the junk with a single ; at the end.
For the curious, this is what remains in the list:
[ 3 5 6 9 10 12 15 "" 18 20 21 24 25 27 30 "" 33 35 36 39 40 42 45 "" 48 50 51 54 55 57 60 "" 63 65 66 69 70 72 75 "" 78 80 81 84 85 87 90 "" 93 95 96 99 100 ]

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf! Nice answer. \$\endgroup\$ Feb 8 at 0:09
3
\$\begingroup\$

Crystal, 114 bytes

1.upto(100)do|n|case when n%15==0;puts "FizzBuzz"when n%5==0;puts "Buzz"when n%3==0;puts "Fizz"else puts n end end

Try it online!

How it works:

First, it tells it to repeat the code 100 times, with the iteration as n.

If n mod 15 = 0, then output FizzBuzz.
If n mod 5 = 0, then output Buzz.
If n mod 3 = 0, then output Fizz.

If none of those are true, just output the iteration, n.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ May 2 at 23:42
  • \$\begingroup\$ @RadvylfPrograms Thanks! \$\endgroup\$ May 3 at 0:21
3
\$\begingroup\$

CATHY, 420 bytes

cathYcathycathyCathYcatHYCAthycATHyCatHYCAtHycaThYCAthycATHyCatHYcaThYCAthyCaTHYCatHYcathYCAtHycAThycAThYcathYCAtHycAtHYCatHyCatHYCathYcaTHYcathyCATHycathYcathycaThYCATHycaTHycaTHyCATHycathYcathYcaTHYCATHycAtHycAtHycAtHycatHycAThycathYcatHycatHyCATHycatHycAThYcAtHycAThYcATHycAtHyCatHyCAtHyCathYcATHycAtHyCatHyCatHYcathYCAtHycAThycAThYcathYCAtHycAtHYcathYcathycathyCathYcathyCATHycathycAThYCaTHYCatHycAThYcAtHyCathYCAThY

Try it Online!

What have I done...

In normal form:

100⟑3τƒ:$5τƒ:5τJ:1$/*1$-;:⟑70C105C66C117C+++2/122C2*+*ƒ+;$⟑ƒ+;:1$/*1$-100⟑0C0*J;*+⟑,
=== Part 1: divisors ===
100⟑3τƒ:$5τƒ:5τJ:1$/*1$-;
100⟑                    ; # Map 1...100 to...
    3τƒ:                  # Convert to base 3 and get the last (dup-reduce)
        $5τƒ:             # Also convert to base 5 and get last (dup-reduce)
             5τJ          # Wrap in a list by converting to base 5 again, and concat
                :1$/      # Duplicate and take reciprocal
                    *     # Multiply, resolving 1+s to 1 and 0s to 0
                     1$-  # Subtract from 1
                          # Leaving [divisible by 3, divisible by 5]

=== Part 2: Fizz and Buzz ===
:⟑70C105C66C117C+++2/122C2*+*ƒ+;
:⟑                             ; # Duplicate previous and map to...
  70C105C66C117C+++              # Construct string "FiBu"
                   2/            # Divide into two pieces
                     122C        # chr(122) = z
                         2*+     # Double and append to each
                            *ƒ+  # Repeat by the above and concatenate
=== Part 3: Putting it together ===
$⟑ƒ+;:1$/*1$-100⟑0C0*J;*+⟑,
$⟑ƒ+;                       # Swap and sum each
     :1$/*1$-               # Turn 2s into 1s
             100⟑     ;     # Map 1...100 to...
                 0C0*J      # Concatenate the empty string (stringify)
                       *    # Repeat numbers by amounts
                        +   # Concatenate
                         ⟑, # Over each, print
\$\endgroup\$
2
\$\begingroup\$

PHP, 73 71 bytes

<?for($i=0;$i++<100;)echo$i%3?$i%5?$i:@Buzz:@Fizz.($i%5?"":@Buzz),"
";

All the most terrible things. I wanted the wrongheaded ternary to do something magical, but it did not.

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

Ceylon, 368 144 123 96 bytes

shared void z(){for(i in 1..100){print(["FizzBuzz","Buzz","Fizz",i][(i%5).sign*2+(i%3).sign]);}}

Here we have the ungolfed original of 368 bytes:

shared void fizzBuzz() {
    for(i in 1..100) {
        if(3.divides(i)){
             if(5.divides(i)) {
                 print("FizzBuzz");
             } else {
                 print("Fizz");
             }
        } else {
            if(5.divides(i)) {
                print("Buzz");
            } else {
                print(i);
            }
        }
    }
}

In Ceylon, Integers are also just objects, so one can call methods on them (like the divides method here).

1..100 is syntactic sugar for span(1, 100), which is a Range<Integer>, which implements Iterable<Integer>, and can therefore be used with the for loop.

The print function takes one argument (of type Anything), stringifies it (i.e. if it's an object, calls its .string attribute, if it's null, takes "<null>") and prints it to the standard output.

Removing whitespace, using a shorter function name, and replacing x.divides(y) by the shorter y%x==0, which is essentially how divides is implemented, gives us this (144 bytes):

shared void f(){for(i in 1..100){if(i%3==0){if(i%5==0){print("FizzBuzz");}else{print("Fizz");}}else{if(i%5==0){print("Buzz");}else{print(i);}}}}

Of course, this is not the best which is possible ... this uses print and if much too often, and also does the check for divisibility by 5 twice.

Integers (or Numbers types in general) have also the .sign attribute, which is 1 for positive numbers, 0 for zero, and -1 for negatives. We can use that together with the remainder operator to get a different value for each of the four cases: (i % 5).sign * 2 + (i % 3).sign]. This is 0 for FizzBuzz, 1 for Buzz, 2 for Fizz and 3 for the "plain" case. We can use this as an index of a tuple, coming to this 123-bytes program:

shared void z() {
    for(i in 1..100) {
        print(["FizzBuzz", "Buzz", "Fizz", i][(i%5).sign*2 + (i%3).sign]);
    }
}

([...] is the syntax for both Tuple creation (here a Tuple with element types String, String, String, Integer, formally Tuple<String|Integer, String, Tuple<String|Integer, String, Tuple<String|Integer, String, Tuple<Integer, Integer, Empty>>>, which can be written shorter as [String, String, String, Integer]) and lookup in a Correspondence (and this tuple type implements Correspondence<Integer, String|Integer>).

Removing the whitespace again gives us this 96 byte program:

shared void z(){for(i in 1..100){print(["FizzBuzz","Buzz","Fizz",i][(i%5).sign*2+(i%3).sign]);}}
\$\endgroup\$
2
\$\begingroup\$

VB.Net, 147 146 bytes

Module F
Sub Main()
For i=1To 100
Dim a=i Mod 3,b=i Mod 5
Console.WriteLine("{0:#}{1:;;Fizz}{2:;;Buzz}",If(a*b>0,i,0),a,b)
Next
End Sub
End Module

It uses the same conditional formatting trick as the C# answer by Pierre-Luc Pineault.

UPDATED: saved 1 byte thanks to Brian J

\$\endgroup\$
1
  • \$\begingroup\$ you can save a byte by using If instead of IIf. The difference is that IIf is a function that will evaluate both the true and false options, while If is an operator that will only evaluate the option it needs. Functionally doesn't make a difference in this case, but does save a character. \$\endgroup\$
    – Brian J
    Sep 25, 2015 at 13:13
2
\$\begingroup\$

Python 2, 148 133 Bytes

def f(n):
 if n%3+n%5<1:return"FizzBuzz"
 if n%5<1:return"Buzz"
 if n%3<1:return"Fizz"
 return n
for x in map(f,range(1,101)):print x
\$\endgroup\$
5
  • \$\begingroup\$ it is possible to save some bytes by reducing the amount of indentation. (1 space is sufficient) \$\endgroup\$
    – Mohammad
    Sep 27, 2015 at 18:12
  • 1
    \$\begingroup\$ @Mhmd it's probably actually tabs, SE converts them to 4 spaces. \$\endgroup\$ Sep 27, 2015 at 22:18
  • \$\begingroup\$ if n%3+n%5==0:return"FizzBuzz" -> if n%3+n%5==0:return f(3)+f(5) EDIT: nevermind i miscounted, it's the same \$\endgroup\$ Sep 27, 2015 at 22:19
  • 1
    \$\begingroup\$ oh, but you can change ==0 to <1 everywhere it appears \$\endgroup\$ Sep 27, 2015 at 22:58
  • 1
    \$\begingroup\$ This can be shortened significantly by removing the function and return logic: for i in range(100):print(i%3+i%5<1and'FizzBuzz')or(i%5<1and'Buzz')or(i%3<1and'Fizz')or i golfs it down to 89 bytes. \$\endgroup\$
    – Skyler
    Oct 26, 2015 at 13:47
2
\$\begingroup\$

C# using LINQ, 168 186

using System.Linq;class A{static void Main(){foreach(var s in Enumerable.Range(1,100).Select(n=>n%3==0?n%5==0?"FizzBuzz":"Fizz":n%5==0?"Buzz":n.ToString()))System.Console.WriteLine(s);}}
\$\endgroup\$
0
2
\$\begingroup\$

haxe, 110 bytes

class Main{
  static function main()
    for(i in 1...101)
      Sys.println(i%3<1?"Fizz"+(i%5<1?"Buzz":""):i%5<1?"Buzz":i);
}

(newlines and indents added for clarity)

Haxe isn't much of a golfing language … I was trying to do something with enumerators:

class Main{
  static function main()
    for(i in 1...101)
      Sys.println(
        switch(i){
          case _%3=>0:i%5<1?"FizzBuzz":"Fizz";
          case _%5=>0:"Buzz";
          case _:i;
        }
      );
 }

But 140 bytes. :I

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

C#, 155 142 Bytes

class a{static void Main(){for(int i=0;i++<100;){var s="";if(i%3<1)s="Fizz";if(i%5<1)s+="Buzz";if(s=="")s=i+"";System.Console.WriteLine(s);}}}

Added as an alternate approach to the example using LINQ

Thanks @Riokmij!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can replace the ==0's with <1, change the declaration of s with var instead of string, and replace the call to .ToString() by +"". Also, initial value for i should be 0. \$\endgroup\$
    – Najkin
    Sep 29, 2015 at 15:56
2
\$\begingroup\$

MSX-BASIC, 106 bytes

1FORI=1TO100:IFIMOD3=0ANDIMOD5=0THEN?"FizzBuzz"ELSEIFIMOD3=0THEN?"Fizz"ELSEIFIMOD5=0THEN?"Buzz"ELSE?I
2NEXT

The one-liner version to be executed in direct mode would be 120 bytes because all of the extra NEXTs needed before the ELSEs:

FORI=1TO100:IFIMOD3=0ANDIMOD5=0THEN?"FizzBuzz":NEXTELSEIFIMOD3=0THEN?"Fizz":NEXTELSEIFIMOD5=0THEN?"Buzz":NEXTELSE?I:NEXT
\$\endgroup\$
1
  • 1
    \$\begingroup\$ IFIMOD3=0ANDIMOD5=0 -> IFIMOD15=0? I think MSX BASIC could be tokenized too, which would decrease your byte count. \$\endgroup\$
    – lirtosiast
    Sep 30, 2015 at 14:53
2
\$\begingroup\$

rs, 92 91 bytes

(_)^^(100)
+^(_+)(_)/\1 \1\2
\b((___)+)\b/Fi;\1
\b(_{5})+\b/Bu;
;_*/zz
\b(_+)\b/(^^\1)
 /\n

Saved 1 byte thanks to @MartinBüttner!

Live demo. (It may take a bit to run!)

\$\endgroup\$
6
  • \$\begingroup\$ Do you not have \0 in rs? You can probably save some bytes either way by using the trick from my Retina answer: \b((___)+)\b/Fi;\1 ... \b(_{5})+\b/Bu; ... ;_+/zz \$\endgroup\$ Sep 28, 2015 at 14:38
  • \$\begingroup\$ @MartinBüttner Thanks! I updated the post. What exactly do you mean by \0? \$\endgroup\$ Oct 1, 2015 at 19:44
  • \$\begingroup\$ Something to reference the entire match, not just a capturing group. \$\endgroup\$ Oct 1, 2015 at 19:59
  • \$\begingroup\$ @MartinBüttner I guess not. :( \$\endgroup\$ Oct 1, 2015 at 20:05
  • \$\begingroup\$ @MartinBüttner I'm just using Python's built-in substitution thing. I can easily override it, though... \$\endgroup\$ Oct 1, 2015 at 20:06
2
\$\begingroup\$

O, 53 52 bytes

I'm sure that there will be a better way to do this. Thanks to kirbyfan64sos for the implicit J.

"Buzz"JA.*1mrl{.3%{.5%{}{;J}?}{"Fizz"\5%{}{J+}?}?p}d

Try it here

\$\endgroup\$
2
  • \$\begingroup\$ 52 bytes: "Buzz"JA.*1mrl{.3%{.5%{}{;J}?}{"Fizz"\5%{}{J+}?}?p}d. O has the implicit J variable like Pyth. \$\endgroup\$ Oct 1, 2015 at 23:13
  • \$\begingroup\$ omg someone other than my mom used O <3 \$\endgroup\$
    – jado
    Nov 8, 2015 at 4:09
2
\$\begingroup\$

Minkolang, 50 bytes

"d"[i1+d5%6&"Buzz"0c3%6&"Fizz"I1-3&N6@0gx(O)25*O].

Try it here.

Explanation

"d"[...].        For loop that loops from 0 to 99, then stops
i1+              Loop counter + 1 (so it's 1 to 100)
d5%6&"Buzz"      Divisibility test by 5, skips "Buzz" if not divisible
0c3%6&"Fizz"     Divisibility test by 3, skips "Fizz" if not divisible
I1-              Length of stack minus 1 (0 if there's no Fizz or Buzz)
3&N6@            Output as integer if ^ is 0, skip character output otherwise
0gx(O)           Dump the loop counter and output "Fizz"/"Buzz"/"FizzBuzz"
25*O             Print newline
\$\endgroup\$
2
\$\begingroup\$

Swift, 75 bytes

for n in 1...100{print(n%3*n%5>0 ?n:(n%3>0 ?"":"Fizz")+(n%5>0 ?"":"Buzz"))}
\$\endgroup\$
2
\$\begingroup\$

F#, 129 116 113 111

Seq.iter(fun x->printfn"%s"(["Fizz";"";""].[x%3]+(if x%5=0 then"Buzz"elif x%3>0 then string x else""))){1..100}
\$\endgroup\$
2
\$\begingroup\$

scg, 51 bytes

1á01°r{d[[d"Buzz"]["Fizz"d"Buzz"+]]\3%!@\5%!@"
"}m

So, does this mean that scg is a real language now? Explanation:

1                         .- adds 1 to the stack
 á01                      .- adds 101 to the stack
    °r                    .- range, adds array with 1-100 on the stack
      {                   .- start function for use in map
       d                  .- duplicates number
       [                  .- array
        [
         d                .- duplicate number again, ends up in array
          "Buzz"          .- wonder what this does
                ]         .- end array
        [
         "Fizz"
               d          .- duplicate fizz
         "Buzz"+          .- ends up with "FizzBuzz"
                ]
                 ]        .- end array. Ends up with a 2D array
        \                 .- gets number to calculate to the top
        3%                .- mod 3
          !               .- not, so any above 0 int turns to 0 and 0 turns to1
           @              .- get array value. Now you have two choices for output
            \5%!@         .- same as above but for 5.
                          .- now we have the correct fizzbuzz value
                 "\n"     .- pushes newline. I do not have variables yet so no shortcuts
                     }m   .- end function, map. output is implicit
\$\endgroup\$
2
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 32 chars / 47 bytes

⩥Ṥⓜᵖ`FizzBuzz`ė⧺_%3⅋4,_%5?4:8)⋎_

Try it here (Firefox only).

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

C#, 174 bytes

void A(){for(int x=1;x<101;x++){if(x%15<1)Console.Write("FizzBuzz\n");else if(x%3<1)Console.Write("Fizz\n");else if(x%5<1)Console.Write("Buzz\n");else Console.WriteLine(x);}}

Ungolfed:

void A(){
    for (int x = 1; x < 101; x++) {
        if (x % 15 < 1) Console.Write("FizzBuzz\n");
        else if (x % 3 < 1) Console.Write("Fizz\n");
        else if (x % 5 < 1) Console.Write("Buzz\n");
        else Console.WriteLine(x);
    }
}
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 103 86 73 bytes

With 30 bytes saved thanks to @A Simmons!

f="Fizz";b="Buzz";Range@100/.{x_/;15∣x->f<>b,x_/;3∣x->f,x_/;5∣x->b}
\$\endgroup\$
4
  • \$\begingroup\$ You can use g=Divisible instead of the function definition, there's a leading space (and another one right after f<>b,), and you can use x~g~15 instead of x~g~3&&x~g~5. So f="Fizz";b="Buzz";g=Divisible;Range@100/.{x_/;x~g~15->f<>b,x_/;x~g~3->f,x_/;x~g~5->b} \$\endgroup\$ Mar 2, 2016 at 16:28
  • \$\begingroup\$ You can use f="Fizz";b="Buzz";Range@100/.{x_/;15∣x->f<>b,x_/;3∣x->f,x_/;5∣x->b} for 73 bytes in 67 characters. \$\endgroup\$
    – A Simmons
    Mar 2, 2016 at 17:51
  • \$\begingroup\$ CatsAreFluffy, It turns out that your code gives incorrect answers for 5 and 100, for example. \$\endgroup\$
    – DavidC
    Mar 2, 2016 at 22:28
  • \$\begingroup\$ It's a pair of zero width characters right after the g. Here's code for correcting that: FromCharacterCode[ToCharacterCode@#/.{8204|8203->(##&[])}]& (Don't worry, no zero widths here.) \$\endgroup\$ Mar 2, 2016 at 22:34
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\$
2
  • 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, 2016 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\$ Mar 11, 2016 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\$
1
  • \$\begingroup\$ ["Fizz",""][i*i%%3] saves 1. Using ord on a boolean is a useful tip, thanks for that. \$\endgroup\$
    – primo
    Jun 19, 2019 at 10:02
2
\$\begingroup\$

Befunge, 65 bytes

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

Try it online!

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

Valyrio, 13 bytes

s∫main [CF]

This is a fairly basic (and slightly unimaginative) answer.

Explanation

C pushes 100 to the stack, which means that ...

F is the FizzBuzz builtin. This was mainly added in as a basic stack based program but got left in as a command and I never got rid of it.

\$\endgroup\$
0
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\$
1
4 5
6
7 8
13

Your Answer

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

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