203
\$\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>

Toby Speight
6,9941 gold badge30 silver badges43 bronze badges
asked Sep 24, 2015 at 19:23
\$\endgroup\$
15
  • 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\$ Commented Sep 24, 2015 at 20:47
  • 1
    \$\begingroup\$ @AShelly Only when running \$\endgroup\$ Commented 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\$ Commented Sep 24, 2015 at 23:28
  • 10
    \$\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\$ Commented Sep 25, 2015 at 0:50
  • 77
    \$\begingroup\$ A "vanilla fizzbuzz" sounds delicious. \$\endgroup\$ Commented Sep 25, 2015 at 15:12

436 Answers 436

1
...
3 4
5
6 7
...
15
4
\$\begingroup\$

Java, (削除) 100 (削除ここまで) (削除) 98 (削除ここまで) (削除) 95 (削除ここまで) 94 Bytes (only loop, full code is 136 bytes, see bottom)

for(int i=0;i++<100;){var s=i%3<1?"Fizz":"";s=i%5<1?s+"Buzz":s;System.out.println(s==""?i:s);}

Try it here. Probably could be a lot smaller. I just started to learn java a few days ago (although I do have experience in other languages), this is the best I can do. Breakdown of how it works. Sorry if I get some terms wrong or explain stuff poorly:

for(int i=0;i++<100;) 

Just the loop - starts at 1 goes to 100:

{var s=i%3<1?"Fizz":"";

Making a variable called "s", equal to a value, if "i" is divisible by 3 with a remainder of less than 1 (1 character/byte smaller than checking ==0), set "s" equal to "buzz". If it isn't, set it equal to "".

s=i%5<1?s+"Buzz":s;

Set s equal to a value: if i is divisible by 5 with a remainder of less than 1, set s equal to s + "buzz", if it isn't, set it equal to itself.

System.out.println(s==""?i:s);}

