159
\$\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
4 5
6
7 8
10
2
\$\begingroup\$

sed 4.2.2, 129 bytes

A 400-rep bounty for those who outgolf this solution https://codegolf.meta.stackexchange.com/a/18428/

s/^/00,/;h
:
y/0123456789';,/1234567890;,'/
/0.$/!{x;G;s/..\n.//}
h
s/[05].$/&Buzz/
s/.*,/Fizz/
s/.*\WB/B/
s/[0';]*//gp
g;/00/d
t

Try it online!

s/^/00,/;h

Each number is stored as three characters, the first two store the two-digit padded decimal number, and the last character stores its modulo 3.

: ... t

In a loop,

y/0123456789';,/1234567890;,'/

the modulo is cycled, and the number is incremented using transliteration. Each digit is increased modulo 10, then

/0.$/!{x;G;s/..\n.//}

a simple conditional corrects the first digit on the number if necessary, using the fact each number is stored with exactly 3 characters.

h

This incremented form is stored in the hold space.

s/[05].$/&Buzz/

Buzzs are added by looking at the last base-10 digit of the number.

s/.*,/Fizz/

Fizzs are added by looking at the modulo-3.

s/.*\WB/B/

There is some cleanup of the number, remove the base-10 digits if needed and

s/[0';]*//gp

remove leading 0s and the modulo-3 so that it is print-ready, and print it.

g;/00/d

Finally retrieve the number from the hold space and exit if 00 is present, i.e. 100 has been reached

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

Rust, 114 bytes

It's not the shortest one available (on code-golf.io someone managed to somehow solve it in 99 bytes, but I have no idea how).

fn main(){(1..101).for_each(|i|println!("{}",["FizzBuzz","Fizz","Buzz",&i.to_string()][1.min(i%5)+2.min(i%3*2)]))}

First off, the obvious part

fn main() {/*..*/}

Then we iterate from 1 to 100 (inclusive) and println! an element of the array

(1..101).for_each(|i| println!("{}", ["FizzBuzz", "Fizz", "Buzz", &i.to_string()][/*..*/]))

To choose the right index, we use the following, where i is the current number

1.min(i % 5) + 2.min((i % 3) * 2)

The first part is 0 if i is a multiple of 5. Otherwise it's 1.

The second part is 0 if i is a multiple of 3. Otherwise it's 2.

Examples:

1.min(1 % 5) + 2.min((1 % 3) * 2) == 1 + 2 == 3 => "1"
1.min(3 % 5) + 2.min((3 % 3) * 2) == 1 + 0 == 1 => "Fizz"
1.min(4 % 5) + 2.min((4 % 3) * 2) == 1 + 2 == 3 => "4"
1.min(5 % 5) + 2.min((5 % 3) * 2) == 0 + 2 == 2 => "Buzz"
1.min(15 % 5) + 2.min((15 % 3) * 2) == 0 + 0 == 0 => "FizzBuzz"

This way, if both parts produce 0, we get the string FizzBuzz. If the sum is 1 we get Fizz, if it's 2 Buzz, if it's 3 we get &i.to_string() (the stringified number).

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can replace println! with print! and add a literal (as in press ENTER/RETURN) newline after the {} in the format string to save a byte. Also, if you're allowed to use a closure, it'd save quite a few bytes over using fn main(). Finally, you save some bytes using a for loop. 99 bytes: TIO \$\endgroup\$ – TehPers Aug 3 at 22:15
2
\$\begingroup\$

Javascript 78 (but kinda 58) bytes

Produces the required string, then prints it. The function that produces the string is 58 bytes, another 20 are used to call console.log.

z=n=>n?z(n-1)+((n%3?'':'Fizz')+(n%5?'':'Buzz')||n)+'\n':'';console.log(z(100))

Javascript 72 71 bytes (thanks, Jo King!)

Prints the required lines as it goes. Interweaving console.log with the logic creates a shorter, though less elegant, program.

z=n=>n&&(z(n-1),console.log((n%3?'':'Fizz')+(n%5?'':'Buzz')||n));z(100)

It can also be written like the following, to the same effect and byte count.

z=n=>n&&console.log((z(n-1),(n%3?'':'fizz')+(n%5?'':'buzz')||n));z(100) 

De-golfed version:

z = n =>           // take n as argument
  n && (           // if n is not falsey (e.g. if it is not 0)
    z(n-1),        // use the comma operator to call the previous fizzbuzz
    console.log(   // then print a line
      (n%3 ? '' : 'Fizz')   // 'Fizz' if n is a multiple of 3, otherwise empty
      + (n%5 ? '' : 'Buzz') // Concatenated with 'Buzz' if multiple of 5
      || n         // Or just the number n if the previous string is empty, which implies that n is not divisible by 3 or 5
    )
  );
z(100)             // Run fizzbuzz from 100
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can save 5 bytes (on every solution) by using alert instead of console.log \$\endgroup\$ – VFDan May 19 at 0:46
2
\$\begingroup\$

International Phonetic Esoteric Language, 61 60 bytes

{2T}1ɑeb3ⱱɐbʌɔ|a|"Fizz"u|a|q5ⱱɐbʌɔ|b|"Buzz"u|b|ɞʌue1sø"\n"uɒ

Explanation:

{2T}1                                                        (push loop bounds 1 to 2T == 101 base36)
     ɑ                                                       (start loop)
      eb                                                     (push and dup loop index)
        3ⱱɐbʌɔ|a|"Fizz"u|a|                                  (print if fizzy)
                           q                                 (OVER)
                            5ⱱɐbʌɔ|b|"Buzz"u|b|              (print if buzzy)
                                               ɞʌu           (else print the number)
                                                  e1sø       (increment loop index)
                                                      "\n"u  (print a newline)
                                                           ɒ (end loop)

-1 byte for using base 36 for 101.

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

Integral, 416 Bytes

⌡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⌡[j

Try it

Integral doesn't have very many ways of doing this challenge yet, so this'll have to do for now.

I'll update it once there's a better way.

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

| improve this answer | |
\$\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]
}
}
}
}
| improve this answer | |
\$\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}
| improve this answer | |
\$\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)")}
| improve this answer | |
\$\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
1
\$\begingroup\$

