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

411 Answers 411

1
7 8
9
10 11
14
2
\$\begingroup\$

Japt -R, 27 bytes

Finally came up with a shorter way!

Lõ@"FiBu"ò úz4 ËpXv°EÑÄìªX

Test it

Lõ@"FiBu"ò úz4 ËpXv°EÑÄìªX
L                               :100
 õ                              :Range [1,L]
  @                             :Map each X
   "FiBu"                       :  Literal string
         ò                      :  Partitions of length 2
           úz4                  :  Right pad each with "z" to length 4
               Ë                :  Map each element at 0-based index E
                p               :    Repeat
                 Xv             :      Test X for divisibility by (result will be 1 or 0)
                   °E           :        Prefix increment E
                     Ñ          :        Multiply by 2
                      Ä         :        Add 1
                       Ã        :  End map
                        ¬       :  Join
                         ªX     :  Logical OR with X
                                :Implicit output, joined with newlines
\$\endgroup\$
2
\$\begingroup\$

uBASIC, 106 96 bytes

Improved syntax, and fixed a bug

0ForI=1To100:S$="":IfIMod3=0ThenS$="Fizz"
1IfIMod5=0ThenS$=S$+"Buzz"
2IfS$=""Then?I,
3?S$
4NextI

Try it online!

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

JavaScript, 69 68 bytes

-1 byte: inspired by this answer

for(i=0;++i<101;)console.log(i%3?i%5?i:"Buzz":i%5?"Fizz":"FizzBuzz")

In a readable form:

for (i = 0; ++i < 101; ) { // iterates from 1 to 100
  console.log(
    i % 3 != 0
      ? i % 5 != 0            // if i is not divisible by 3 ...
        ? i                     // ... and not divisible by 5, print i
        : "Buzz"                // ... but divisible by 5, print Buzz
      : i % 5 != 0            // if i is divisible by 3 ...
        ? "Fizz"                // ... but not divisible by 5, print Fizz
        : "FizzBuzz"            // ... and divisible by 5, print FizzBuzz
  )
}

Omit all != 0 comparison and unnecessary whitespaces.

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

Java 94 92 91 Bytes (loop without class declaration or main function) by Alex North

small optimizations by Alexander Lemieux

first the fully golfed/optimized code (the best I could do)

for(int i=0;i++<100;System.out.println(i%3<1|i%5<1?(i%3<1?"fizz":"")+(i%5<1?"buzz":""):i));

for reference here is what he posted to wiki.c2 (exactly copied and pasted from there)

