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>
436 Answers 436
Cardinal 217 bytes
%x>+ v >+++++~M! 8# "buzz"
0 #~0#+++~>M! # V "fizz"
+ ^jM< ~ V>#
+ V > #xV
= 0 V
t ~ >#x
t t v V
* = > #}#} /
> ^ >}/
.
Explanation
%x
0
+
+
= 0
t ~
t t
* =
> ^
Initializes a pointer with an inactive value of 100 and active value of 0
>+ v
#
^jM<
Loops around, incrementing the active value of the pointer and sending out a duplicate until the active value is equal to the inactive value (100)
>+++++~M!
~0#+++~>M!
Checks if the value mod 3 or mod 5 is equal to 0
# "buzz"
# # V "fizz"
~ V>#
V > #xV
V
>#x
v V
> #}#} /
>}/
.
Uses reflectors and splitters in order to print only numbers that are not divisible by 3 or 5. Printing fizz when divisible by 3 and buzz when divisible by 5.
Java, (削除) 153 (削除ここまで) 145 bytes
enum f{;static{for(int i=1;i<101;i++)System.out.println(i%3>0&i%5>0?i:(i%3<1&i%5<1?"FizzBuzz":(i%3<1?"Fizz":(i%5<1?"Buzz":0))));System.exit(0);}}
Not as long as I expected it to be, maybe because I ommited main function and used an enum. Quite overcomplicated print statement.
Ungolfed version(Modified to be readable) with comments:
enum f {; // Required because it is an enum
static { // static block
for (int i = 1; i < 101; i++) { // loop from 1 to 100 inclusive
boolean by3 = i % 3 == 0;
boolean by5 = i % 5 == 0;
if (!by3 && !by5) // Not divisible by 3 and 5
System.out.println(i); // Just show the number
else if (by3 && by5) // Divisible by both
System.out.println("FizzBuzz"); // Show FizzBuzz
else if (by3) // Divisible only by 3
System.out.println("Fizz"); // Show Fizz
else // Divisible only by 5
System.out.println("Buzz"); // Show Buzz
}
System.exit(0); // Required because there can't be output to stderr and it would crash trying to run main function
}
}
Ungolfed not modified:
enum f {;
static {
for (int i = 1; i < 101; i++)
System.out.println(i % 3 > 0 & i % 5 > 0 ? i : (i % 3 < 1 & i % 5 < 1 ? "FizzBuzz" : (i % 3 < 1 ? "Fizz" : (i % 5 < 1 ? "Buzz" : 0))));
System.exit(0);
}
}
EDIT: Saved 8 bytes by replacing ==0 with <1, !=0 with >0 and && with &
Julia, 70 bytes
for i=1:100 println("$(i%3<1?"Fizz":"")$(i%5<1?"Buzz":i%3>0?i:"")")end
Julia has perl-like string formatting. Very nice for challenges like these.
REXX 81 Bytes
f.=""
f.0="buzz"
t.=""
t.0="fizz"
do i=1 to 100
f=i//5
t=i//3
say overlay(t.t||f.f,i)
end
-
\$\begingroup\$ You can golf away four characters by removing the empty strings:
f.=instead off.="". \$\endgroup\$idrougge– idrougge2017年05月16日 11:54:32 +00:00Commented May 16, 2017 at 11:54 -
\$\begingroup\$ I tested f.= on a couple of online interpreters and they didn't like it. \$\endgroup\$theblitz– theblitz2017年05月16日 12:06:34 +00:00Commented May 16, 2017 at 12:06
-
\$\begingroup\$ All right, I only tested it with Regina. \$\endgroup\$idrougge– idrougge2017年05月16日 12:11:01 +00:00Commented May 16, 2017 at 12:11
Swift, 97 bytes
for i in 1...100{i%15==0 ?print("FizzBuzz"):i%3==0 ?print("Fizz"):i%5==0 ?print("Buzz"):print(i)}
q/kdb+, (削除) 58 (削除ここまで) (削除) 56 (削除ここまで) 49 bytes
Solution:
0{$[sum i:0=y mod 3 5;`Fizz`Buzz(&)i;y]}'1+(!)100
Example:
q)0{$[sum i:0=y mod 3 5;`Fizz`Buzz(&)i;y]}'1+(!)100
1
2
,`Fizz
4
,`Buzz
,`Fizz
7
8
,`Fizz
,`Buzz
11
,`Fizz
13
14
`Fizz`Buzz
16
...etc
Explanation:
0{$[sum i:0=y mod 3 5;`Fizz`Buzz where i;y]}'1+til 100 / ungolfed
{ }' / anonymous function that takes each-left and each-right
0 / this would be parameter 'x' but we dont use it
til 100 / til generates a list of 0..99
1+ / adds 1 to every item in the list, thus 1..100
$[ ; ; ] / switch, $[condition;true;false]
y mod 3 5 / modulo operation on input for 3 and 5, mod[1;3 5] = 1 1
0= / is 0 equal to this result (basically a 'not' operation)
i: / save in i for later
sum / add these, will get 0, 1 or 2. 0 is interpretted as false
where i / where gives indices where i is true
`Fizz`Buzz / 2 item list which gets indexed into (and implicitly returned)
y / return the input if the condition was false
Notes:
This ^^ is pretty much a q version of the k solution, so I've written in a different way.. unfortunately it's about 50% slower :(
0{(`Fizz;`Buzz;y)(&)(0=a),all a:mod[y;3 5]}'1+(!)100
Here we are indexing into a list of Fizz, Buzz, based on the result of the modulo operation... The k solution style is better.
MATLAB, 172 bytes
s=char(arrayfun(@(n){num2str(n)},[1:100 1e7])');s(3:3:end,1:4)=repmat('Fizz',33,1);s(5:5:100,1:4)=repmat('Buzz',20,1);s(15:15:100,:)=repmat('FizzBuzz',6,1);disp(s(1:100,:))
groovy, 55 bytes
Borrows idea from answer by @feersum https://codegolf.stackexchange.com/a/58623
100.times{println'Fizz'*(it%3/2)+'Buzz'*(it%5/4)?:it+1}
-
\$\begingroup\$ Welcome to PPCG! :) \$\endgroup\$Beta Decay– Beta Decay2017年09月24日 07:21:46 +00:00Commented Sep 24, 2017 at 7:21
Python 2, 57 bytes
for i in range(100):print i%3/2*"Fizz"+i%5/4*"Buzz"or i+1
Not the most beautiful code but it works at least with
python -c
Java (OpenJDK 8), 127 bytes
interface J{static void main(String[]a){for(int i=0;i++<100;)System.out.println((i%3<1?"Fizz":"")+(i%5<1?"Buzz":i%3<1?"":i));}}
ungolfed:
interface J {
static void main(String[] a) {
for (int i = 0; i++ < 100;)
System.out.println((i % 3 < 1 ? "Fizz" : "") + (i % 5 < 1 ? "Buzz" : i % 3 < 1 ? "" : i));
}
}
SmileBASIC, 70 bytes
Printing "Fizz" and "Buzz" is easy, the slightly more difficult part is to only print the number when required. There are basically 2 ways to do this (and they end up being the same length)
1: Print the number when I isn't divisible by 3 or 5
FOR I=0TO 100A=I MOD 3B=I MOD 5?"Fizz"*!A;"Buzz"*!B;STR$(I)*(A&&B)NEXT
2: Print the number if the cursor is at column 0:
FOR I=1TO 100?"Fizz"*!(I MOD 3);"Buzz"*!(I MOD 3);
?STR$(I)*!CSRX
NEXT
In a previous version of SB, % was used for MOD, making the program shorter:
Petit Computer BASIC, 62 bytes
FOR I=0TO 100?"Fizz"*!(I%3);"Buzz"*!(I%5);
?STR$(I)*!CSRX
NEXT
-
1\$\begingroup\$ The language name should probably be "SmileBASIC 2.x" or something. \$\endgroup\$snail_– snail_2017年02月06日 13:39:02 +00:00Commented Feb 6, 2017 at 13:39
KoopaScript, 250 characters
def i 1 if \%va is \%vu set a 1;setath b \%va %% 3;setath c \%va %% 5;setath e \%va %% 15;if \%vb is 0 if \%vc not 0 print Fizz;if \%vb not 0 if \%vc is 0 print Buzz;if \%ve is 0 print FizzBuzz;if \%vb not 0 if \%vc not 0 print \%va;setath a \%va + 1
Guide to reading: Don't. Either learn KoopaScript more or less entirely by looking at the code (my documentation isn't that great) or just take my word for it that it works. By the way, KoopaScript is an interpreted language I made (not specifically for this) that runs inside ActionScript 2, and doesn't actually have else statements, or... most of the stuff that makes other examples short. All functions are one line, so this was pretty easy. Here's the GitHub repo.
-
1\$\begingroup\$ This looks like quite the painful programming language to use haha. Welcome to PPCG! :) \$\endgroup\$DJMcMayhem– DJMcMayhem2018年03月08日 19:55:27 +00:00Commented Mar 8, 2018 at 19:55
-
\$\begingroup\$ Thanks! Yeah, it is a bit painful, but I can't really be bothered to add stuff like arrays. I made a pi approximator and a prime number generator (both very slow), both were quite painful because of the lack of useful functions. \$\endgroup\$Jhynjhiruu Rekrap– Jhynjhiruu Rekrap2018年03月08日 20:45:03 +00:00Commented Mar 8, 2018 at 20:45
Husk, 35 bytes
m§Ysλṁ!07ḣ3)ḣ100
¶Σfm%0NC2 ̈¶¶¶⌈iΩZu
I feel like I'm missing some opportunities for cleverness here. Like, I'm not really pleased with that lambda sitting there on the first line and having to be manually closed. But anyway, here we are.
Explanation
I'll explain the second line first. It's a function TNum -> [[TChar]] that maps
1 -> []
2 -> ["","","Fizz"]
3 -> ["","","","","Buzz"]
It works like this:
¶Σfm%0NC2 ̈¶¶¶⌈iΩZu
̈¶¶¶⌈iΩZu --The compressed string "\n\n\n\nFiBuzz"
C2 ̈¶¶¶⌈iΩZu --cut; the list ["\n\n", "\n\n", "Fi", "Bu", "zz]
m%0N --nats mod input, i.e, with input 3, [1,2,0,1,2,...]
fm%0NC2 ̈¶¶¶⌈iΩZu --filter;so grab (1) nothing, (2) pairs 135, (3) pairs 1245
Σfm%0NC2 ̈¶¶¶⌈iΩZu --concat;now at (1) [] (2) ["\n\nFizz"] (3) ["\n\n\n\nBuzz"]
¶Σfm%0NC2 ̈¶¶¶⌈iΩZu --split by newlines to return claimed result
Next, the lambda in the first line. It has type TNum -> [TChar]; given an integer, it returns "", "Fizz", "Buzz", or "FizzBuzz" as appropriate. In detail:
λṁ!07ḣ3)
ḣ3 --heads; the list [1,2,3]
7ḣ3 --call 2nd line with map overflow: [[],["","","Fizz"],["","","","","Buzz"]]
ṁ!07ḣ3 --index the lambda argument to each list (modularly) and concat the results
And pulling it together:
m§Ysλṁ!07ḣ3)ḣ100
λṁ!07ḣ3) --"","Fizz","Buzz",or"FizzBuzz"
s --string representation of the number
§Ysλṁ!07ḣ3) --apply both and take max; "" < numeric strings < alphabet
m§Ysλṁ!07ḣ3)ḣ100 --map across [1,..,100]
Then Husk's default output style for an object of type [[TChar]] turns out to be just what we want.
-
\$\begingroup\$ This could have been -3 bytes if we didn't need to hardcode it for 100 lines! Ah well. \$\endgroup\$Sophia Lechner– Sophia Lechner2018年03月23日 23:48:09 +00:00Commented Mar 23, 2018 at 23:48
///, 198 bytes
/%/!"
//$/
"!//#/
Fizz"
//"/Buzz//!/
Fizz
/1
2!47ドル
8%11!13
14#16
17!1922ドル
23%26!28
29#31
32!3437ドル
38%41!43
44#46
47!4952ドル
53%56!58
59#61
62!6467ドル
68%71!73
74#76
77!7982ドル
83%86!88
89#91
92!9497ドル
98!"
Ada (GNAT), 298 bytes
with Ada.Text_IO;use Ada.Text_IO;procedure T is begin for I in Integer range 1..100 loop if I mod 15 = 0 then Put_Line("FizzBuzz");elsif I mod 3 = 0 then Put_Line ("Fizz");elsif I mod 5 = 0 then Put_Line ("Buzz");else Put_Line (Integer'Image (I)(2 ..Integer'Image(I)'Last));end if; end loop; end T;
Ungolfed:
with Ada.Text_IO;use Ada.Text_IO;
procedure Test is begin
for I in Integer range 1 .. 100 loop
if I mod 15 = 0 then
Put_Line ("FizzBuzz");
elsif I mod 3 = 0 then
Put_Line ("Fizz");
elsif I mod 5 = 0 then
Put_Line ("Buzz");
else
Put_Line (Integer'Image (I)(2 .. Integer'Image(I)'Last));
end if;
end loop;
end Test;
Pretty vanilla, but I didn't see an Ada solution yet. Probably because Ada might just be the worst real-world language to golf with!
-
\$\begingroup\$ Ada isn't bad, it's more that hardly anyone even knows about it \$\endgroup\$ASCII-only– ASCII-only2018年04月18日 22:06:35 +00:00Commented Apr 18, 2018 at 22:06
-
\$\begingroup\$ Oh, I love Ada. It is definitely my favourite language. It just sucks for code golf. And yeah, I wish more people knew about it. :) \$\endgroup\$LambdaBeta– LambdaBeta2018年04月18日 22:11:31 +00:00Commented Apr 18, 2018 at 22:11
-
\$\begingroup\$ Well, it can't possibly be as bad as Java :P \$\endgroup\$ASCII-only– ASCII-only2018年04月18日 22:12:48 +00:00Commented Apr 18, 2018 at 22:12
-
\$\begingroup\$ also on your profile: "am familiar with a majority of commonly used programming languages". which ones exactly? :P \$\endgroup\$ASCII-only– ASCII-only2018年04月18日 23:05:59 +00:00Commented Apr 18, 2018 at 23:05
-
\$\begingroup\$ 207 \$\endgroup\$ASCII-only– ASCII-only2018年04月24日 09:56:58 +00:00Commented Apr 24, 2018 at 9:56
ORACLE SQL (107 bytes)
select nvl(decode(mod(rownum,3),0,'Fizz')||decode(mod(rownum,5),0,'Buzz'),rownum)from xmltable('1 to 100')
-
\$\begingroup\$ Hi, you can save 2 bytes: xmltable('0to 99') \$\endgroup\$t-clausen.dk– t-clausen.dk2024年07月23日 15:36:41 +00:00Commented Jul 23, 2024 at 15:36
QB64, (削除) 102 (削除ここまで) 94 bytes
FOR i=1TO 100
o$=MID$("Fizz",i*5MOD 15)+MID$("Buzz",i*5MOD 25)
IF""<o$THEN?o$ELSE WRITE i
NEXT
Doesn't work on actual QBasic; see below for why.
This program has one problem: QBasic/QB64 outputs to an 80x24 window, not a terminal, so the results can't be scrolled back. If you run the above code as-is, all you'll see is the lines from 78 onward. To prove that the code does 1 to 100 correctly, you can add the line SLEEP 1 right before NEXT for a 1-second delay on each iteration.
Ungolfed code and explanation
FOR i = 1 TO 100
index = 5 * (i MOD 3)
o$ = MID$("Fizz", index)
index = 5 * (i MOD 5)
o$ = o$ + MID$("Buzz", index)
IF "" < o$ THEN
PRINT o$
ELSE
WRITE i
END IF
NEXT
On each iteration, we put the appropriate fizzes and buzzes into the string o$, check if it's empty, and output o$ or the number accordingly. The main question is how to get "Fizz" when i is divisible by 3 and "" otherwise. Here are the approaches I tried:
IF i MOD 3THEN o$=""ELSE o$="Fizz"
o$="":IF i MOD 3=0THEN o$="Fizz"
o$=MID$("Fizz",5*(i MOD 3))
o$=MID$("Fizz",i*5MOD 15)
The approach with MID$ is much shorter. This function takes 3 arguments--string, start index, and number of characters--and returns the appropriate substring. When the third argument is omitted, it takes everything from the start index to the end of the string. Here, when i is exactly divisible, the start index is 0 and we get the whole string; otherwise, it's something larger that's past the end of the string, so MID$ gives "".1
The other tricky part is printing numbers according to the spec. QBasic's PRINT command outputs positive numbers with leading spaces, which is occasionally useful but usually just annoying. The WRITE command, however, does not add a leading space--perfect for our purposes here.
1 Strings are 1-indexed in QBasic--i.e., in the string "abcd", a is at index 1 and d is at index 4. This is why I'm multiplying the mod result by 5: MID$("Fizz",4) gives "z". In actual QBasic, 0 isn't a legal index and gives Illegal function call; but in QB64, MID$("Fizz",0) happily returns the whole string instead of complaining.
Whitespace, 307 bytes
[S S S N
_Push_0][N
S S N
_Create_Label_LOOP][S S S T N
_Push_1][T S S S _Add][S N
S _Duplicate][S N
S _Duplicate][S S S T T N
_Push_3][T S T T _Modulo][N
T S T N
_Jump_to_Label_FIZZ_if_0][N
S S S N
_Create_Label_RETURN_FIZZ][S N
S _Duplicate][S N
S _Duplicate][S S S T S T N
_Push_5][T S T T _Modulo][N
T S T T N
_Jump_to_Label_BUZZ_if_0][N
S S S S N
_Create_Label_RETURN_BUZZ][S S S T T S S T S S N
_Push_100][T S S T _Subtract][N
T S T T T N
_Jump_to_Label_EXIT_WITH_ERROR_if_0][N
S T T T T T N
_Call_Label_PRINT_INT][S S S T S T S N
_Push_10][T N
S S _Print_as_character][N
S N
N
_Jump_to_Label_LOOP][N
S S T
_Create_Label_FIZZ][S S S T S S S T T S N
_Push_70][T N
S S _Print_as_character][S S S T T S T S S T N
_Push_105][T N
S S _Print_as_character][S S S T T T T S T S N
_Push_122][S N
S _Duplicate][T N
S S _Print_as_character][T N
S S _Print_as_character][N
S N
S
_Return_to_Label_FIZZ_RETURN][N
S S T T N
_Create_Label_BUZZ][S S S T S S S S T S N
_Push_66][T N
S S _Print_as_character][S S S T T T S T S T N
_Push_117][T N
S S _Print_as_character][S S S T T T T S T S N
_Push_122][S N
S _Duplicate][T N
S S _Print_as_character][T N
S S _Print_as_character][N
S N
S S N
_Return_to_Label_BUZZ_RETURN][N
S S T T T T N
_Create_Label_PRINT_INT][S N
S _Duplicate][S S S T T N
_Push_3][T S T T _Modulo][N
T S S T N
_Jump_to_Label_LOOP_if_0][S N
S _Duplicate][S S S T S T N
_Push_5][T S T T _Modulo][N
T S S T N
_Jump_to_Label_LOOP_if_0][T N
S T _Print_as_integer][N
T N
_Return][N
S S S T N
_Create_Label_RETURN][N
T N
_Return]
Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.
Try it online (with raw spaces, tabs and new-lines only).
Can definitely be golfed. If-checks are rather annoying in Whitespace, and I still have to get used to them some more.
EDIT: Fixed TIO version. Will try to golf it some more later.
General explanation in Pseudo-code:
Integer i = 0
Start LOOP
Increase i by 1
If i modulo 3 is 0: Call function FIZZ()
If i modulo 5 is 0: Call function BUZZ()
If i is 100: Stop program
Call function PRINT_INT(i)
Print new-line
Go to next iteration of the LOOP
function FIZZ():
Print "Fizz"
Return
function BUZZ():
Print "Buzz"
Return
function PRINT_INT(integer i):
If i modulo 3 is 0: Return
If i modulo 5 is 0: Return
Print i
Return
Example run:
Command Explanation Stack STDOUT STDERR
SSSN Push 0 [0]
NSSN Create Label_LOOP [0]
SSSTN Push 1 [0,1]
TSSS Add (0+1) [1]
SNS Duplicate (1) [1,1]
SNS Duplicate (1) [1,1,1]
SSSTTN Push 3 [1,1,1,3]
TSTT Modulo (1%3) [1,1,1]
NTSTN Jump to Label_FIZZ if 0 [1,1]
NSSSSN Create Label_RETURN_FIZZ [1,1]
SNS Duplicate (1) [1,1,1]
SNS Duplicate (1) [1,1,1,1]
SSSTSTn Push 5 [1,1,1,1,5]
TSTT Modulo (1%5) [1,1,1,1]
NTSTTN Jump to Label_BUZZ if 0 [1,1,1]
NSSSSN Create Label_RETURN_BUZZ [1,1,1]
SSSTTSSTSSN Push 100 [1,1,1,100]
TSST Subtract (1-100) [1,1,-99]
NTSTTTN Jump to Label_EXIT if 0 [1,1]
NSTTTTTN Call Label_PRINT_INT [1,1]
NSSTTTTN Create Label_PRINT_INT [1,1]
SNS Duplicate (1) [1,1,1]
SSSTTN Push 3 [1,1,1,3]
TSTT Modulo (1%3) [1,1,1]
NTSSTN Jump to Label_RETURN if 0 [1,1]
SNS Duplicate (1) [1,1,1]
SSSTSTN Push 5 [1,1,1,5]
TSTT Modulo (1%5) [1,1,1]
NTSSTN Jump to Label_RETURN if 0 [1,1]
TNST Print as integer [1] 1
NTN Return [1]
SSSTSTSN Push 10 [1,10]
TNSS Print as character [1] \n
NSNN Jump to Label_LOOP [1]
SSSTN Push 1 [1,1]
TSSS Add (1+1) [2]
SNS Duplicate (2) [2,2]
SNS Duplicate (2) [2,2,2]
SSSTTN Push 3 [2,2,2,3]
TSTT Modulo (2%3) [2,2,2]
NTSTN Jump to Label_FIZZ if 0 [2,2]
NSSSSN Create Label_RETURN_FIZZ [2,2]
SNS Duplicate (2) [2,2,2]
SNS Duplicate (2) [2,2,2,2]
SSSTSTn Push 5 [2,2,2,2,5]
TSTT Modulo (2%5) [2,2,2,2]
NTSTTN Jump to Label_BUZZ if 0 [2,2,2]
NSSSSN Create Label_RETURN_BUZZ [2,2,2]
SSSTTSSTSSN Push 100 [2,2,2,100]
TSST Subtract (2-100) [2,2,-98]
NTSTTTN Jump to Label_EXIT if 0 [2,2]
NSTTTTTN Call Label_PRINT_INT [2,2]
NSSTTTTN Create Label_PRINT_INT [2,2]
SNS Duplicate (2) [2,2,2]
SSSTTN Push 3 [2,2,2,3]
TSTT Modulo (2%3) [2,2,2]
NTSSTN Jump to Label_RETURN if 0 [2,2]
SNS Duplicate (2) [2,2,2]
SSSTSTN Push 5 [2,2,2,5]
TSTT Modulo (2%5) [2,2,2]
NTSSTN Jump to Label_RETURN if 0 [2,2]
TNST Print as integer [2] 2
NTN Return [2]
SSSTSTSN Push 10 [2,10]
TNSS Print as character [2] \n
NSNN Jump to Label_LOOP [2]
SSSTN Push 1 [2,1]
TSSS Add (3+1) [3]
SNS Duplicate (3) [3,3]
SNS Duplicate (3) [3,3,3]
SSSTTN Push 3 [3,3,3,3]
TSTT Modulo (3%3) [3,3,0]
NTSTN Jump to Label_FIZZ if 0 [3,3]
NSST Create Label_FIZZ [3,3]
SNS Duplicate (3) [3,3,3]
SNS Duplicate (3) [3,3,3,3]
SSSTSSSTTSN Push 70 [3,3,3,3,70]
TNSS Print as character [3,3,3,3] F
SSSTTSTSSTN Push 105 [3,3,3,3,122]
TNSS Print as character [3,3,3,3] i
SSSTTTTSTSN Push 122 [3,3,3,3,122]
SNS Duplicate (122) [3,3,3,3,122,122]
TNSS Print as character [3,3,3,3,122] z
TNSS Print as character [3,3,3,3] z
NSNS Jump to Label_RETURN_FIZZ [3,3,3,3]
NSSSSN Create Label_RETURN_FIZZ [3,3,3,3]
SSSTSTN Push 5 [3,3,3,3,5]
TSTT Modulo (3%5) [3,3,3,3]
NTSTTN Jump to Label_BUZZ if 0 [3,3,3]
NSSSSN Create Label_RETURN_BUZZ [3,3,3]
SSSTTSSTSSN Push 100 [3,3,3,100]
TSST Subtract (3-100) [3,3,-97]
NTSTTTN Jump to Label_EXIT if 0 [3,3]
NSTTTTTN Call Label_PRINT_INT [3,3]
NSSTTTTN Create Label_PRINT_INT [3,3]
SNS Duplicate (3) [3,3,3]
SSSTTN Push 3 [3,3,3,3]
TSTT Modulo (3%3) [3,3,0]
NTSSTN Jump to Label_RETURN if 0 [3,3]
Stack contains additional leading [3, but we'll ignore it in this explanation
SSSTN Push 1 [3,1]
TSSS Add (3+1) [4]
SNS Duplicate (4) [4,4]
SNS Duplicate (4) [4,4,4]
SSSTTN Push 3 [4,4,4,3]
TSTT Modulo (4%3) [4,4,1]
NTSTN Jump to Label_FIZZ if 0 [4,4]
NSSSSN Create Label_RETURN_FIZZ [4,4]
SNS Duplicate (4) [4,4,4]
SNS Duplicate (4) [4,4,4,4]
SSSTSTN Push 5 [4,4,4,4,5]
TSTT Modulo (4%5) [4,4,4,4]
NTSTTN Jump to Label_BUZZ if 0 [4,4,4]
NSSSSN Create Label_RETURN_BUZZ [4,4,4]
SSSTTSSTSSN Push 100 [4,4,4,100]
TSST Subtract (4-100) [4,4,-96]
NTSTTTN Jump to Label_EXIT if 0 [4,4]
NSTTTTTN Call Label_PRINT_INT [4,4]
NSSTTTTN Create Label_PRINT_INT [4,4]
SNS Duplicate (4) [4,4,4]
SSSTTN Push 3 [4,4,4,3]
TSTT Modulo (4%3) [4,4,1]
NTSSTN Jump to Label_RETURN if 0 [4,4]
SNS Duplicate (4) [4,4,4]
SSSTSTN Push 5 [4,4,4,5]
TSTT Modulo (4%5) [4,4,4]
NTSSTN Jump to Label_RETURN if 0 [4,4]
TNST Print as integer [4] 4
NTN Return [4]
SSSTSTSN Push 10 [4,10]
TNSS Print as character [4] \n
NSNN Jump to Label_LOOP [4]
SSSTN Push 1 [4,1]
TSSS Add (4+1) [5]
SNS Duplicate (5) [5,5]
SNS Duplicate (5) [5,5,5]
SSSTTN Push 3 [5,5,5,3]
TSTT Modulo (5%3) [5,5,2]
NTSTN Jump to Label_FIZZ if 0 [5,5]
NSSSSN Create Label_RETURN_FIZZ [5,5]
SNS Duplicate (5) [5,5,5]
SNS Duplicate (5) [5,5,5,5]
SSSTSTN Push 5 [5,5,5,5,5]
TSTT Modulo (5%5) [5,5,5,0]
NTSTTN Jump to Label_BUZZ if 0 [5,5,5]
NSSTTN Create Label_BUZZ [5,5,5]
SSSTSSSSTSN Push 66 [5,5,5,66]
TNSS Print as character [5,5,5] B
SSSTTTSTSTN Push 117 [5,5,5,117]
TNSS Print as character [5,5,5] u
SSSTTTTSTSN Push 122 [5,5,5,122]
SNS Duplicate (122) [5,5,5,122,122]
TNSS Print as character [5,5,5,122] z
TNSS Print as character [5,5,5] z
NSNS Jump to Label_RETURN_FIZZ [5,5,5]
NSSSSN Create Label_RETURN_BUZZ [5,5,5]
SSSTTSSTSSN Push 100 [5,5,5,100]
TSST Subtract (5-100) [5,5,-95]
NTSTTTN Jump to Label_EXIT if 0 [5,5]
NSTTTTTN Call Label_PRINT_INT [5,5]
NSSTTTTN Create Label_PRINT_INT [5,5]
SNS Duplicate (5) [5,5,5]
SSSTTN Push 3 [5,5,5,3]
TSTT Modulo (5%3) [5,5,2]
NTSSTN Jump to Label_RETURN if 0 [5,5]
SNS Duplicate (5) [5,5,5]
SSSTSTN Push 5 [5,5,5,5]
TSTT Modulo (4%5) [5,5,0]
NTSSTN Jump to Label_RETURN if 0 [5,5]
... etc. etc.
SSSTN Push 1 [99,1]
TSSS Add (99+1) [100]
SNS Duplicate (100) [100,100]
SNS Duplicate (100) [100,100,100]
SSSTTN Push 3 [100,100,100,3]
TSTT Modulo (100%3) [100,100,1]
NTSTN Jump to Label_FIZZ if 0 [100,100]
NSSSSN Create Label_RETURN_FIZZ [100,100]
SNS Duplicate (100) [100,100,100]
SNS Duplicate (100) [100,100,100,100]
SSSTSTN Push 5 [100,100,100,100,5]
TSTT Modulo (100%5) [100,100,100,0]
NTSTTN Jump to Label_BUZZ if 0 [100,100,100]
NSSTTN Create Label_BUZZ [100,100,100]
SSSTSSSSTSN Push 66 [100,100,100,66]
TNSS Print as character [100,100,100] B
SSSTTTSTSTN Push 117 [100,100,100,117]
TNSS Print as character [100,100,100] u
SSSTTTTSTSN Push 122 [100,100,100,122]
SNS Duplicate (122) [100,100,100,122,122]
TNSS Print as character [100,100,100,122] z
TNSS Print as character [100,100,100] z
NSNS Jump to Label_RETURN_FIZZ [100,100,100]
NSSSSN Create Label_RETURN_BUZZ [100,100,100]
SSSTTSSTSSN Push 100 [100,100,100,100]
TSST Subtract (100-100) [100,100,0]
NTSTTTN Jump to Label_EXIT if 0 [100,100] error
Groovy, 63 bytes
Stand-alone program, prints result to STDOUT.
100.times{n->s=(++n%3?'':'Fizz')+(n%5?'':'Buzz')
println s?s:n}
I'd have preferred to use (1..100).each{ instead of 100.times{, so that I didn't have to ++n at the start of each iteration, but this is golf and that saves me two bytes.
Other than that, a pretty standard truthy-based submission. 'Fizz' and 'Buzz' are added when remainder is 0 because 0 is falsy, and '' is a falsy string, so I can print n when the first line hasn't had either of 'Fizz' or 'Buzz' added.
Brachylog, 48 bytes
100⟦1{f{∋15∧"FizzBuzz"|∋3∧"Fizz"|∋5∧"Buzz"|t}ẉ}m
Probably not golfed too well but it works.
Scheme, 118 bytes
(for-each(lambda(i)(printf"~a~a~%"(if(=(mod i 3)0)'Fizz"")(if(=(mod i 5)0)'Buzz(if(=(mod i 3)0)""i))))(cdr(iota 101)))
Ungolfed:
(for-each
(lambda (i)
(printf
"~a~a~%"
(if (= (mod i 3) 0) 'Fizz "")
(if (= (mod i 5) 0) 'Buzz
;; else
(if (= (mod i 3) 0) "" i))))
(cdr (iota 101)))
C, (削除) 98 (削除ここまで) 94 bytes
i;main(){for(;++i<101;)i%3*i%5?printf("%d\n",i):printf("%s%s\n",i%3?"":"Fizz",i%5?"":"Buzz");}
Pretty simple stuff...
Thanks to Yoris Fresh for helping me save a couple bytes. Thanks to Jerry Jeremiah for pointing out a mistake in the ungolfed version.
Ungolfed:
i;
main() {
for (; ++i < 101;)
i % 3 && i % 5 ?
printf("%d\n",i) :
printf("%s%s\n", i % 3 ? "" : "Fizz", i % 5 ? "" : "Buzz");
}
-
\$\begingroup\$ the ungolfed version increments i twice each loop. \$\endgroup\$Jerry Jeremiah– Jerry Jeremiah2019年06月04日 03:33:26 +00:00Commented Jun 4, 2019 at 3:33
05AB1E, 24 bytes
тÝ3Å€"Fizz"}5Å€á"ÒÖ"J}¦»
Explanation:
тÝ # range 0..100
3Å€ } # for every 3rd element...
"Fizz" # replace it with Fizz
5Å€ } # for every 5th element...
á # keep only letters...
"ÒÖ"J # and append "Buzz"
¦ # drop the first element
» # join with newlines
# implicit output
Or alternatively:
тÝ35vyÅ€á"FizzÒÖ"#NèJ]¦»
35vy iterates over the digits of 35, which avoids repeating Å€}. "FizzÒÖ" is the string Fizz Buzz, and then #Nè selects the appropriate element.
Legacy 05AB1E doesn’t have Å€, so neither of those work for it. However, legacy à (set intersection) implicitly splits numbers, which lets us get a 26 that only works on legacy:
тLεDÑ35Ãvá"Fizz ÒÖ"#yèJ},
тLε # for y in 1..100
DÑ # divisors of y
35Ã # keep only those in [3, 5]
v } # for each...
á # keep only letters
"Fizz ÒÖ"#yèJ # append either Fizz or Buzz
, # print with newline
Pip, (削除) 43 (削除ここまで) 41 bytes
(削除) Fa1,101{i:0a%3?i:1O"FIZZ"a%5?i?PaPxP"BUZZ"} (削除ここまで)
Lh{i:0o%3?i:1O"FIZZ"o%5?i?PoPxP"BUZZ"++o}
Ink, (削除) 77 (削除ここまで) (削除) 70 (削除ここまで) (削除) 57 (削除ここまで) 48 bytes
-(n){n%3:{n%5:{n}}|Fizz}{n%5: |Buzz}
{n<100:->n}
Python 3, 80 bytes
for x in range(1,101):print(("Fizz"if x%3==0else"")+("Buzz"if x%5==0else"")or x)
MC6000 ASM (Shenzhen I/O), 133 Bytes
add 1
mov acc x3
mov 51 x2
mov acc x2
mov 3 x2
teq x2 0
+mov 1 x1
mov 51 x2
mov acc x2
mov 5 x2
teq x2 0
+mov 2 x1
slp 1
mov -999 x1
Uses an MC4010 co-processor on x2, numeric display on x3, and a custom fizzbuzz display on x1.
"Hardware":
Custom LCD:
Technically doesn't follow the rules as MCxxxx don't have a STDOUT, but I did the best I could.
APL, 52 chars/bytes
generate integers from 1 to n
⍳
b is a 2 by n boolean matrix highlighting multiples of 3 and 5
b←0=3 5∘.|⍵
c is a boolean vector highlighting multiples of either 3 or 5
c←∨⌿b
format each integer into a character vector
⍕ ̈⍵
where a number is a multiple of either 3 or 5...
@{c}
...put either Fizz or Buzz (i.e. discard the last 4, the first 4 or no character at all from the string "FizzBuzz" according to b)
(↓∘'FizzBuzz' ̈ ̄4 ×ばつc/b)
transform the nested vector into a matrix
↑
-
\$\begingroup\$ tio.run/##SyzI0U2pTMzJT////… \$\endgroup\$Popov– Popov2020年01月10日 12:05:46 +00:00Commented Jan 10, 2020 at 12:05
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
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
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).
-
2\$\begingroup\$ You can replace
println!withprint!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 usingfn main(). Finally, you save some bytes using a for loop. 99 bytes: TIO \$\endgroup\$TehPers– TehPers2020年08月03日 22:15:29 +00:00Commented Aug 3, 2020 at 22:15 -
\$\begingroup\$ I have a different solution. What I did was I merged mine with yours (your logic and my loop) and got 108 bytes:
fn main(){for i in 1..101{print!("{} ",["FizzBuzz","Fizz","Buzz",&i.to_string()][1.min(i%5)+2.min(i%3*2)])}}- Try it online! \$\endgroup\$DaCuteRaccoon– DaCuteRaccoon2022年05月03日 21:29:35 +00:00Commented May 3, 2022 at 21:29 -
\$\begingroup\$ It's 94 bytes now :3 \$\endgroup\$DialFrost– DialFrost2022年11月03日 23:49:14 +00:00Commented Nov 3, 2022 at 23:49
Explore related questions
See similar questions with these tags.
Nothing can be printed to STDERR.Is this true only when running, or also when compiling (assuming that is a separate step?) \$\endgroup\$