STATA, 115 bytes

qui{
set ob 100
g a="Fizz" if!mod(_n,3)
g b="Buzz" if!mod(_n,5)
g c=a+b
replace c=string(_n) if c==""
}
l c,noo noh

qui{} suppresses output for everything in that block. First set the number of observations to be 100. Then generate variable a to be "Fizz" for every observation where its index number is divisible by 3. Then generate variable b to be "Buzz" for every observation where its index number is divisible by 5. Generate variable c to be the concatenation of these two. Then, replace c with the index number (STATA uses 1 indexing) if it is still an empty string. Then list the results of c in a table without observation numbers or headers.

Only works in the "real" STATA interpreter. I need to add functions and conditions to the online interpreter for it to work there.

A different solution in 116 bytes:

forv x=1/100{
if!mod(`x',3){
di"Fizz"_c
if!mod(`x',5) di"Buzz"_c
}
else if!mod(`x',5) di"Buzz"_c
else di `x' _c
di
}

This solution goes through a for loop and checks whether the loop variable is divisible by 3 or not. If it is, it prints "Fizz". Then it checks if it is divisible by 5 and prints "Buzz". Otherwise, it checks if it is divisible by 5 and prints "Buzz". If not, it prints the loop variable.

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

zsh, 65 63 bytes

repeat 100 x=|let ++i%3||x=Fizz&&let i%5||x+=Buzz&&<<<${x:-$i}

Changed echo to <<<. It's now 2 bytes shorter, because <<< doesn't need a space.

repeat 100 x=|let ++i%3||x=Fizz&&let i%5||x+=Buzz&&echo ${x:-$i}

| improve this answer | |
\$\endgroup\$
1
\$\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
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ it is possible to save some bytes by reducing the amount of indentation. (1 space is sufficient) \$\endgroup\$ – Mhmd Sep 27 '15 at 18:12
  • 1
    \$\begingroup\$ @Mhmd it's probably actually tabs, SE converts them to 4 spaces. \$\endgroup\$ – undergroundmonorail Sep 27 '15 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\$ – undergroundmonorail Sep 27 '15 at 22:19
  • 1
    \$\begingroup\$ oh, but you can change ==0 to <1 everywhere it appears \$\endgroup\$ – undergroundmonorail Sep 27 '15 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 '15 at 13:47
1
\$\begingroup\$

UniBasic, 106 bytes

FOR I=1 TO 100;D='';IF MOD(I,3)=0 THEN D='Fizz'
IF MOD(I,5)=0 THEN D:='Buzz'
IF D='' THEN D=I
CRT D;NEXT I
| improve this answer | |
\$\endgroup\$
1
\$\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);}}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Don't you need to include using System.Linq; so that Enumerable can be referenced? \$\endgroup\$ – Ken Gregory Sep 28 '15 at 20:13
  • \$\begingroup\$ @KenGregory You're right - thank's for pointing that out. I forgot that LINQPad (where I tried it) adds this implicitly. I've edited my anwer accordingly. \$\endgroup\$ – Bill Tür Sep 28 '15 at 20:31