System.out.println is just a simple print statement. Inside it, check if s is equal to "" (if s wasn't divisible by 3 or 5, it would be "") print i (the number), otherwise print s.

Feedback is greatly appreciated. Edit: 94 bytes for just the loop, below is what I think the full script would be, 136 bytes : (

interface f{static void main(String[]a){for(int i=0;i++<100;){var s=i%3<1?"Fizz":"";s=i%5<1?s+"Buzz":s;System.out.println(s==""?i:s);}}}
ouflak
2,0892 gold badges18 silver badges28 bronze badges
answered Jan 30, 2021 at 21:10
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to Code Golf, nice first answer! You may want to check out Tips for golfing in Java. \$\endgroup\$ Commented Jan 30, 2021 at 21:19
4
\$\begingroup\$

Excel VBA, 81 Bytes

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

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

Ungolfed

For i=1To 100 '' iterate from 1 to 100
 o=IIf(i Mod 3,"","Fizz") '' set the out var to `Fizz` if i Mod 3 = 0, else empty
 +IIf(i Mod 5,"","Buzz") '' append `Buzz` to out var if i Mod 5 = 0
 ?IIf(o="",i,o) '' If out var is non-empty, output the out var
 '' else output i
Next

Worksheet Version, 95 Bytes

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

 [A1]="=Let(r,Row(1:100),s,If(Mod(r,3),"""",""FIZZ"")&If(Mod(r,5),"""",""BUZZ""),If(s="""",r,s))
answered Aug 20, 2017 at 17:51
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can remove o="": and change your first iif to IIf(i Mod 3,"","Fizz") \$\endgroup\$ Commented Sep 7, 2018 at 20:53
  • \$\begingroup\$ For the Subroutine version, the () after F isn't optional, and gets added in automatically. Same with the ? in debug. \$\endgroup\$ Commented Feb 26, 2019 at 18:14
  • 1
    \$\begingroup\$ @Selkie, the behavior you are describing is known as autoformatting, and it is generally accepted in this community for VBA and for all languages that exhibit autoformatting, one may post their code from before it is autoformatted. in this case, that would mean that a couple of spaces may be removed, terminal "s dropped, changing print to ? and removal of the () from the Sub declaration. I suggest that you take a look at the Tips for Golfing in VBA page for more info \$\endgroup\$ Commented Feb 26, 2019 at 21:30
4
\$\begingroup\$

Perl 5 (ppencode-compatible), (削除) 1232 (削除ここまで) 690 bytes

Today I learned that and has higher precidence than or or xor; I suffered from making a proper control flow

eval q y eval q x ord or return cos while chop and chop and chop x and print chr ord uc qw q for q and print chr ord q tie gt and print chr length q else x oct oct ord q eq le and print chr length q else x oct oct ord q eq le and return cos for uc y xor eval q y print uc chr ord q dbmopen and and print chr ord q dump and and print chr length q else x oct oct ord q eq le and print chr length q else x oct oct ord q eq le and return cos if length q q s s q eq chr ord reverse length or not chr ord reverse length y or eval q x print length unless length q q s s q eq chr ord reverse length or not chr ord reverse length x xor print chr hex qw q and q while s qq q and length ne ord qw q eq

Try it online!

How it works

# fizz part
eval q y
 # if(length%3==0)
 eval q x
 # instead of length or return 1
 ord or return cos
 while
 # instead of s/...//
 chop and chop and chop
 x and
 
 # print 'Fizz'
 print chr ord uc qw q for q and
 print chr ord q tie gt and
 print chr length q else x oct oct ord q eq le and
 print chr length q else x oct oct ord q eq le and
 # for truthy
 return cos
for
 # instead of $_
 uc
y
xor
# buzz part
eval q y
 # print 'Buzz'
 print uc chr ord q dbmopen and and
 print chr ord q dump and and
 print chr length q else x oct oct ord q eq le and
 print chr length q else x oct oct ord q eq le and
 # for truthy
 return cos
if
 # instead of length%5, /[05]$/
 length q q s s q eq chr ord reverse length or
 not chr ord reverse length
y
or
# this block is done when length%15==0 or length%3&&length%5
eval q x
 print length
unless
 # instead of length%5
 length q q s s q eq chr ord reverse length or
 not chr ord reverse length
x
xor
# newline
print chr hex qw q and q
while
 s qq q and
 # ord qw q eq == 101
 length ne ord qw q eq

Previous

eval q y eval q kill and length or return cos while s q qq and s q qq and s q qq and s q qq and s q qq and s q qq and s q qq and s q qq and s q qq and s q qq and s q qq and s q qq and s q qq and s q qq and s q qq and ok and print chr ord uc qw q for q and print chr ord q tie gt and print chr length q else x oct oct ord q eq le and print chr length q else x oct oct ord q eq le and print uc chr ord q dbmopen and and print chr ord q dump and and print chr length q else x oct oct ord q eq le and print chr length q else x oct oct ord q eq le and return cos for uc y or eval q y eval q kill and length or return cos while s q qq and s q qq and s q qq and s q qq and s q qq and ok and print uc chr ord q dbmopen and and print chr ord q dump and and print chr length q else x oct oct ord q eq le and print chr length q else x oct oct ord q eq le and return cos for uc y or eval q y eval q kill and length or return cos while s q qq and s q qq and s q qq and ok and print chr ord uc qw q for q and print chr ord q tie gt and print chr length q else x oct oct ord q eq le and print chr length q else x oct oct ord q eq le and return cos for uc y or print length xor print chr length q q x x x x eq while s qq q and length ne ord qw q eq

Try it online!

Explained

# fizzbuzz part
eval q y
 # length%15==0?
 eval q k
 ill and length or return cos
 while
s q qq and s q qq and s q qq and
s q qq and s q qq and s q qq and
s q qq and s q qq and s q qq and
s q qq and s q qq and s q qq and
s q qq and s q qq and s q qq and o
 k and
 # then print FizzBuzz, as in first ppencode
 print chr ord uc qw q for q and
 print chr ord q tie gt and
 print chr length q else x oct oct ord q eq le and
 print chr length q else x oct oct ord q eq le and
 print uc chr ord q dbmopen and and
 print chr ord q dump and and
 print chr length q else x oct oct ord q eq le and
 print chr length q else x oct oct ord q eq le and
 # cos is for TRUTHY
 return cos
 # don't change $_ outside 
 for uc
# buzz part, like above
y or eval q y
 eval q kill and length or return cos while s q qq and s q qq and s q qq and s q qq and s q qq and ok and print uc chr ord q dbmopen and and print chr ord q dump and and print chr length q else x oct oct ord q eq le and print chr length q else x oct oct ord q eq le and return cos for uc
# fizz part, like above
y or eval q y
 eval q kill and length or return cos while s q qq and s q qq and s q qq and ok and print chr ord uc qw q for q and print chr ord q tie gt and print chr length q else x oct oct ord q eq le and print chr length q else x oct oct ord q eq le and return cos for uc
# no special strings
y or print length
# print LF
xor print chr length q q x x x x eq
# pretty familiar, isn't it?
while s qq q and length ne ord qw q eq
answered Aug 3, 2021 at 8:51
\$\endgroup\$
0
4
\$\begingroup\$

Fact, 11 bytes (SBCS)

ef̀|3 5^ŹFh̃õ

Explanation

e 1e2 (i.e. 100)
 f̀ for i in 1:100
 |3 5 Divisibility by [3, 5] (vectorizes)
 ^ String repeat by (vectorizes):
 ŹF "FizzBuzz"
 h̃ Halved (i.e. ["Fizz", "Buzz"])
 õ Logical OR with i
 (in Fact, lists are false when all items are "" or 0
 or when the list is empty)
Implicit grid (smash inner lists and join outer list by newline)

Fork and test

Fact, 15 bytes

ef̀|3 5^'b̀M̀b̀Ò'h̃õ

No "FizzBuzz" builtin, simply uses string compression

answered Aug 30, 2021 at 1:45
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome back old friend! \$\endgroup\$ Commented Aug 30, 2021 at 2:01
  • 1
    \$\begingroup\$ Welcome to Code Golf, new user! winks \$\endgroup\$ Commented Aug 30, 2021 at 2:01
  • 1
    \$\begingroup\$ Thank you. :) Hate to say that, but this is going to be my last post before a 4-month hiatus. I'll return as soon as possible \$\endgroup\$ Commented Aug 30, 2021 at 2:07
4
\$\begingroup\$

Python, (削除) 108 (削除ここまで) 102 bytes

Thanks to caird coinheringaahing for -6 bytes!

for x in range(1,101):
 y=""
 if x%3<1:y="Fizz"
 if x%5<1:y+="Buzz"
 y=y or x
 print(y)
answered Jan 30, 2022 at 17:09
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can use <1 instead of ==0 for -1 byte, and instead of if y=="":y=x", you can do y=y or x. Also, this just outputs the numbers from 1 to 100: Try it online! This is 82 bytes \$\endgroup\$ Commented Jan 30, 2022 at 18:10
4
\$\begingroup\$

Quipu, 101 bytes

1&0&'F0&'B1&\n
++[]'i[]'u[]/\
1%3&'z5&'z6&0&
1&%%'z%%'z==??
%%3&/5円&/0円&
1&>> >>6&[]
>> ??/\
::

Try it online!

Ungolfed:

" 0 1 2 3 4 5 6"
"--------------------"
 1& 0& 'F 0& 'B 1& \n
 ++ [] 'i [] 'u [] /\
 1% 3& 'z 5& 'z 6& 0&
 1& %% 'z %% 'z == ??
 %% 3& /\ 5& /\ 0&
 1& >> >> 6& []
 >> ?? /\
 ::

Thread 0 increments the accumulator (acc) every iteration, stopping execution if acc == 101, then jumps to thread 1.

Thread 1 checks if (acc % 3) > 0. If so, it jumps to thread 3, otherwise it jumps to thread 2.

Thread 2 prints Fizz, then jumps to thread 3.

Thread 3 checks if (acc % 5) > 0. If so, it jumps to thread 5, otherwise it jumps to thread 4.

Thread 4 prints Buzz, then jumps to thread 6.

Thread 5 looks back to thread 1 to see if (acc % 3) == 0. If so, it jumps to thread 6, otherwise it prints acc, then jumps to thread 6.

Thread 6 prints a newline, then jumps back to thread 0.

I think this could probably be refactored to golf away some of the whitespace, but I'm not doing that since if I do, I'll likely get sucked in and do nothing else for ~8 hours or so. :/

DLosc
40.7k6 gold badges87 silver badges142 bronze badges
answered Sep 2, 2021 at 16:28
\$\endgroup\$
1
  • \$\begingroup\$ Two less whitespace by adding four more threads. I gave this a go before looking at your answer and ended up at 102 bytes through a different method \$\endgroup\$ Commented Sep 6, 2021 at 8:11
4
\$\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.

answered May 2, 2022 at 23:41
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Commented May 2, 2022 at 23:42
  • \$\begingroup\$ 111 bytes \$\endgroup\$ Commented May 1, 2024 at 16:45
4
\$\begingroup\$

Rattle, (削除) 54 (削除ここまで) 44 bytes

Fizz&Buzz|!I=[g+bs%3[0b0b^0]g%5[0b1b^0]B]100

Try it Online!

This [was] my first answer in my new programming language! (This answer has since been golfed, and works on my new online interpreter)

(削除) Eventually, this programming language might have a more concise way to solve this challenge. (削除ここまで)

^this ended up being true - after a couple updates, 10 bytes can be shaved off the original answer (without implementing trivial built-ins)

Explanation

Fizz&Buzz a variable containing the text Fizz and Buzz
| signals the end of the input
! is a flag to disable implicit printing at EOF
I splits the variable into parts and stores it in consecutive memory slots
= sets top of stack to 0
[ start outer loop
g+bs gets value at slot 3 (starts at zero), increments, appends it to a buffer, saves it to slot 3
%3 takes the current value on stack and pushes the value mod 3 to stack
[0b0b^0] if the value on stack is equal to 0, concatenates value from memory slot 0 ("Fizz") to a buffer and nullifies the 0th element of the buffer
g%5 pushes value from slot 3 to stack, takes the value and pushes the value mod 5 to stack
[0b1b^0] if the value on stack is equal to 0, concatenates the value from memory slot 1 ("Buzz") to the buffer and nullifies the 0th element of the buffer if not null already
B if the buffer is non-empty, prints buffer
]100 end outer loop - repeats 100 times
answered Aug 14, 2020 at 19:25
\$\endgroup\$
4
\$\begingroup\$

Julia 59 char, 59 bytes

Below works in the Julia REPL

(x->(t="Fizz"^(x%3<1)*"Buzz"^(x%5<1);t>"" ? t : x)).(1:100)

an alternative solution of same char/byte length in Julia REPL is

(x->(s=x%3<1;t=x%5<1;(x,"Fizz"^s*"Buzz"^t)[s|t+1])).(1:100)

In search of the 59 char/59 bytes solution which includes a print statement.

answered Feb 21, 2023 at 14:46
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Commented Feb 21, 2023 at 14:47
  • 1
    \$\begingroup\$ This only prints in the repl. You can either specify Julia REPL, or add a print \$\endgroup\$ Commented Feb 24, 2023 at 12:50
4
\$\begingroup\$

Thunno 2 N, 11 bytes

ɦık+Ḋkg×ばつJn|

Attempt This Online!

Add a trailing £ if you want it flagless.

Thunno 2, (削除) 18 (削除ここまで) 17 bytes

ɦı35d×ばつJn|£

Attempt This Online!

No questionable constants.

Explanation

ɦık+Ḋkg×ばつJn| # Full program
ɦ # Push 100
 ı # Map over [1..100]:
 k+ # Push [3, 5]
 Ḋ # Divisible by [3, 5] (vectorises)
 kg # Push ["Fizz", "Buzz"]
 ×ばつ # Multiply element-wise
 J # Join by nothing
 n| # Logical OR with the number
 # N flag joins by newlines
 # Implicit output
ɦı35d×ばつJn|£ # Full program
ɦ # Push 100
 ı # Map over [1..100]:
 35d # Push [3, 5]
 Ḋ # Divisible by [3, 5] (vectorises)
 ʋ4ȥæÞ # Push compressed string "FizzBuzz"
 1⁄2 # Split in half to get ["Fizz", "Buzz"]
 ×ばつ # Multiply element-wise
 J # Join by nothing
 n| # Logical OR with the number
 £ # Print the result
answered Apr 12, 2023 at 10:42
\$\endgroup\$
4
\$\begingroup\$

Fortran, 187 bytes

do i=1,100
if(mod(i,3)==0.and.mod(i,5)==0)then;print'(a)','fizzbuzz'
elseif(mod(i,3)==0)then;print'(a)','fizz'
elseif(mod(i,5)==0)then;print'(a)','buzz'
else;print'(i0)',i
endif;enddo;end
answered Aug 3, 2017 at 15:27
\$\endgroup\$
4
  • 3
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Commented Aug 3, 2017 at 15:32
  • 2
    \$\begingroup\$ ...change print'(a)' etc. to print* to save 16 bytes \$\endgroup\$ Commented Apr 5, 2019 at 14:49
  • 2
    \$\begingroup\$ I never used print* as it prints the characters with a space or two before hand, thus it doesn't match the required format. \$\endgroup\$ Commented Apr 15, 2019 at 14:09
  • 1
    \$\begingroup\$ You can change end if to endif and save one byte :) \$\endgroup\$ Commented Oct 6, 2023 at 23:34
4
\$\begingroup\$

Fortran (GFortran), (削除) 145 138 134 128 (削除ここまで) 125 bytes

character*8s;do5 i=1,100;s=''
if(mod(i,3)<1)s='fizz'
if(mod(i,5)<1)s=trim(s)//'buzz'
if(s=='')write(s,'(i0)')i
5 print*,s
end

Try it Online!

On line 4 we write the current value of i into the string s. This greatly simplifies the subsequent print statement.
(削除) 128b (削除ここまで) (削除) 134b (削除ここまで) (削除) 138b (削除ここまで) (削除) 145b (削除ここまで)

answered Nov 4, 2022 at 22:21
\$\endgroup\$
4
\$\begingroup\$

Perl 5, (削除) 109 (削除ここまで) 93 bytes

for$i(1..100){if($i%3<1){say"Fizz"};if($i%5<1){say"Buzz"};if($i%3>0&&$i%5>0){say$i};say"\n";}

There might be some more golfing potential here.
Takes advantage the fact that 0 is the only way x % y can be less than 1 (thanks to Alex. A for shaving off 4 bytes with this) and Perl 6's say keyword.

Toto
1,0311 gold badge14 silver badges25 bronze badges
answered Sep 24, 2015 at 19:39
\$\endgroup\$
1
  • \$\begingroup\$ I didn't spot this somehow... Sorry, If you'd like to adopt some of the ideas in my answer you can save more bytes! say is available in Perl 5 too for free (meta.codegolf.stackexchange.com/q/273#answer-274) and it includes a newline (which would further shorten mine by 5 bytes). Happy to remove mine (since it's newer than yours). \$\endgroup\$ Commented Sep 24, 2015 at 20:26
4
\$\begingroup\$

Acc!!, 135 bytes

Count i while 900-i {
i/9%3/2*47620372+i/9%5/4*47621400*86^4
(_+0^_*(i/90+i%90/9*86+86)+86^8*85)/86^(i%9)%86
Write (_+48)%123)*0^0^(_
}

Try it online!

This started as a golf to DLosc's answer, but got a lot shorter from there.

The main optimisation this uses is, instead of manually writing the logic to conditionally print Fizz, Buzz and numbers, we build up an output buffer for each line and then print each character.

The buffer needs to fit 9 characters - potentially containing FizzBuzz\n - and needs to be able to contain the characters 123456789BFiuz\n. In particular, we never need to print 0, and indeed don't want to where it occurs as a first digit - we want 1, 2, Fizz, 4 not 01, 02, Fizz, 04. This means we can use 0 as a null character, and print buffer values with 48 - the codepoint of 0 - added to their charcodes. This also means that digits in the buffer will just be printed as themselves.

We do also need to print \n, which has a charcode of 10, and the way I've dealt with this is by storing it as 85 and moduloing the printed character by 123 (since z has a codepoint of 122), which results in (85 + 48) % 123) = 10. The buffer is stored in base 86, and built up like so:

0 0 0 0 0 0 0 0 Initial buffer
F i z z Added if multiple of 3
 B u z z Added if multiple of 5
5 6 Added if buffer is still zero
 \n Added after ^, due to zero checks

The hardcoded numbers 47620372 and 47621400 correspond to Fizz and Buzz respectively.

The other optimisation this uses is, rather than iterating 100 times to build each buffer and iterate over all 9 chars, we can iterate 900 times and divmod by 90 to get a line number and a buffer index. Count i while 900-i { iterates i from 0 to 899, and then:

  • i/9 is the line number from 0 to 99, and i/9%3/2 is 1 iff i%3 == 2 i.e. the line number is divisible by 3, so we put a Fizz in the buffer if that's the case. A similar thing happens for Buzz, and the buffer is then stored in the accumulator _.

  • 0^_ is 1 if the buffer is 0, i.e. if the buffer contains neither Fizz nor Buzz. If this is nonzero, we add the digits to the buffer. However, i/9 ranges from 0 to 99 and we want to print 1 to 100. While we could just use (i/9+1), we can take advantage of the fact that we never have to print a multiple of 10 for a shorter way.

    Instead of incrementing 14 into 15 and then manipulating its digits, we can take 1 4 and add 1 to the last digit to get 1 5. If done on e.g. 19, this will produce 1 10 instead of 2 0, but as this would result in a Buzz anyway it doesn't matter, and it saves a few bytes.

    We then add this to the buffer, along with a 86^8*85 representing a newline, and divide the buffer by the appropriate power of 86 to get the corresponding character, which is stored in the accumulator _ again.

  • Finally, we print the character, by adding to 48 and printing if it's nonzero exploiting an Acc!! bug - Write (_+48)%123)*0^0^(_ is transpiled to print(end=chr((_+48)%123)*0^0^(_)), and * in Python is string multiplication, so this will print the char only if it's nonzero.

answered Jan 15 at 21:29
\$\endgroup\$
3
\$\begingroup\$

Mouse, 75 bytes

1I:(I.101<^0J:I.30円=["Fizz"J.1+J:]I.50円=["Buzz"J.1+J:]J.0=[I.!]"!"I.1+I:)$

Ungolfed:

1 I: ~ Start a loop index at 1
( I. 101 < ^ ~ While I < 101...
 0 J: ~ Begin a divisibility indicator at 0
 I. 3 \ 0 = [ ~ If I % 3 == 0
 "Fizz" ~ Print "Fizz" to STDOUT
 J. 1 + J: ~ Increment J
 ]
 I. 5 \ 0 = [ ~ If I % 5 == 0
 "Buzz" ~ Print "Buzz" to STDOUT
 J. 1 + J: ~ Increment J
 ]
 J. 0 = [ ~ If neither 3 nor 5 divides I
 I. ! ~ Print I to STDOUT
 ]
 "!" ~ Print a newline
 I. 1 + I: ~ Increment I
)
$
answered Sep 24, 2015 at 20:04
\$\endgroup\$
3
  • 1
    \$\begingroup\$ I'm gonna upvote if you add a cat-program. \$\endgroup\$ Commented Sep 24, 2015 at 20:16
  • 1
    \$\begingroup\$ @flawr You should submit your own cat program. It will be a game of cat and mouse. \$\endgroup\$ Commented Sep 24, 2015 at 20:44
  • 1
    \$\begingroup\$ @flawr Not quite cat, but I did add a zcat program. \$\endgroup\$ Commented Sep 25, 2015 at 2:23
3
\$\begingroup\$

Processing, 74 bytes

This is based on the Java answer by Geobits. I converted it into Processing and since Processing is similar to Java, the code is a lot similar to Geobits'.

for(int i=0;i++<100;)println((i%3<1?"Fizz":"")+(i%5<1?"Buzz":i%3<1?"":i));
answered Sep 24, 2015 at 20:21
\$\endgroup\$
1
  • 10
    \$\begingroup\$ Ah, Processing. Java's willful son, stubbornly insisting that he's going to grow up to be an artist :D \$\endgroup\$ Commented Sep 24, 2015 at 20:24
3
\$\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.

answered Sep 25, 2015 at 5:06
\$\endgroup\$
3
\$\begingroup\$

Common Lisp, 121

(dotimes(x 100)(flet((d(n)(= 0(mod(1+ x)n))))(princ(cond((d 15)"FizzBuzz")((d 3)"Fizz")((d 5)"Buzz")(t(1+ x)))))(terpri))

Readable:

(dotimes (x 100)
 (flet ((d (n) (= 0 (mod (1+ x) n))))
 (princ (cond ((d 15) "FizzBuzz")
 ((d 3) "Fizz")
 ((d 5) "Buzz")
 (t (1+ x)))))
 (terpri))
answered Sep 25, 2015 at 13:43
\$\endgroup\$
3
\$\begingroup\$

Ruby, 59

I wanted to make a Ruby version using the slick modulo/integer-division/string-multiplication trick from feersum's Python answer (though unfortunately Ruby doesn't handle string multiplication the same way, so I spent some bytes on that):

100.times{|i|puts "#{i+1}\r"+"Fizz"*(i%3/2)+"Buzz"*(i%5/4)}

Note that this uses a carriage return \r without a newline. I don't know how portable this is; it works on my Linux and should work on Linux in general, as well as on Mac, but I'm not sure how Windows handles it. Without that, here's a 61-byte version:

100.times{|i|puts ("Fizz"*(i%3/2)+"Buzz"*(i%5/4))[/.+/]||i+1}
\$\endgroup\$
3
  • \$\begingroup\$ You don't need space after puts. \$\endgroup\$ Commented Dec 7, 2015 at 3:20
  • \$\begingroup\$ Your first answer always prints the number even when it should only print the string \$\endgroup\$ Commented Nov 21, 2021 at 23:25
  • \$\begingroup\$ As an amendment to my previous comment: The first answer may look visually correct on some terminals, the actual output still contains the number \$\endgroup\$ Commented Jan 20, 2023 at 0:37
3
\$\begingroup\$

Commodore Basic, 87 bytes

1F┌I=1TO100:F=F+1:B=B+1:IFF=3T|?"FIZZ";:F=0
2IFB=5T|?"BUZZ";:B=0
3IFF>0A/B>0T|?I;
4?:N─

Or in "shifted mode" to get both lower- and upper-case letters, but with the byte values of lowercase and uppercase swapped relative to ASCII-1967 (press COMMODORE+SHIFT):

1fOi=1to100:f=f+1:b=b+1:iff=3tH?"Fizz";:f=0
2ifb=5tH?"Buzz";:b=0
3iff>0aNb>0tH?i;
4?:nE

Usual PETSCII-to-Unicode substitutions: = SHIFT+O, | = SHIFT+H, / = SHIFT+N, = SHIFT+E

Commodore Basic doesn't have a "modulus" operation, so I need to use alternate methods to figure out when to print what: keeping a pair of counters turns out to be fewer bytes than dividing and checking for integer-ness. It also doesn't have a true logical "and" (despite the manual saying otherwise), so I need to do an explicit comparison against zero to decide if I should print the plain number.

answered Sep 26, 2015 at 6:05
\$\endgroup\$
1
  • \$\begingroup\$ @DLosc, you can switch into "shifted mode" which gives access to both upper- and lower-case, but with the character positions reversed relative to the ASCII-1967 which people are familiar with (eg. character #65 is lowercase "a" rather than uppercase). \$\endgroup\$ Commented Sep 28, 2015 at 2:27
3
\$\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}
answered Nov 4, 2015 at 17:04
\$\endgroup\$
3
\$\begingroup\$

Gol><>, 40 bytes

`e2RFL5%zR"zzuB"L3%zR"zziF"lQlRoaoC|LN|;

Updated for 0.4.0! I'm still tinkering with loops and trying to figure out how to do things best, but this is looking good so far.

Try it online.

Explanation

`e Push 'e', or 101
2RF ... | Execute F for loop twice - the first time activates the loop, and the
 second time updates it. This effectively makes the loop start from 1
L5%z Push 1 if loop counter % 5 is 0, else 1
R"zzuB" Push "Buzz" (top of stack) number of times
L3%z Push 1 if loop counter % 3 is 0, else 1
R"zziF" Push "Fizz" (top of stack) number of times
lQ ... | If the stack is not empty...
 lRo Output stack
 ao Output newline
 C Continue for loop
LN Otherwise, print loop counter with newline
; Terminate program

As we can see, there's a lot of abuse of R, which pops the top of the stack and executes the next instruction that many times.

answered Oct 27, 2015 at 12:35
\$\endgroup\$
1
  • \$\begingroup\$ That's a smart idea! Mind if I use it? \$\endgroup\$ Commented Oct 28, 2015 at 12:16
3
\$\begingroup\$

Jolf, (削除) 42 (削除ここまで) (削除) 33 (削除ここまで) 31 bytes

Try it here! Replace ƒ with \x9f. I'm stealing ETHproduction's method of fizzbuzzing.

ƒΜz~1d|]"FizzBuzz"?%H340?%H548H

Old version, 42 bytes

γ"Fizz"ƒΜz~1d?mτ ͅ35H+γζ?m|3Hγ?m|5HΖ"Buzz"H
γ"Fizz" γ = "Fizz"
 Μz~1d map 1..100 with the following function
 ?mτ ͅ35H if both 3 and 5 | H
 +γζ return γ + ζ
 m|3H else if 3 | H
 γ return γ
 ?m|5H else if 5 | H
 Ζ"Buzz" return ζ = "Buzz"
 H else return H
 ƒ join by newlines

I might be able to golf it down by using the dictionary in Jolf, but who would want to with such a perfect score?

answered Feb 24, 2016 at 23:44
\$\endgroup\$
3
\$\begingroup\$

D, 130 bytes

import std.stdio,std.conv;void main(){string x;for(int i;i++<100;x=((i%3?"":"Fizz")~(i%5?"":"Buzz")),writeln(x?x:i.to!string)){};}

Ungolfed:

module FizzBuzz;
import std.stdio;
import std.conv;
void main() {
 string x;
 for (
 int i;
 i++ < 100;
 x = ((i % 3 ? "" : "Fizz")
 ~ (i % 5 ? "" : "Buzz")),
 writeln(x ? x : i.to!string)
 ){};
}

Or, building a string, for 142 bytes,

import std.stdio,std.conv;void main(){string x,y;for(int i;i++<100;x=((i%3?"":"Fizz")~(i%5?"":"Buzz")),y~=(x?x:i.to!string)~"\n"){};write(y);}

Ungolfed:

module FizzBuzz;
import std.stdio;
import std.conv;
void main() {
 string x,y;
 for (
 int i;
 i++ < 100;
 x = ((i % 3 ? "" : "Fizz")
 ~ (i % 5 ? "" : "Buzz")),
 y ~= (x ? x : i.to!string) ~ "\n"
 ){};
 write(y);
}
answered Mar 2, 2016 at 15:58
\$\endgroup\$
3
\$\begingroup\$

Hoon, 126 bytes

%+
turn
(gulf [1 100])
|=
a/@
=+
[=((mod a 3) 0) =((mod a 5) 0)]
"{?:(-< "Fizz" "")}{?:(-> "Buzz" "")}{?:(=(- [| |]) <a> "")}"

Map over the list [1...100] with the function, interpolating the strings "Fizz" and "Buzz". If both mods are false, then it also interpolates the number.

This uses the fact that =+ pushes the value to the top of the context, which you can access with - and navigate with -< or -> for head/tail. Unfortunately, it looks pretty ugly because it needs a newline after runes to minimize byte count, along with not having built in operator functions.

I'm not entirely sure if this counts as a valid entry: It simply returns a list of strings to be printed by the shell, which is optimal. The other way would be to use ~& to print each element as it's mapped over, but it would still be rendered as "Fizz" or "Buzz" (with quotes) since it's a typed print, along with the shell then printing out the entire list anyways since it's the return value.

answered Mar 2, 2016 at 20:09
\$\endgroup\$
0
3
\$\begingroup\$

Vim, (削除) 66, 57 (削除ここまで) 56 keystrokes

i1<esc>qqYp<C-a>q98@q3Gqy:s/\d*/Fizz<CR>3j@yq@y5Gqz:s<CR>ABuzz<esc>5j@zq@z

Further golfing, and explanation on the way.

answered May 5, 2016 at 20:20
\$\endgroup\$
1
  • \$\begingroup\$ A bit too late, but apparently this doesn't output correctly: Try it online! \$\endgroup\$ Commented May 16, 2017 at 9:47
3
\$\begingroup\$

Javascript, 56 bytes

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

Assuming 100 alerts is an acceptable output method.

answered Feb 10, 2017 at 6:20
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Nice answer, but it's already been posted (That one originally used alert as well, but it was changed to console.log because all the other JS answers do the same) \$\endgroup\$ Commented Mar 22, 2017 at 21:46
3
\$\begingroup\$

Ruby, (削除) 82 (削除ここまで) (削除) 72 (削除ここまで) (削除) 70 (削除ここまで) 68 bytes

based on other answers:

puts (1..100).map{|i|(x=(i%3<1?"Fizz":"")+(i%5<1?"Buzz":""))>""?x:i}

old solution:

(1..100).map{|i|$><<"Fizz"if f=i%3<1
$><<"Buzz"if b=i%5<1
$><<i if !(f||b)
puts()}
\$\endgroup\$
1
  • \$\begingroup\$ I posted a 50-byte solution. \$\endgroup\$ Commented Sep 27, 2015 at 22:44
3
\$\begingroup\$

AppleScript, (削除) 104 (削除ここまで) (削除) 102 (削除ここまで) 100 bytes

""
repeat with i from 1to 100
result&{"FizzBuzz",i,"Fizz","Buzz"}'s item((i^4mod 15+7)div 4)&"
"
end

If you run it in Script Editor, the result has "quotes" around it. If you run it with osascript, there are no "quotes", but the result ends with an extra blank line.

I steal the technique from Lynn's Lua answer, where the script picks from a list of possible values to print. I use (i^4mod 15+7)div 4 to calculate the index 1, 2, 3, or 4. It's different from Lua's i^2%3+i^4%5*2+1.

In AppleScript, i^4 raises i to 4th power. It returns a real, which is a floating-point number, but the value is equal to the correct integer.

The values of n = i^4mod 15 with i from 1 to 15 are

i = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = 1 1 6 1 10 6 1 1 6 10 1 6 1 1 0

So n is 0.0 for "FizzBuzz", 1.0 for i, 6.0 for "Fizz", or 10.0 for "Buzz". This pattern continues with i from 16 to 100. I need to map the values 0.0, 1.0, 6.0, 10.0 to a list index; lists in AppleScript start at index 1.

n = 0.0 1.0 6.0 10.0
(n+2)mod 7 = 2.0 3.0 1.0 5.0
(n+7)div 4 = 1 2 3 4

My 104-byte answer used (n+2)mod 7, but that mapping had 5.0 instead of 4.0, so it needed a list of 5 items, where the extra item 0, cost 2 bytes. My 102-byte answer uses (n+7)div 4. My 100-byte answer deletes 2 extra spaces.

answered Oct 25, 2017 at 2:33
\$\endgroup\$
3
\$\begingroup\$

Forth, (削除) 107 (削除ここまで) (削除) 101 (削除ここまで) 98 bytes

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

Ungolfed + close Python equivalent:

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

Run it!

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.