for(int i = 0; i < 100; i++, System.out.println(i % 3 == 0 || i % 5 == 0 ? ((i % 3) == 0 ? "fizz" : "")

first I removed all of the spaces and changed the logical "or" to a bitwise "or" (changed || to |)

for(int i=0;i<100;i++,System.out.println(i%3==0|i%5==0?((i%3)==0?"fizz":"")+((i%5)==0?"buzz":""):i));

then I changed i%3==0 and i%5==0 to i%3<1 and i%5<1 ( I also removed unnecessary parenthesis),

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

lastly, I changed the loop from

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

to

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

how it works

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

regular java for loop

System.out.println(

print statement

(i%3<1|i%5<1?(i%3<1?"Fizz":"")+(i%5<1?"Buzz":""):i));

this is a little more complicated, it's roughly equivalent to the pseudo code:

if i is evenly divisible by 3 or 5 then if i is evenly divisible by 3 then "Fizz" + if i is evenly divisible 5 then "Buzz" else i

hope that wasn't too confusing.

THIS IS NOT MY CODE, I JUST CHANGED A FEW CHARACTERS, ALL CREDIT SHOULD GO TO ALEX NORTH!

my current best for FizzBuzz using java is 94 characters that can be found here I did this because I thought his code deserved to be shown off because it is a smart solution.

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

Retina, 47 bytes


100*
L$`_
$.>`
4,5,m`^
Buz
2,3,m`^
Fiz
z\d*
zz

Try it online! Explanation: Much simpler than @MartinEnder's approach, although I did copy his zz trick to save two bytes.


100*
L$`_
$.>`

List the numbers from 1 to 100.

4,5,m`^
Buz

Prefix Buz to every fifth number.

2,3,m`^
Fiz

Prefix Fiz to every third number.

z\d*
zz

Double the zs, and also delete the number after Fizz or Buzz.

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

Burlesque, 72 bytes

,1 100r@{JJ3.%0=="Fizz"j.*j5.%0=="Buzz"j.***Jr&0=={vv}j{jvv}jie}m[\[Sush

Try it online!

Do you see that at the end? Sus?!? Does that mean this program is the impostor!!!

This is why I need to stop fizzbuzzing at 10pm at night. I'll explain this tomorrow if I remember how it works.

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

C#, 97 bytes

for(int x=0;x++<100;)System.Console.WriteLine(x%3*x%5>0?x+"":"FizzBuzz"[x%5>0?..4:x%3>0?4..:..]);

Requires C# 9 for top level statements and range syntax.

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

Dash, with these restrictions

  • with SUSv2-compatible utilities
  • no built-in loops, no built-in branches, no variables
  • 1 pipeline
  • no awk
  • no LF in pattern space in sed
  • no hold space in sed

Dash, 103 bytes, 5 utilities.

echo ,,fizz,,buzz,fizz,,,fizz,buzz,,fizz,,,fizzbuzz|sed -n ':x
p
bx'|tr , \\n|sed '/./b
=
d'|head -n100

Try it online!

Dash, 124 121 bytes, 9 utilities.

echo xx|sed -n ':x
p
bx'|sed s/\$/fizz/|tr x \\n|paste - - - - -|sed s/\$/buzz/|tr \\t \\n|sed -n '/./p
/^$/='|head -n100

Try it online!

Dash, 133 132 bytes, 10 utilities.

echo|sed -n ':x
p
bx'|paste - - -|sed s/\$/fizz/|tr \\t \\n|paste - - - - -|sed s/\$/buzz/|tr \\t \\n|sed -n '/./p
/^$/='|head -n100

Try it online!

Originally, on GitHub Gist

With comments

echo | sed -n '
    :x
    p
    bx
' | # yes ''
paste - - - | sed s/\$/fizz/ | tr \\t \\n | # if NR%3==0; then $0="fizz";
paste - - - - - | sed s/\$/buzz/ | tr \\t \\n | # if NR%5==0; then $0=$0 "buzz";
sed -n '
    /./p
    /^$/=' | # = is to output line number
head -n 100
\$\endgroup\$
6
  • \$\begingroup\$ You might want to put some of the stuff from your header into your body, since it's a bit long. \$\endgroup\$
    – user
    Mar 19, 2021 at 13:48
  • \$\begingroup\$ program is very sus \$\endgroup\$
    – Razetime
    Mar 19, 2021 at 13:53
  • \$\begingroup\$ @Original^3VI It was my intention that I thought of such long code at first. \$\endgroup\$
    – user100411
    Mar 19, 2021 at 14:03
  • \$\begingroup\$ @tailsparkrabbitear I was talking about the Dash, xxx bytes, ... part, not your code. The headings are usually kept short-ish. \$\endgroup\$
    – user
    Mar 19, 2021 at 14:08
  • \$\begingroup\$ @Original^3 how about this? \$\endgroup\$
    – user100411
    Mar 19, 2021 at 14:14
2
+100
\$\begingroup\$

Factor, 145 142 122 bytes

As usual, @Bubbler nailed it. Go upvote one of his answers!

100 [1,b] [ dup [ 3 mod 0 = dup [ "Fizz"write ] when ] [ 5 mod 0 = dup [ "Buzz"write ] when ] bi or [ nl ] [ . ] if ] each

Try it online!

Old version:

1 [ dup 100 > ] [ dup [ 3 mod 0 = dup [ "Fizz"write ] when ] [ 5 mod 0 = dup [ "Buzz"write ] when ] bi or [ ""print ] [ dup . ] if 1 + ] until

Try it online!

Amazingly, until now no one posted a Factor answer. It is not really golfed much, probably can be improved.

\$\endgroup\$
3
  • \$\begingroup\$ Nice try. Some quick golfing: you can replace the explicit loop with a range and each, like 100 [1,b] [ ... ] each. nl is a shorter alternative to ""print. 122 bytes \$\endgroup\$
    – Bubbler
    Mar 24, 2021 at 0:17
  • \$\begingroup\$ @Bubbler Thanks. I was looking for newline word but couldn't find it. I find the wealth of words a bit overwhelming, and some things are well hidden in the help system of Factor. Is there a document with all/most of the available words somewhere? \$\endgroup\$
    – hdrz
    Mar 24, 2021 at 9:54
  • 1
    \$\begingroup\$ Everything can be found on the online docs, but it isn't easy to find the words you want either (mainly because it only searches for names, not content). \$\endgroup\$
    – Bubbler
    Mar 24, 2021 at 10:04
2
\$\begingroup\$

Kotlin, 95 bytes

fun main(){(1..100).map{i->println(when(0){i%15->"FizzBuzz";i%3->"Fizz";i%5->"Buzz";else->i})}}

Try it Online!

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

Deadfish~, 3299 bytes

{iiiii}dc{dddd}ic{iiii}c{dddd}c{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iic{dddd}ddc{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iiiiic{dddd}dddddc{iiii}iiiiiic{dddd}ddddddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiii}dcc{dddd}ic{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}dciic{dddd}dc{iiii}dciiic{dddd}ddc{iiiiii}c{iii}iiiiic{ii}dddcc{ddddd}ddddddc{iiiii}iciiiiicc{{d}}{d}ddc{iiii}dciiiiic{dddd}ddddc{iiii}dciiiiiic{dddd}dddddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}dc{i}ddc{ddddd}iiic{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}cc{dddd}c{iiii}cic{dddd}dc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiii}ciiiic{dddd}ddddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}ciiiiiic{dddd}ddddddc{iiii}c{i}dddc{ddddd}iiic{iiiiii}c{iii}iiiiic{ii}dddcc{ddddd}ddddddc{iiiii}iciiiiicc{{d}}{d}ddc{iiii}icddc{dddd}ic{iiii}icdc{dddd}c{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}icic{dddd}ddc{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iciiiic{dddd}dddddc{iiii}iciiiiic{dddd}ddddddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiii}iicdddc{dddd}ic{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iicdc{dddd}dc{iiii}iicc{dddd}ddc{iiiiii}c{iii}iiiiic{ii}dddcc{ddddd}ddddddc{iiiii}iciiiiicc{{d}}{d}ddc{iiii}iiciic{dddd}ddddc{iiii}iiciiic{dddd}dddddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iiciiiiic{ddddd}iiic{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iiicdddc{dddd}c{iiii}iiicddc{dddd}dc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiii}iiicic{dddd}ddddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iiiciiic{dddd}ddddddc{iiii}iiiciiiic{ddddd}iiic{iiiiii}c{iii}iiiiic{ii}dddcc{ddddd}ddddddc{iiiii}iciiiiicc{{d}}{d}ddc{iiii}iiiicdddddc{dddd}ic{iiii}iiiicddddc{dddd}c{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iiiicddc{dddd}ddc{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iiiicic{dddd}dddddc{iiii}iiiiciic{dddd}ddddddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiii}iiiiicddddddc{dddd}ic{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iiiiicddddc{dddd}dc{iiii}iiiiicdddc{dddd}ddc{iiiiii}c{iii}iiiiic{ii}dddcc{ddddd}ddddddc{iiiii}iciiiiicc{{d}}{d}ddc{iiii}iiiiicdc{dddd}ddddc{iiii}iiiiicc{dddd}dddddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iiiiiciic{ddddd}iiic{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iiiiiicddddddc{dddd}c{iiii}iiiiiicdddddc{dddd}dc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiii}iiiiiicddc{dddd}ddddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iiiiiicc{dddd}ddddddc{iiii}iiiiiicic{ddddd}iiic{iiiiii}c{iii}iiiiic{ii}dddcc{ddddd}ddddddc{iiiii}iciiiiicc{{d}}{d}ddc{iiiii}dddc{d}iic{dddd}ic{iiiii}dddc{d}iiic{dddd}c{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiiii}dddcdddddc{dddd}ddc{iiiii}iiiiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiiii}dddcddc{dddd}dddddc{iiiii}dddcdc{dddd}ddddddc{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiiii}iiiiiic{iiiii}iciiiiicc

Oof.

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

Branch, 98 bytes

\^//C//70/105/122Z/zc\/66/117/z/za1O[/ob3^%Vc/v?[./]b5^%Wc\w?[./]a/vbw^*/0bo^?[#0]a10.o}O/;b101^-]

Try it on the online Branch interpreter!

Explanation

The first part basically loads "Fizz" and "Buzz" along the left branches of two trees, so it looks like:

          0
         / \
        0   0
       /|  / \
      F 0 B   0
     /   /
    i   u
   /   /
  z   z
 /   /
z   z

Then, start the value at 1. Each time, load the value to the left child and 3 to the right child, modulo, move that to the left fork, and branch on condition. If it's 0, it goes to the left branch with "Fizz" and otherwise to the right branch with just 0. [./] keeps printing as a character and moving to the left child so long as the value is not 0, so this prints "Fizz" if it's a multiple of three, and nothing happens otherwise.

Then, do the same thing but with 5 and move to the "Buzz" branch. Finally, load the results of both modulos (using registers to save and move these values) and multiply them. Loading 0 to the left child and the current value into the right branch, using ? on the product will go to the 0 if either modulo was 0, and to the value otherwise. Then, [#0] keeps outputting the value and zeroing it if it is not zero; in other words, if the modulo product was 0, it skips this, and otherwise, it outputs the current value as a number once.

Finally, output a newline, increment the current value, and check if it's high enough to end the program, and if so, exit the main loop.

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

PPL, 164 bytes

declarex=0
loop100{
x=x+1
ifx%15<1{
printLine("FizzBuzz")
}
ifx%3<1&&x%5>0{
printLine("Fizz")
}
ifx%5<1&&x%3>0{
printLine("Buzz")
}
ifx%3>0&&x%5>0{
printLine(x)
}
}

Yes... 164 bytes.

The problem with my language is that it's almost impossible to golf, because:

  • the newline is the only separator
  • all variables must be declared with the declare keyword
  • no shorthands like += or %=
  • no ternary operators
  • false is the only falsey value
  • long function name: printLine
  • no bitwise and

The good thing is that because of my sloppy lexer, we can mash together expressions. You would never see ifx%15<1 or loop100 in Python, for example. [it's not a bug, it's a feature ...?]There also happens to be no documentation for PPL at the moment.

\$\endgroup\$
2
  • \$\begingroup\$ It looks just like python..... \$\endgroup\$
    – Wasif
    Apr 21, 2021 at 16:19
  • \$\begingroup\$ @Wasif you can say that :) but I didn't design it to look like python \$\endgroup\$
    – user100690
    Apr 21, 2021 at 16:20
2
\$\begingroup\$

Python 3, 59 bytes

for i in range(100):print(i%3//2*'Fizz'+i%5//4*'Buzz'or-~i)
\$\endgroup\$
2
\$\begingroup\$

Javastack, 145 bytes

100 range map 1 add duplicate 3 mod 0 equal "Fizz"swap repeat swap 5 mod 0 equal "Buzz"swap repeat swap add swap 1 add swap logicor end "\\n"join

Try it online!

-17 thanks to exedraj -22 thanks to new mod builtin

\$\endgroup\$
3
  • \$\begingroup\$ Oh, nice. It's probably cheaper to use a for loop and print, instead of trying to join by newlines, but whatever, and I'm glad I made my docs clear enough for you to understand. \$\endgroup\$
    – emanresu A
    Aug 8, 2021 at 8:53
  • \$\begingroup\$ I'll add it to DSO soonish... \$\endgroup\$
    – emanresu A
    Aug 8, 2021 at 8:53
  • \$\begingroup\$ 167 bytes: 100 range map 1 add duplicate 3 divmod 1 index 0 equal "Fizz"swap repeat swap 5 divmod 1 index 0 equal "Buzz"swap repeat swap add swap 1 add swap logicor end "\\n"join \$\endgroup\$
    – lyxal
    Aug 8, 2021 at 13:34
2
\$\begingroup\$

XML + XSLT, 1282 635 bytes

output.xml

<?xml-stylesheet type="text/xsl" href="s.xsl"?><n></n>

s.xsl

<x:stylesheet version="1.0" xmlns:x="http://www.w3.org/1999/XSL/Transform"><x:output method="text" /><x:template match="n"><x:text>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
</x:text></x:template></x:stylesheet>

Hardcoding it is shorter than doing it the fun way :(

So here's the old way for posterity

1282 bytes

output.xml

<?xml-stylesheet type="text/xsl" href="s.xsl"?><n><N>1</N><N>2</N><N>3</N><N>4</N><N>5</N><N>6</N><N>7</N><N>8</N><N>9</N><N>10</N><N>11</N><N>12</N><N>13</N><N>14</N><N>15</N><N>16</N><N>17</N><N>18</N><N>19</N><N>20</N><N>21</N><N>22</N><N>23</N><N>24</N><N>25</N><N>26</N><N>27</N><N>28</N><N>29</N><N>30</N><N>31</N><N>32</N><N>33</N><N>34</N><N>35</N><N>36</N><N>37</N><N>38</N><N>39</N><N>40</N><N>41</N><N>42</N><N>43</N><N>44</N><N>45</N><N>46</N><N>47</N><N>48</N><N>49</N><N>50</N><N>51</N><N>52</N><N>53</N><N>54</N><N>55</N><N>56</N><N>57</N><N>58</N><N>59</N><N>60</N><N>61</N><N>62</N><N>63</N><N>64</N><N>65</N><N>66</N><N>67</N><N>68</N><N>69</N><N>70</N><N>71</N><N>72</N><N>73</N><N>74</N><N>75</N><N>76</N><N>77</N><N>78</N><N>79</N><N>80</N><N>81</N><N>82</N><N>83</N><N>84</N><N>85</N><N>86</N><N>87</N><N>88</N><N>89</N><N>90</N><N>91</N><N>92</N><N>93</N><N>94</N><N>95</N><N>96</N><N>97</N><N>98</N><N>99</N><N>100</N></n>

s.xsl

<x:stylesheet version="1.0" xmlns:x="http://www.w3.org/1999/XSL/Transform"><x:output method="text" /><x:template match="n"><x:for-each select="N"><x:if test=".mod 3=0">Fizz</x:if><x:if test=".mod 5=0">Buzz</x:if><x:if test=".mod 3*.mod 5!=0"><x:value-of select="."/></x:if><x:text>&#10;</x:text></x:for-each></x:template></x:stylesheet>

W...why did I do this to myself?

Example output (using Firefox + privacy.file_unique_origin = false):

enter image description here

Explaination

The XML

XSLT/XML doesn't (afaik) have a nice easy way of generating ranges...so I have to manually place all the numbers in tags so I can use for-each later on.

The XSLT

<x:stylesheet version="1.0"
    xmlns:x="http://www.w3.org/1999/XSL/Transform"> <!-- Define the XSL namespace to be "X" for brevity -->
    <x:output method="text" /> <!-- Textual output -->
    <x:template match="n"> <!-- Match the root tag n -->
        <x:for-each select="N"> <!-- For each N, which is a number in the range 1...100 -->
            <x:if test=".mod 3=0">Fizz</x:if> <!-- If N % 3 == 0, write "Fizz" -->
            <x:if test=".mod 5=0">Buzz</x:if> <!-- If N % 5 == 0, write "Buzz" -->
            <x:if test=".mod 3*.mod 5!=0"> <!-- If N isn't divisible by either 3 or 5, write the number -->
                <x:value-of select="."/>
            </x:if>
            <x:text>&#10;</x:text> <!-- Add a newline -->
        </x:for-each>
    </x:template>
</x:stylesheet>
\$\endgroup\$
2
\$\begingroup\$

Dash and utils, 89 bytes

f(){ [ $((i%$1)) = 0 ]&&j=$2zz$j;};for i in `seq 100`;do(f 5 Bu;f 3 Fi;echo ${j-$i});done

Try it online!

As a bonus this is partially obfuscated, containing neither Fizz nor Buzz (even saved 2 bytes in doing so).

88 bytes (with caveats)

f(){(: $((0/(i%$1))))||j=$2zz$j;};for i in `seq 100`;do(f 5 Bu;f 3 Fi;echo ${j-$i});done

Try it online!

This solution was an attempt to reduce the bytes used by the conditional. In the end it saved only 1 byte, but it spams STDERR, which actually looks OK on TIO because it separates the streams. Even so I’m not sure it’s a proper solution due to the spam, but honestly I’m quite proud of the trickery involved. We could suppress the errors with a redirect but that eats up precious bytes :)

EDIT: the trickery probably bears an explanation as the mechanism may not be immediately obvious.

Most of this explanation applies to the 89 byte, the only change is the conditional.

f(){

Define function f, easy enough.

(: $((0/(i%$1))))

This is the (most) sneaky bit

( begin a subshell, use : 'true' to perform some maths $(( in which we take 0 and divide / by the result of (i%$1), end the maths )) and end the subshell ).

This whole construct returns exit code 0 when the result of i % $1 is non-zero, otherwise it returns 1.

This works because when the i % $1 operation results in a zero we get a "division by zero" error terminating the subshell and causing it to return exit code 1.

This trumps the : "true" that the maths is running behind.

In all other instances we are taking zero and dividing it by a non-zero i % $1 result, which is obviously always zero by definition but the key thing is that it doesn't error the shell (thus the : stands) and from this we can catch the "division by zero" without the whole script bombing out.

||j=$2zz$j;};

Catch the error from the subshell via || and set j= to the value of $2 appending zz (to complete our Fizzes and Buzzes) and prepending the whole thing to $j (the prepend action stems from an earlier iteration and only remains so because there's no advantage in making it an append. Even more so now I've done this explanation), then just finish up the function ;};.

NB j lives and dies inside another subshell which calls this function, and is assumed to always start empty (but we'll get to that later).

for i in `seq 100`;do

As it appears, for loop through 1-100.

(f 5 Bu;f 3 Fi;echo ${j-$i})

The second (very mildly) sneaky part -- we start a subshell ( and for every number in the sequence, we call f twice. Once each for the FizzBuzz numbers 5, 3 (to be used in the i % $1 statement) and the corresponding two prefix letters which get used in making j. We then print the value of j or, if it's empty, insert the current $i value.

When we close the subshell, j gets reset to its starting value (empty, assuming we started in a clean room).

;done

Yes, I do believe we are.

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

PARI/GP, 58 bytes

for(i=1,100,print(if(i%3<1,Fizz,i%5,i,"")if(i%5,"",Buzz)))

Attempt This Online!

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

BQN (CBQN), 48 bytes

(•Out•Fmt<○≠◶⊣‿⊢·∾"Fizz"‿"Buzz"/˜0=3‿5|⊢)¨1+↕100

Attempt This Online!

Explanation

(...)¨1+↕100
        ↕100  ⟨ 0 1 2 ... 98 99 ⟩
      1+      ⟨ 1 2 3 ... 99 100 ⟩
(...)¨        Apply this function to each:

•Out•Fmt<○≠◶⊣‿⊢·∾"Fizz"‿"Buzz"/˜0=3‿5|⊢
                                         ⊢  The argument number
                                     3‿5|   Mod 3 and mod 5, in a list
                                   0=        Is each equal to 0? (1 if so, 0 if not)
                   "Fizz"‿"Buzz"            List containing "Fizz" and "Buzz"
                                 /˜          Repeat each a number of times equal to the
                                             corresponding number from the previous step
                 ·∾                         Join that list of strings together
    •Fmt                                     Cast the initial number to a string
                                             Apply this function to the stringified number
                                             and the FizzBuzz string:
        ○≠                                     Take the length of each
       <                                       Test if the first is less than the second
          ◶                                 If the result is 0,
            ⊣                               use the stringified number
             ‿⊢                             but if it's 1, use the FizzBuzz string
•Out                                        Print that string with a newline
\$\endgroup\$
2
\$\begingroup\$

Fig, \$25\log_{256}(96)\approx\$ 20.578 bytes

#jeFcdn3n5a@2'D&=g"'+D&=_

Try it online!

Take that, Lyxal. Not so bad now, is it? Only 0.5 bytes longer than Jelly, and 3.5 longer than legit Vyxal.

#jeFcdn3n5a@2'D&=g"'+D&=_
          a@2             # The range [1, 10^2]
        n5   '            # For every 5th item
              D&=g"       # Replace with the compressed string for "Buzz"
      n3           '      # For every 3rd item
                    +     # Prepend
                     D&=_ # The compressed string "Fizz"
  eF                      # Filter out
    cd                    # Digits
#j                        # Join on newlines
\$\endgroup\$
2
  • \$\begingroup\$ You should at least have a two-byte constant for 100, if not one byte \$\endgroup\$
    – naffetS
    Sep 8, 2022 at 18:49
  • \$\begingroup\$ @Steffan an earlier version of Fig had the @ overload for numbers be ten to the power of, so @2 would have worked. Lemme reimplement that... \$\endgroup\$
    – Seggan
    Sep 8, 2022 at 18:51
2
\$\begingroup\$

Pure Bash, 62+1=63 bytes

Filename is x, which is 1-byte penalty. Outgolfed user100411's answer by one byte.

!
((++x%3))||Fizz
((x%5))||$_\Buzz
echo ${_:-$x}
((x>99))||. x

Try it online!

How it works

!                # invoke no command so $_ (last command's last argument) is reset
                 # First time: $_ was random string
((++x%3))||Fizz  # increase x. if x mod 5 is zero try to invoke Fizz
                 # if invoked $_ is Fizz
                 # ((expression)) is not a command
((x%5))||$_\Buzz # like previous but prefix $_
echo ${_:-$x}    # try to output $_ but if $_ is empty output $x
((x>99))||. x    # load myself if x is less than 100
                 # . x does not change $_
\$\endgroup\$
2
\$\begingroup\$

Terse, 22 bytes

电让一3我5u"Fizz‘Buzz"死开】而

Try it here

A golfing language made by me.

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

Python, 580 bytes

Here's a version that pre-computes the FizzBuzz table and then just returns a slice, which would be useful if you're running a worldwide FizzBuzz service. It'd be more interesting in a language that has slices as a first-class construct. Either way, a very unoptimized implementation:

from typing import List

class FizzBuzz:
    def __init__(self, max_n=10**4):
        self.max_n = max_n
        self.data = [i for i in range(0, self.max_n)]
        for r in range(0, self.max_n, 3):
            self.data[r] = "Fizz"
        for r in range(0, self.max_n, 5):
            self.data[r] = "Buzz"
        for r in range(0, self.max_n, 15):
            self.data[r] = "FizzBuzz"

    def fizzBuzz(self, n: int) -> List[str]:
        if n > self.max_n or n < 1:
            return None  # Out of range, not permitted
        return self.data[1:n]

s = FizzBuzz()
print(s.fizzBuzz(100))
```
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf! This site is for competitive challenges, not general programming questions.This challenge has minimum code size as its goal, and isn't just asking for answerers to create a FizzBuzz program (however interesting it is) unless some effort has been made to optimize its size. \$\endgroup\$ Dec 4, 2022 at 2:51
2
\$\begingroup\$

Functional(), 314 310 288 277 273 269 bytes

0,1,=,:,$,&,$(X,& >($(I,T(>(a,b,c,d),b d(0,I)(= a,a(=,1)b,a b(=,1)c,a b(c,d))))1))(:(T,&(a,b,c,d))(X(T(b(d,a c)($(S,T(W()1 b()0()1()1 0(= b)b(= b)>()0 1()> >()0 1()> > 0,0)),=(:(t,t(S,1)))(0,&()(u()>()0,:(W,T(>(a 1),W))a b c d >()0)))0,W()1()1()0()0)),:(u,&()(W a b c d

Try it online!
Try the 273B version online!
Try the 277B version online!
Try the 288B version online!
Try the 310B version online!
Try the 314B version online!

in a more readable form ( ommited last )s are added ):

0,1,=,:,$,&,
$(X,& >(
  $(I,T(
    >(a,b,c,d),
    b d (0,I)(= a,a(=,1)b,a b(=,1)c,a b(c,d))
  )) 1
))(
 :(T,&(a,b,c,d))(
  X(
   T(
    b(d,a c)(
     $(S,T(
       W() 1 b () 0 () 1 () 1 0 (= b) b (= b) > () 0 1 () > > () 0 1 () > > 0,
       0
     )),
     =(
      :(t,t(S,1))
     )(
      0,
      &()(
       u() > () 0,
       :(W,T(>(a 1),W)) a b c d > () 0
      )
     )
    ) 0,
    W() 1 () 1 () 0 () 0
   )
  ),
  :(u,&()(W a b c d))
 )
)

explanation

The key of this code is a executer func "X" like a pseudo Python code below;

def executer(f):
  def incrementer(a,b=0,c=0,d=0):
    f(a,b,c,d)
    nextf = nullf if b==1 and d==1 else incrementer
    # nullf is a function to do nothing
    nextf(1^a,a^b,(a&b)^c,(a&b&c)|d)

  incrementer(1)

Then, this realize a 10-length loop to evaluate f(1,0,0,0), f(0,1,0,0), f(1,1,0,0), ... , f(0,1,0,1) in this order. The parameters a,b,c,d describe 4 bits of 1 thru 10 ( the lowest bit first ).

Thus, the rough structure of this code is a double loop with "X" like below;

X(
 T(
  X(
   T(
    ** main part
    * print Fizz or Buzz or FizzBuzz or the sequence number
    * and print a newline
   )
  ),
  ** post process
  * re-assign "u", a func to print the upper digit, for the next iteration
 )
)

"T" is a "function template" and acts like a synonym of "&(a,b,c,d)" ( a user function template with 4 parameters a,b,c,d ) due to the assignment with ":" at the 8th line.

In addition to "X" and "T", I implemented "W" and "S" func for output.
"W" is for bit-sequence output. "W" returns "W" itself after printing bits so that "W 0 1 0 1 0 0 0 0" print a bit sequence ( a newline in this case ) continuously for example. And "W" takes a parameter as a bit to print, but is improved, for compressing bit streams, to print two bits of 11 at a time with ">" .
"S" is for "Fizz" ( against parameter (x,1) ) or "Buzz" ( against parameters (x,0) ) output with "W".

Other function "u" and "t" are variable over the double loop.
"u" is for printing the lower 4 bits of the upper digits, and is re-assigned in the post process of the outer loop.
"t" acts as a 3 state register, which holds 0, 1 and "S", and is re-assigned with ":(t,t(S,1))" on every loop iteration. When "t" is 0 or 1, it is re-assigned with 1 or "S" respectively. And when "t" is "S", it is re-assigned with 0 as a return value of "S" after printing "Fizz".

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

Miled, 143127 bytes

I don't know if a language in alpha is allowed, but here it is.

-16 by myself. Yes. I was dumb.

join "\n" map fn cS "x" <: if:: div 15 x "FizzBuzz" else if:: div 3 x "Fizz" else if:: div 5 x "Buzz" else ->str x :> ..= 1 100

TIO (read: Try It Offline)

A little note on this language: it's created by me. The current release is the first release of the language and it's still in alpha, so expect bugs. I haven't encountered any yet, so I think, maybe the best way to test it is to make more people use it! So if you find bugs, please report it. And I'm just a hobbyist with few coding experiences, so please give me advices. Thanks!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ languages in alpha/heavy development are definitely allowed - the implementation defines the language, so the language here is Miled (alpha) (or whatever versioning system you choose). I submitted a fizzbuzz for a in-dev alpha esolang once and no one was upset that it was still in development, so people won't be upset with this answer too. You can also always update this answer as later versions of Miled are released. Anyhow, this esolang looks really cool and I look forward to seeing how it turns out! :) \$\endgroup\$
    – lyxal
    Dec 13, 2022 at 11:40
2
\$\begingroup\$

Thunno N, \$35\log_{256}(95)\$ ≈ 28.80921 bytes

aSR1+eD3 5ZP%0="FizzBuzz"2APz*JD!?K

Attempt This Online! (not yet though)

Thought I'd try my usual approach in Thunno to see how it turned out.

Explained

aSR1+eD3 5ZP%0="FizzBuzz"2APz*JD!?K
aSR1+                               # The range [1, 100]
     e                              # To each item:
      D                             #   Duplicate it
       3 5ZP%                       #   [% 3, % 5]
             0=                     #   == 0
               "FizzBuzz"2AP        #   ["Fizz", "Buzz"]
                            z*      #   repeated element-wise
                              J     #   "".join
                               D!?K #   if that's empty, leave the original number
# The N flag, of course, joins the output on newlines
\$\endgroup\$
0
2
\$\begingroup\$

jq -nr, 43 bytes

Exploiting that out-of-bounds array access evaluates to null. Works with jq 1.5+

range(100)+1|["Fizz"][.%3]+["Buzz"][.%5]//.

Try it online!

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

Desmos, 244 bytes

a=\left[1...100\right]
b\left(m\right)=\operatorname{mod}\left(a,m\right)
c\left(n\right)=\left\{b\left(n\right)=0:1,0\right\}
\left(a,1\right)Fizzc\left(3\right)
\left(a,0\right)Buzzc\left(5\right)
\left(a,0\right)${a}b\left(3\right)b\left(5\right)

Ungolfed

  • a=\left[1...100\right] sets a to be [1,2,3,4...100]
  • b\left(m\right)=\operatorname{mod}\left(a,m\right) b(m)=mod(a,m)
  • c\left(n\right)=\left\{b\left(n\right)=0:1,0\right\}if b(n)=0,output 1,otherwise 0
  • \left(a,1\right)Fizzc\left(3\right)This is just points at (a,1) labeled with fizz, and opacity c(3)
  • \left(a,0\right)Buzzc\left(5\right) Same with Buzz
  • \left(a,0\right)${a}b\left(3\right)b\left(5\right) Points at (a,0) with label a, opacity b(3)b(5)
\$\endgroup\$
2
\$\begingroup\$

SAS 4GL, 84 (or 81 or 72 or 63) 100 (or 89? ) 110 bytes (or 104? or 88?) 133 bytes (or 120? or 111?) 142 bytes (or 129? or 120?) (out of game 82)

  1. The code (short)
data;do n=1to 1e2;x=scan('Fizz FizzBuzz Buzz'!!n,mod(gcd(n,15),11)-2);put x;end;run;

  1. The "human readable" code
data;
  do n = 1 to 1e2;
    x = scan('Fizz FizzBuzz Buzz'!!n, mod(gcd(n, 15), 11) - 2);
    put x;
  end;
run;

Explanation (for n=20):

data;
  do n = 1 to 20;
    g = gcd(n, 15);
    m = mod(g, 11) - 2;
    y = 'Fizz FizzBuzz Buzz' !! n;
    x = scan(y, m);
    put n= @10 g= @20 m= @30 y= @70 x=;
  end;
run;

Variables:

  • g from greatest common divisor function gets : 1,3,5,15,
  • m from modulo function gets: -1,1,3,2,
  • y gets: "Fizz FizzBuzz Buzz          n" for n,
  • x value is scanned from y on position m (negative m means "scan backward").

SAS Log printout:

n=1      g=1       m=-1      y=Fizz FizzBuzz Buzz           1        x=1
n=2      g=1       m=-1      y=Fizz FizzBuzz Buzz           2        x=2
n=3      g=3       m=1       y=Fizz FizzBuzz Buzz           3        x=Fizz
n=4      g=1       m=-1      y=Fizz FizzBuzz Buzz           4        x=4
n=5      g=5       m=3       y=Fizz FizzBuzz Buzz           5        x=Buzz
n=6      g=3       m=1       y=Fizz FizzBuzz Buzz           6        x=Fizz
n=7      g=1       m=-1      y=Fizz FizzBuzz Buzz           7        x=7
n=8      g=1       m=-1      y=Fizz FizzBuzz Buzz           8        x=8
n=9      g=3       m=1       y=Fizz FizzBuzz Buzz           9        x=Fizz
n=10     g=5       m=3       y=Fizz FizzBuzz Buzz          10        x=Buzz
n=11     g=1       m=-1      y=Fizz FizzBuzz Buzz          11        x=11
n=12     g=3       m=1       y=Fizz FizzBuzz Buzz          12        x=Fizz
n=13     g=1       m=-1      y=Fizz FizzBuzz Buzz          13        x=13
n=14     g=1       m=-1      y=Fizz FizzBuzz Buzz          14        x=14
n=15     g=15      m=2       y=Fizz FizzBuzz Buzz          15        x=FizzBuzz
n=16     g=1       m=-1      y=Fizz FizzBuzz Buzz          16        x=16
n=17     g=1       m=-1      y=Fizz FizzBuzz Buzz          17        x=17
n=18     g=3       m=1       y=Fizz FizzBuzz Buzz          18        x=Fizz
n=19     g=1       m=-1      y=Fizz FizzBuzz Buzz          19        x=19
n=20     g=5       m=3       y=Fizz FizzBuzz Buzz          20        x=Buzz

Remarks:

  • if you decide to uses F, B, and FB for Fizz, Buzz, and FizzBuzz it can be shortened to:
data;do n=1to 1e2;x=scan('F FB B'!!n,mod(gcd(n,15),11)-2);put x;end;run;

so 72, right? ;-)

  • The data; and run; are mandatory keywords of every SAS 4GL data step, so if you thinking only about the "working code" (and have in mind the previous remark) you could say 63 bytes, right? ;-) ;-)

Observation

I dare to say that "FizzBuzz" is equivalent with "BuzzFizz", after all if a number is divisible by "3 and 5" it is also divisible by "5 and 3", isn't it? In such case it can be golfed to 81 bytes:

data;
  do n = 1 to 1e2;
    t='Fizz Buzz';
    x = scan(t!!t!!n, mod(gcd(n, 15), 11) - 2);
    put x;
  end;
run;

Until I get confirmation I'll keep it unofficial.



The 4th obsolete solution.

  1. The code (short)
data;do n=1to 1e2;f=^mod(n,3);b=^mod(n,5);put n@(4*^(f+b))"Fizz"+(-4*^f)"Buzz"+(-4*^b)4*" ";end;run;

  1. The "human readable" code

The Code:

data;
  do n=1to 1e2;

    f=^mod(n,3);
    b=^mod(n,5);

    put n@(4*^(f+b))"Fizz"+(-4*^f)"Buzz"+(-4*^b)4*" ";

  end;
run;

Process:

  • print n and overprint it with Fizz, Buzz or FizzBuzz if divisibility occurs by shifting text with @() operator.

Remarks:

  • if you decide to uses F, B for Fizz and Buzz it can be shortened to:
data;do n=1to 1e2;f=^mod(n,3);b=^mod(n,5);put n@(4*^(f+b))"F"+(-^f)"B"+(-^b)"  ";end;run;

so 89, right? ;-) ;-)


"Out-of-game 82"

This example "breaks" the rule because it uses F, B and X instead full words, that is why I only puts it as a interesting case. It is a variation on the cycle string idea. And I like it the most :-)

The code:

data;do n=1 to 100;x=coalescec(char("X  F BF  BF B",mod(n,15)+1),n);put x;end;run;

Human readable:

data;
  do n = 1 to 100;
    x = coalescec(char("X  F BF  BF B", mod(n, 15) + 1), n);
    put x;
  end;
run;

If you will to create a SAS format:

proc format;
 value $ X (default=20)
 "F" = "Fizz"
 "B" = "Buzz"
 "X" = "FizzBuzz"
 ;
run;

and modify the put statement to:

put x $X.;

you will see "full words" in the log.



The 3rd obsolete solution.

  1. The code (short)
data;do n=1to 100;x=ifc(mod(n,5)*mod(n,3),n,ifc(^mod(n,5),"Fizz","")!!ifc(^mod(n,3),"Buzz",""));put x;end;run;

  1. The "human readable" code

The Code:

data;
  do n = 1 to 100;
    x =                       /* create variable x and... */
        ifc(mod(n,5)*mod(n,3) /* if n not divisible by 3 or 5 */
            ,n                /* assign n to x, else... */
            ,ifc(^mod(n,5)    /* if divisible by 5 add Fizz string to x */
                 ,"Fizz"
                 ,"")
             !!               /* and concatenate with... */
             ifc(^mod(n,3)    /* if divisible by 5 add Buzz string */
                 ,"Buzz"
                 ,"")
             );
    put x ;                   /* print value from x variable */
  end;
run;

Comments:

  • It looks like "concatenation" of four mod(.,.) functions and three ifc() functions do the job even better.

Remarks:

  • If the Fizz and Buzz phrases could be "replaced" by F and B the code would be 6 bytes shorter, that's why I wrote 104 in brackets. :-)
  • The data; and run; are mandatory keywords of every SAS 4GL data step, so if you thinking only about the "working code" (and have in mind the previous remark) you could say 95 bytes, right? ;-) ;-)


The 2nd obsolete solution.

  1. The code (short)
data;do n=1to 100;select;when(^mod(n,15))put"FizzBuzz";when(^mod(n,5))put"Fizz";when(^mod(n,3))put"Buzz";otherwise put n;end;end;run;

  1. The "human readable" code

The Code:

data;
  do n = 1 to 100;
    x = ifc(mod(n,5)*mod(n,3)
            ,n
            ,ifc(^mod(n,5)
                 ,"Fizz"
                 ,"")
             !!
             ifc(^mod(n,3)
                 ,"Buzz"
                 ,"")
             );
    put x ;
  end;
run;

Comments:

  • It looks like doing 3 times "negated modulo" (^mod(.,.)) takes less keystrokes than previous "cycle string" approach.

  • Remarks from the previous one stay, but with 120 and 111 accordingly. ;-) ;-)



The 1st obsolete solution.

  1. The code (short)
data;do n=1to 100;select(char("300102100120100",mod(n,15)+1));when(0)put n;when(1)put"Fizz";when(2)put"Buzz";when(3)put"FizzBuzz";end;end;run;

  1. The "human readable" code "step by step"

The Code:

data;
  do n = 1 to 100;

    select( char( "300102100120100", mod(n, 15) + 1) );
      when(0) put n;
      when(1) put "Fizz";
      when(2) put "Buzz";
      when(3) put "FizzBuzz";
    end;

  end;
run;

The process:

  • the do n = 1 to 100; makes the loop from 1 to 100, so if you want to play FizzBuzz for 1000 it is just 1 extra byte cost,
  • "300102100120100" - the "cycle string" - when you realise the divisibility by 3 or 5 is cyclic modulo 15 you will see all "cases" repeating in a loop; you just have to put the last marker (3 = divisible by 3 and 5) as the first on the list (since 15 modulo 15 is 0).
  • char( "300102100120100", mod(n, 15) + 1) select "n-th modulo 15, plus 1" character from the "cycle string", do the test in when(), and print with put.

Remarks:

  • If the Fizz, Buzz, and FizzBuzz phrases could be "replaced" by F, B, and X the code would be 13 bytes shorter, that's why I wrote 129 in brackets. :-)
  • The data; and run; are mandatory keywords of every SAS 4GL data step, so if you thinking only about the "working code" (and have in mind the previous remark) you could say 120 bytes, right? ;-) ;-)
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 73 bytes

for(i=s='';i++<100;s+=((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)+"\n");alert(s)
\$\endgroup\$
0
1
7 8
9
10 11
14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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