1
\$\begingroup\$

Windows Batch, 172

@setlocal enableDelayedExpansion&for /l %%N in (1 1 100) do @(set v=&set/a1/(%%N%%3^)||set v=Fizz&set/a1/(%%N%%5^)||set v=!v!Buzz&if defined v (echo !v!)else echo %%N)2>nul
| improve this answer | |
\$\endgroup\$
1
\$\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!

| improve this answer | |
\$\endgroup\$
  • 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 '15 at 15:56
1
\$\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
| improve this answer | |
\$\endgroup\$
  • 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 '15 at 14:53
1
\$\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!)

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Martin Ender Sep 28 '15 at 14:38
  • \$\begingroup\$ @MartinBüttner Thanks! I updated the post. What exactly do you mean by \0? \$\endgroup\$ – kirbyfan64sos Oct 1 '15 at 19:44
  • \$\begingroup\$ Something to reference the entire match, not just a capturing group. \$\endgroup\$ – Martin Ender Oct 1 '15 at 19:59
  • \$\begingroup\$ @MartinBüttner I guess not. :( \$\endgroup\$ – kirbyfan64sos Oct 1 '15 at 20:05
  • \$\begingroup\$ @MartinBüttner I'm just using Python's built-in substitution thing. I can easily override it, though... \$\endgroup\$ – kirbyfan64sos Oct 1 '15 at 20:06
1
\$\begingroup\$

OCaml, 106

for i=1to 100do
let(!)n=i mod n<1and p=Printf.printf
in!3&p"Fizz"=();!5&p"Buzz"=()or!3||p"%d"i=();p"
"done

Apparently this isn't a very good attempt as the shortest one on anarchy golf is only 97.

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

Scala, 90 bytes

for(i<-1 to 100)println{var s="";if(i%3==0)s="Fizz";if(i%5==0)s+="Buzz";if(s=="")i else s}
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Dart, 88

main({i:0}){while(i<100)print(["FizzBuzz","Fizz","Buzz",++i][(i%3).sign*2+(i%5).sign]);}

Dart is somewhat hampered in the golfing by not having conversion between bool and int, but the sign getter on integers helps a little.

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

Groovy, 83 80 bytes

(1..100).each{def f=it%3,b=it%5;println!f&&!b?'FizzBuzz':!f?'Fizz':!b?'Buzz':it}
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Python 2, 72 bytes

for x in range(100):print('Fizz'*(x%3>1)+'Buzz'*(x%5>3)or str(x+1))+'\n'

Not as clever as feersum's solution, but it avoids casting exec magic.

EDIT: with just two more parentheses, it works in Python 3 AND Python 2:

for x in range(100):print(('Fizz'*(x%3>1)+'Buzz'*(x%5>3)or str(x+1))+'\n')
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

APL, 56 50 bytes

⊣{1+⍵⊣⎕←∊2↑((0=3 5|⍵)/'Fizz' 'Buzz'),''(⍕⍵)}⍣100⊢1

Note: The very first should suppress output in GNU APL. Replace it with to get correct results in Dyalog, etc, or with portable assignment to some variable X← adding one byte.

A+, 51 bytes

(x←100)do↓⊃2↑((0=3 5|1+x)/4⊂'FizzBuzz'),⌽2↑<1↓⍕1+x;
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ These seem quite different, so why not put them in different answers? \$\endgroup\$ – Martin Ender Nov 9 '15 at 7:47
  • \$\begingroup\$ While I wouldn't call A+ a “trivial variant,” it's a direct derivative of APL and I think any solution in one would translate to similar byte count in another. Also, I just updated it making the answers work exactly the same way. \$\endgroup\$ – user46915 Nov 9 '15 at 9:22
1
\$\begingroup\$

ShapeScript, 57 bytes

0'1+0?3%1<"Fizz"*1?5%1<"Buzz"*+"#0?"1?_1<*!@"
"@'554***!#

Try it online!

How it works

0          Push 0 (accumulator).
'          Push a string that, when evaluated, does the following:
  1+         Increment the accumulator.
  0?         Push a copy.
  3%1<       Check if the remainder of its division by 3 is zero.
  "Fizz"*    Push "Fizz" for True, "" for False.
  1?         Push another copy of the accumulator.
  5%1<       Check if the remainder of its division by 5 is zero.
  "Buzz"*    Push "Buzz" for True, "" for False.
  +          Concatenate the potential fizzes and buzzes.
  "          Push a string that, when evaluated, does the following:
    #          Discard the topmost stack item.
    0?         Push a copy of the item below it (accumulator).
  "
  1?         Push a copy of the concatenation.
  _1<        Check if its length is zero.
  *!         Execute "#0?" once for True, zero times for False.
  @          Swap the generated output with the accumulator.
  "          Push "\n".
  "
  @          Swap it with the accumulator.
'
554**      Push 5 * 5 * 4 = 100.
*!         Execute the '...' string 100 times.
#          Discard the accumulator.
| improve this answer | |
\$\endgroup\$
1
\$\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
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Rust, 145 137 131 bytes

Golfed

fn main(){for i in 1..101{let s=i.to_string();println!("{}",match i%15{0=>"FizzBuzz",3|6|9|12=>"Fizz",5|10=>"Buzz",_=>&(s)[..]});}}

Ungolfed

fn main() {
    for i in 1..101 {
        let s = i.to_string();
        println!("{}", match i % 15 {
            0        => "FizzBuzz",
            3|6|9|12 => "Fizz",
            5|10     => "Buzz",
            _        => &(s)[..]
        });
    }
}

Uses the current stable version of Rust (1.5.0).

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can remove a semicolon after println! call, as println! returns (). Also, it's possible to replace &(s)[..] with &s. \$\endgroup\$ – Konrad Borowski Jul 6 '18 at 7:53
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 32 chars / 47 bytes

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

Try it here (Firefox only).

| improve this answer | |
\$\endgroup\$
1
\$\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);
    }
}
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Racket, 125 122 bytes

(for([x(range 1 101)])(define(m n)(=(modulo x n)0))(displayln(cond[(and(m 3)(m 5))'FizzBuzz][(m 3)'Fizz][(m 5)'Buzz][x])))

Simplest approach, took some work to get it lower than 130 bytes. Inspired by the Java example.

Pretty-printed code

(for ([x (range 1 101)])
  (define (m n)
    (= (modulo x n) 0))
  (displayln (cond
               [(and (m 3) (m 5)) 'FizzBuzz]
               [(m 3) 'Fizz]
               [(m 5) 'Buzz]
               [x])))
| improve this answer | |
\$\endgroup\$
1
4 5
6
7 8
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.