20
\$\begingroup\$

Intro

Beauty lies in the eye of the beholder. Output lies in the choice of the compiler. There are some codes that give different outputs based on what language they are executed in. Take for instance, the code given below:

# include <stdio.h># define print(a) int main(){printf("C is better"); return 0;}print("Python is better")

When executed in C, it prints "C is better". When using a python interpreter, it prints "Python is better".

Challenge

The challenge is a modification of the FizzBuzz challenge. Write a code that gives different outputs based on language it is executed in. When executed with the first language, it prints all numbers from 1 to 1000 (both inclusive) which are not divisible by 2. If a number is divisible by 2, it outputs "FizzBuzz". When executed with the second language, if a number is not divisible by 3, it is printed. Else, the string "FizzBuzz" is printed.

Example

Output when executed in language 1 would be

1 FizzBuzz 3 FizzBuzz 5 FizzBuzz 7 FizzBuzz 9 FizzBuzz ... (upto FizzBuzz 999 FizzBuzz)

Output when executed in language 2 would be

1 2 FizzBuzz 4 5 FizzBuzz 7 8 FizzBuzz 10 11 FizzBuzz 13 14 FizzBuzz ... (upto 998 FizzBuzz 1000)

Optional Challenge

You can optionally allow the program to execute in more than 2 languages. For the ith language, every multiple of (i+1) is substituted with FizzBuzz. It isn't necessary, but at least 2 languages are compulsory.

Constraints

Need to write a fully functioning code. For instance, a method/function/procedure alone (which could not independently execute) would not be acceptable

Can use only 1 file

All outputs to be printed to standard output (not to standard error)

All other standard rules of code-golf apply

EDIT: Fixed a loophole:

No taking of user inputs during execution

Edit I got a comment saying the question wasn't clear if a non-empty separator is mandatory between the numbers. Assume it's not mandatory

Scoring

Total score = Number of bytes in the program.

Winning

Consider different participation brackets (based on number of languages being used). Person with least characters in each bracket can be considered a winner.

askedMay 12, 2020 at 12:56
NoShady420's user avatar
\$\endgroup\$
23
  • \$\begingroup\$Beauty lies in the eye of the beer holder. But, without beer, those bonus points looks rather negative? As in < 0.\$\endgroup\$CommentedMay 12, 2020 at 13:16
  • \$\begingroup\$Golfing languagesare esoteric languages, that are designed for golfing. How can you tell the difference?\$\endgroup\$CommentedMay 12, 2020 at 13:19
  • \$\begingroup\$My bad, I see now that the bonus points works as subtractions from the number of bytes.\$\endgroup\$CommentedMay 12, 2020 at 13:19
  • 2
    \$\begingroup\$I'd suggest scrapping the bonus points - that way you don't have to worry about the (artificial) distinction between esoteric/golfing/normal languages. Also solves the problem pointed out by @GammaFunction.\$\endgroup\$CommentedMay 12, 2020 at 15:46
  • 1
    \$\begingroup\$Thanks @Dingus, though that discussion didn't seem to reach a conclusion other than perhaps to say "it's up to the OP" I guess I'll let the votes decide.\$\endgroup\$CommentedMay 13, 2020 at 14:28

19 Answers19

12
\$\begingroup\$

05AB1E /05AB1E (legacy) /2sable, 27bytes

3°Lv®dтнOÌyDrÖi"FizzBuzz"},

2: Try it online in 2sable.
3: Try it online in 05AB1E.
4: Try it online in 05AB1E (legacy).

Explanation:

Let's start with a bit of history of these three languages. The development of 05AB1E started at the start of 2016 (or actually, the very first git-commit was on December 21st, 2015). This new codegolf language was being built in Python as backend. Mid 2016 2sable was branched of the 05AB1E version (July 7th, 2016 to be exact), and the strength of 2sable in comparison to that old 05AB1E version was added: implicit inputs. Later on implicit input was also added to 05AB1E, and 2sable was basically a forgotten version right after it was created on that day July 7th, 2016.Then in mid-2018, a new 05AB1E version was being started, this time completely rewritten in Elixir instead of Python, with loads of new builtins added and some builtins changed or even removed.

So, let's go over the code and see what it does in each of the three languages:

3°                          # Push 10^3: 1000 (NOTE: I'm unable to use builtin `₄` for                            # 1000, since it wasn't available in 2sable yet)  Lv                        # Loop `y` in the range [1,1000] (NOTE: I'm unable to use                            # builtin `E` for the [1,n] loop, since it wasn't available                            # in 2sable nor the legacy version yet)    ®                       #  Push -1     d                      #  2sable: check if -1 only consist of digits (falsey / 0)                            #  05AB1E (legacy): check if -1 is an integer (truthy / 1)                            #  New 05AB1E: check if -1 is non-negative ≥0 (falsey / 0)      т                     #  2sable: no-op, so does nothing                            #  05AB1E (legacy) / new 05AB1E: push 100       н                    #  Pop and push its first character                            #   2sable: does this for the 0 of the `d` falsey result                            #   05AB1E (legacy) / new 05AB1E: 100 → 1        O                   #  Sum all values on the stack:                            #   2sable: 0                            #   05AB1E (legacy): 2 (1+1)                            #   New 05AB1E: 1 (0+1)         Ì                  #  Increase it by 2                            #   2sable: 2                            #   05AB1E (legacy): 4                            #   New 05AB1E: 3          yD                #  Push the loop value `y` two times            r               #  Reverse the values on the stack             Öi          }  #  If `y` is divisible by the value we calculated earlier:               "FizzBuzz"   #   Push string "FizzBuzz"                          , #  Pop and print the top value with trailing newline

Note: theO to sum the stack will also add the previous value that was divisible (since we've duplicated it withD, but only popped and printed"FizzBuzz"). But since we know it's divisible, the increased sum in that next iteration doesn't make a difference to the divisibility check.

answeredMay 12, 2020 at 14:07
Kevin Cruijssen's user avatar
\$\endgroup\$
4
  • \$\begingroup\$you forgot to mention the score\$\endgroup\$CommentedMay 12, 2020 at 14:09
  • \$\begingroup\$@AbhayAravinda Added\$\endgroup\$CommentedMay 12, 2020 at 14:15
  • \$\begingroup\$Had to edit the question due to lot of ambiguity. Consider reading the new question. Apologies for inconvenience caused.\$\endgroup\$CommentedMay 12, 2020 at 16:10
  • \$\begingroup\$@AbhayAravinda I've removed the score\$\endgroup\$CommentedMay 12, 2020 at 17:06
12
\$\begingroup\$

Python 2/Python 3,816157 54 bytes

-20 with thanks to @KevinCruijssen

-4 with thanks to @dingledooper for the idea (prints from 1000 to 1)

-3 with thanks to @Ayxan by losing an unneededint

x=1000while x:print((x,'FizzBuzz')[x%(3/2*2)<1]);x-=1

Uses the differences of the/ operator in Python 2 and 3. In Python 23/2 is1 (integer division) while in Python 3 it is1.5.

Try it online (Python 2)!

Try it online (Python 3)!

Python 2/Python 3, 81 bytes

import sysprint([(x,'FizzBuzz')[x%sys.version_info[0]<1]for x in range(1,1001)])

Try it online (Python 2)!

Try it online (Python 3)!

Although it is longer, I am keeping the original as I think it is pretty cool the way the version numbers tie in with the the requirement for 2nd and 3rd elements :-)

answeredMay 12, 2020 at 14:25
ElPedro's user avatar
\$\endgroup\$
6
  • 1
    \$\begingroup\$int(3/2*2) instead ofsys.version_info[0] saves a couple of bytes.\$\endgroup\$CommentedMay 12, 2020 at 14:47
  • \$\begingroup\$@KevinCruijssen Thanks. I was messing about with the integer division idea then got caight up on a phone call!\$\endgroup\$CommentedMay 12, 2020 at 14:48
  • \$\begingroup\$Had to edit the question due to lot of ambiguity. Consider reading the new question. Apologies for inconvenience caused.\$\endgroup\$CommentedMay 12, 2020 at 16:11
  • \$\begingroup\$No problem. I have removed the original bonus.\$\endgroup\$CommentedMay 12, 2020 at 16:35
  • \$\begingroup\$59 bytes if you print one on each line.\$\endgroup\$CommentedMay 13, 2020 at 1:17
11
\$\begingroup\$

Fortran (GFortran)/Ruby,8079 75 bytes

print&!1.upto(1e3)do|i|puts i%3<1?"*,(i,'FizzBuzz',i=1,999,2)!"[7,8]:iend

Try it online! (Fortran),Try it online! (Ruby)

The Fortran compiler just sees

print&*,(i,'FizzBuzz',i=1,999,2)end

(! is the comment character in Fortran). The Ruby interpreter sees the full program, but we hide the otherwise invalid (Fortran) syntax at the start of the second line by wrapping it in a string.

answeredMay 12, 2020 at 14:05
Dingus's user avatar
\$\endgroup\$
6
  • \$\begingroup\$Accepted for being earliest answer.\$\endgroup\$CommentedMay 12, 2020 at 14:07
  • 16
    \$\begingroup\$"Accepted for being earliest answer." "First answer" ins't the winning criterion. Since you've specifiedcode-golf as the winning criterion, you should wait a at least a few days and accept the answer with the lowest score then.\$\endgroup\$CommentedMay 12, 2020 at 14:20
  • \$\begingroup\$@Jonah In winning, I think I clearly mentioned under early bird category that the earliest answer would be accepted.\$\endgroup\$CommentedMay 12, 2020 at 15:33
  • \$\begingroup\$Ok. Got rid of acceptance\$\endgroup\$CommentedMay 12, 2020 at 16:02
  • \$\begingroup\$Had to edit the question due to lot of ambiguity. Consider reading the new question. Apologies for inconvenience caused.\$\endgroup\$CommentedMay 12, 2020 at 16:11
8
\$\begingroup\$

Perl 4,54 48 bytes

for(;$i++<1e3;){print$i%(2+true)?$i:'FizzBuzz';}

Try it online!

PHP,54 48 bytes

for(;$i++<1e3;){print$i%(2+true)?$i:'FizzBuzz';}

Try it online!

Simple: uses PHP's auto conversion from booleantrue to integer1 while PERL doesn't

EDIT: saved 3 bytes with a leading space separator instead of a trailing one

EDIT2: saved 6 bytes by removing the separator

answeredMay 13, 2020 at 9:54
Kaddath's user avatar
\$\endgroup\$
2
  • 1
    \$\begingroup\$+1 for picking two entirely different languages and not just relying on subtle differences between versions or implementations of the same language and for not abusing comments.\$\endgroup\$CommentedMay 14, 2020 at 13:17
  • \$\begingroup\$@Philipp Thanks, it seems that in the earliest stages of PHP creation, when Rasmus Lerdorf was creating his Personal Home Page and wasn't aware yet he was creating a language, he was coding in PERL. PHP source is written from C, but the syntax similiarities with PERL can be explained by that\$\endgroup\$CommentedMay 14, 2020 at 13:33
8
\$\begingroup\$

JavaScript/PHP 5.4+,83 81 bytes

This was a somewhat simple, but fun challenge.

The code is really simple (outputs to the console in JavaScript and to stdout in PHP with-r):

for($i=0;$i<1e3;)[console.log,'printf'][+![]]("%s\n",++$i%(2+![])?$i:'FizzBuzz');

For JavaScript, outputs FizzBuzz on even numbers, while in PHP outputs in multiples of 3.


The code picks which function to call to output the value based on+![] (previously+!'0').
An empty array ([]) (previously was'0' - a string with 0) is a truthy value in JavaScript, but a falsy value in PHP.
An array is an object in JavaScript, and all objects are truthy in JavaScript.
Using this, one can do![] to detect if the code is in JavaScript (false) or PHP (true).
Since Javascript would coercefalse to a string, the+ is needed to ensure it is a numeric value.

Using this same value, one can just do2+![], resulting in3 for PHP (2+!false =2+true =3) and2 for JavaScript (2+!true =2+false =2).
That value is then used to check if it is a multiple.

The$i=0 is required because JavaScript will throw anUncaught ReferenceError: $i is not defined.

The\n in the output can't be replaced because newlines are line terminators in JavaScript, causing a syntax error if replaced with an actual newline.
Without the\n, PHP would output"12FizzBuzz45FizzBuzz[...]".
JavaScript's console ignores it just fine.

answeredMay 13, 2020 at 9:46
Ismael Miguel's user avatar
\$\endgroup\$
2
  • \$\begingroup\$+1 for picking two entirely different languages and not just relying on subtle differences between versions or implementations of the same language and for not abusing comments\$\endgroup\$CommentedMay 14, 2020 at 13:17
  • \$\begingroup\$@Philipp I think I will disappoint you :( JavaScript and PHP share a huge chunk of their syntax. Making polyglots with JavaScript and PHP is sorta easy. But thank you for the enthusiasm.\$\endgroup\$CommentedMay 14, 2020 at 13:30
5
\$\begingroup\$

Zsh+X/Bash, 58 bytes

for i in {1..1000};{ ((i%${#-}))&&echo $i||echo FizzBuzz;}

Try it online!

This uses the$- parameter, which holds some options used by the shell. By default, it is569X in Zsh, andhB in Bash. Unsetting the-X option in Zsh results in a parameter of569. Since${#-} is the length of that parameter in both Bash and Zsh, we%2 in Bash and%3 in Zsh.


Zsh/Bash, 65 bytes

a=(2 3)for i in {1..1000};{ ((i%a[1]))&&echo $i||echo FizzBuzz;}

Zsh:Try it online!Bash:Try it online!

Zsh arrays are one-indexed, Bash arrays are zero-indexed. The surrounding{ } in the loop are needed in Bash, not in Zsh.


Normally,options count as different languages. However, there is potential for abuse where the options are visible in a parameter. (Ab)Using the$- parameter in Zsh allows fora50 byte program runnable in 45 "languages" (N = 2..46)

answeredMay 12, 2020 at 14:08
GammaFunction's user avatar
\$\endgroup\$
2
  • \$\begingroup\$At first I thought you were abusing similarities (like I have done), but then Ilooked. +1\$\endgroup\$CommentedMay 12, 2020 at 14:29
  • \$\begingroup\$Had to edit the question due to lot of ambiguity. Consider reading the new question. Apologies for inconvenience caused.\$\endgroup\$CommentedMay 12, 2020 at 16:11
5
\$\begingroup\$

Perl 5,60 54 bytes

for(;$i++<1e3;){print$i%(-1**2+3+true)?$i:'FizzBuzz';}

Try it online!

PHP,60 54 bytes

for(;$i++<1e3;){print$i%(-1**2+3+true)?$i:'FizzBuzz';}

Try it online!

Perl 4,60 54 bytes

for(;$i++<1e3;){print$i%(-1**2+3+true)?$i:'FizzBuzz';}

Try it online!

Another answer, a bit longer with 3 languages!!!

still the same difference withtrue between PHP and PERL, but in PERL 5 and PHP** takes precedence over the opposite operator-, while the contrary in PERL 4

EDIT: saved 6 bytes by removing the separator

answeredMay 13, 2020 at 11:41
Kaddath's user avatar
\$\endgroup\$
5
\$\begingroup\$

R (various versions); 74,626 languages; 61 bytes

z=1:1000;z[z%%as.double(R.version$`svn rev`)==0]="fizzbuzz";z

This answer may be ruled to be illegal depending on whether different svn revisions count as different languages or not.

(Is there a more efficient way to convert text to numeric thanas.double that will work with the oldest versions of R? I feel like there is, but I cannot remember it.)

This program will continue to work as long as R continues to release versions and the number of languages will increase. I ran this using R 3.5.0.

Haven't included 74,626 TIO links for obvious reasons.here is one for a recent version of R, but it is not very interesting as it is >1000 in the list, so there are no actual instances of fizzbuzz.

answeredMay 13, 2020 at 14:09
JDL's user avatar
\$\endgroup\$
5
  • 3
    \$\begingroup\$this deserves an equivalent of the IOCCC Abuse of the rules award! :)\$\endgroup\$CommentedMay 13, 2020 at 14:53
  • \$\begingroup\$I'd say that only the first 1000 versions of R would count then, since everything after that will produce identical results given the sample range.\$\endgroup\$CommentedMay 13, 2020 at 17:20
  • \$\begingroup\$just using$sv is shorter.strtoi might work instead ofas.double?\$\endgroup\$CommentedMay 13, 2020 at 17:24
  • \$\begingroup\$I have a nagging feeling that$-completion wasn't in very early versions of R (was it in S-plus?).strtoi might have been around the whole time though — it's annoyingly hard to find documentation for ancient versions of R!\$\endgroup\$CommentedMay 13, 2020 at 17:29
  • \$\begingroup\$@Darrel there is no requirement that the outcome change, just that the solution is a valid program in the specified language(s). If the challenge had said 1 million instead of 1000, this solution would still work (with minor modifications)\$\endgroup\$CommentedMay 14, 2020 at 7:26
3
\$\begingroup\$

Befunge-98 /Befunge-93, 55 bytes

1+:.:"}"8*-!#@_5j$1+:.1+" zzuBzziF",,,,,,,,,:"}"8*-!#@_

Try it in 98!Try it in 93!

This is based on the introduction ofjump in Befunge 98. By jumping in 98 the part1+:. (add1, duplicate, print) is only executed in Befunge 93.

answeredMay 12, 2020 at 16:23
ovs's user avatar
\$\endgroup\$
3
\$\begingroup\$

C 89 (gcc)/C 99 (gcc) 64 bytes

i;main(){while(i++<1e3)printf(i%(2//**/+1)?"%d":"FizzBuzz",i);}

Try online (C 89)
Try online (C 99)

Explanation:
You can find an explanation on how this workshere.

answeredMay 13, 2020 at 22:29
Aykhan Hagverdili's user avatar
\$\endgroup\$
2
  • \$\begingroup\$The difference is that C99 recognizes// as the start of a comment, whereas C89 doesn't? Including an explanation makes the answer better!\$\endgroup\$CommentedMay 15, 2020 at 3:30
  • \$\begingroup\$@CrisLuengo Yes. I added a link to the explanation. Sorry, my first time.\$\endgroup\$CommentedMay 15, 2020 at 5:40
2
\$\begingroup\$

Io/Erlang (escript), 134 bytes

Outputs a string as a list of codepoints in Erlang. Halts with an error in Io.

1%1+1000 repeat(i,if((i+1)%2<1,"FizzBuzz",i+1)println)main(_)->io:write([if I rem 3<1->"FizzBuzz";1<2->I end||I<-lists:seq(1,1000)]).

Try it online! (in Io)Try it online! (in Erlang)

answeredMay 13, 2020 at 1:45
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 58 bytes

for(i=0;i++<1e3;)console.log(i%(2+(this>{}))?i:'FizzBuzz')

Try it online!

JavaScript (V8), 58 bytes

Try it online!

answeredMay 13, 2020 at 4:27
l4m2's user avatar
\$\endgroup\$
2
  • \$\begingroup\$Remove the +0 part for -2,for(i=0;i++<1e3;)console.log(i%(2+(this>{}))?i:'FizzBuzz').\$\endgroup\$CommentedMay 13, 2020 at 7:18
  • \$\begingroup\$@Λ̸̸ Yea forgot the part when optimizing\$\endgroup\$CommentedMay 13, 2020 at 9:41
2
\$\begingroup\$

Python 2,136133 130 bytes

Saved 3 bytes thanks toceilingcat!!!
Saved 3 bytes thanks toAbhay Aravinda!!!

#define print(a)i;main(){for(;i++<1e3;)printf(i%3?"%d":"FizzBuzz",i);}print(''.join(i%2and`i`or"FizzBuzz"for i in range(1,1001)))

Try it online!

C (gcc),136133 130 bytes

Saved 3 bytes thanks toceilingcat!!!
Saved 3 bytes thanks toAbhay Aravinda!!!

#define print(a)i;main(){for(;i++<1e3;)printf(i%3?"%d":"FizzBuzz",i);}print(''.join(i%2and`i`or"FizzBuzz"for i in range(1,1001)))

Try it online!

answeredMay 13, 2020 at 13:18
Noodle9's user avatar
\$\endgroup\$
4
  • \$\begingroup\$@ceilingcat Nice one - thanks! :-)\$\endgroup\$CommentedMay 13, 2020 at 21:34
  • \$\begingroup\$Also, for the python part print(''.join(i%2andior"FizzBuzz"for...\$\endgroup\$CommentedMay 15, 2020 at 1:43
  • \$\begingroup\$Also, based on other C answers, moving i to outside main allows i++<1e3 saving a byte\$\endgroup\$CommentedMay 15, 2020 at 2:04
  • \$\begingroup\$@AbhayAravinda Well spotted golfs - thanks! :-)\$\endgroup\$CommentedMay 15, 2020 at 8:04
1
\$\begingroup\$

Bash/Perl, 96 bytes

eval 'for i in `seq 500`;do echo $((i*2-1))FizzBuzz;done;exit';print$_%3?$_:FizzBuzz for 1..1000

This is based on an old Perl trick to get a Perl program to run as Perl, if executed as it were a shell program. If executed in either language, it takes the argument toeval, and tries to execute it (Bash) or compile, then execute it (Perl). When run as Bash, it dutifully execute the code, printing the numbers, replacing every second number with FizzBuzz, then exits. Perl, OTOH, tries to compile the string, which fails. It then carries on the execute the second statement, printing out the numbers, replacing every third with FizzBuzz.

Since non-empty separators are allowed, when executed in Bash, there will only be newlines after each FizzBuzz, while when executed in Perl, no whitespace will be outputted at all.

Try it online! (Bash)

Try it online! (Perl)

answeredMay 15, 2020 at 9:19
Abigail's user avatar
\$\endgroup\$
1
\$\begingroup\$

Perl -M5.010/C (gcc -w), 112 bytes

//;say$_*2-1,Fizzbuzz for 1..500;<<'}';main(){for(int i=1;i<1001;i++){i%3?printf("%d",i):printf("FizzBuzz");}}

Try it online! (C)

Try it online! (Perl)

This hides the Perl code behind a C++ style comment, and the C code inside a Perl here doc, using the final character of the C code as the here doc terminator. The C++ style comment marker looks like an empty regular expression to Perl, which happily executes it, to no visible effect. Just as the here doc which is in void context.

The C version does not print any whitespace, the Perl version prints a newline after eachFizzBuzz.

answeredMay 15, 2020 at 11:15
Abigail's user avatar
\$\endgroup\$
1
\$\begingroup\$

Befunge-93/Perl -M5.010,125108 Bytes

#  v            .:  <>say $_*2-1,# >:1+:3%|#  >:8555***-|             @,,,,,,,,"FizzBuzz" #<^for 1..500

Try it online!

Try it online!

This can probably easy golfed down further, but I'm far from a Befunge expert. This code cannot be separated into different pieces of code, where each language ignores the part written in the different language -- the,,,,,,,,"FizzBuzz" section is used by both Perl and Befunge.

To explain it further, what Perl sees, after removing the comments, is:

say $_*2-1,,,,,,,,,"FizzBuzz" for 1..500

and what Befunge sees is:

#  v            .:  <>             >:1+:3%|   >:8555***-|             @,,,,,,,,"FizzBuzz" #<^
answeredMay 15, 2020 at 14:13
Abigail's user avatar
\$\endgroup\$
1
\$\begingroup\$

C (gcc),132128101998482 81 bytes

main(){for(int i;i++<1e3;)__builtin_printf(i%(2+sizeof'a'%2)?"%d":"fizzbuzz",i);}

Try it online!

C++ (gcc),132128101998482 81 bytes

main(){for(int i;i++<1e3;)__builtin_printf(i%(2+sizeof'a'%2)?"%d":"fizzbuzz",i);}

Try it online!

-27 -28 bytes from ceilingcat (plus another inspired by ceiling cat)

-15 bytes from Ayxan

answeredMay 14, 2020 at 21:33
Christian Gibbons's user avatar
\$\endgroup\$
4
  • \$\begingroup\$@ceilingcat Ah, well done. I got myself so stuck on using the __cplusplus that I missed golfing it after it showed up so many times. Interesting use of the builtin printf to avoid the include for c++.\$\endgroup\$CommentedMay 14, 2020 at 23:29
  • \$\begingroup\$@ceilingcat Good idea on that one, but bit-wise not is unnecessary if you pick a different value forj on that first line.\$\endgroup\$CommentedMay 15, 2020 at 0:40
  • \$\begingroup\$84 bytes\$\endgroup\$CommentedMay 19, 2020 at 18:35
  • \$\begingroup\$@Ayxan Oh, very nice. I forgot that C++ character literals were actualchars rather thanints.\$\endgroup\$CommentedMay 19, 2020 at 18:45
1
\$\begingroup\$

JavaScript (Node.js) /Ruby /Python 2 /Python 3 /Perl 5 /PHP +-d short_open_tag=on -d output_buffering=on /Bash, 311 bytes

s=0;f='FizzBuzz';0//1;'''=;y=/.__id__;eval"s+=1;puts s%3>0?s:f;"*1e3;%q}<?ob_end_clean();$i=+1;for(;$i<1e3;){print((++$i%(6+true)?$i:FizzBuzz)."");};die;#?>";while(s<1e3)console.log(++s%2?s:f)//';for i in {1..1000};{ a=($f);echo ${a[((i%8))]:-$i};} #''';exec('s+=1;print(s if s%(5if 3/2>1else 4)else f);'*1000)

Try them all online! (truncates to the first 25 lines of each language)

I know there's no benefit to adding more languages, but I enjoyed this so thought I'd have a try at adding a few. Overall I'd like to share more of the code if possible, but the different looping structures make it pretty tricky. I'm sure there's a way I could share the Python and Ruby code to reduce some bytes, so I'll play with that more next.

JavaScript (Node.js) (with comments and unused strings removed)

s=0;f='FizzBuzz';0while(s<1e3)console.log(++s%2?s:f)

Ruby (with comments and unused strings removed)

s=0;f='FizzBuzz';0//1;'''=;y=/.__id__;eval"s+=1;puts s%3>0?s:f;"*1e3;die;

0//1;.../.__id__;: this is0//1;.../.__id__ where/1;.../ is a RegExp and the__id__ property is just a short property that exists on the RegExp object that returns a number to prevent a type error. This uses string repetition andeval as it was slightly shorter (but a true Rubyist might be able to correct me!).

Python 2 /Python 3 (with comments and unused strings removed)

s=0;f='FizzBuzz';0//1;exec('s+=1;print(s if s%(5if 3/2>1else 4)else f);'*1000)

This uses the classic floored integer division check for Python 2 vs. 3 and string repetition the same as Ruby because it was shorter (although, again, any Pythonistas please feel free to correct me!)

Perl 5 +-M5.10.0 (with unused strings removed/truncated)

s=...=...=;y=...=...=+1;for(;$i<1e3;){print((++$i%(6+true)?$i:FizzBuzz)."");};die;

This usesPerl'ss/// andy/// (tr///) operators to ignore most of the code. This is the same code as is used for PHP and works because there's notrue in Perl, so the barewordtrue evaluates to0.

PHP +-d short_open_tag=on -d output_buffering=on (with non-executed code removed)

<?ob_end_clean();$i=+1;for(;$i<1e3;){print((++$i%(6+true)?$i:FizzBuzz)."");};die;#?>

A bit cheaty, but uses a pretty well known technique of theoutput_buffering option andob_end_clean() to discard the content before the first<?, thendie;#?> before the rest of the program is parsed, basically 'hiding' the PHP program within strings and comments of the others. The majority of this code is shared with Perl.

Bash (with comments and unused strings removed)

s=0;f='FizzBuzz';0//1;for i in {1..1000};{ a=($f);echo ${a[((i%8))]:-$i};}

Because of how variables are set in Bash (var=value) we can sharef='FizzBuzz' from the JavaScript, Ruby, Python code, but it's pretty much just on its own. The useful thing with Bash is that''' isn't a syntax error, it's just concatenating an empty string and the start of a new string, so Bash pretty much just 'ignores' (executes and returns an error) all the code and it's relatively easy to find a space to drop it into.

answeredJun 4, 2020 at 19:49
Dom Hastings's user avatar
\$\endgroup\$
0
0
\$\begingroup\$

Python 2 /Python 3 /Octave, 109 bytes

This is an add-on to @ElPedro'sanswer, adding Octave to his clever solution for Python 2 and 3.

x=1000;while x>0%1:print((x,'FizzBuzz')[x%(3/2*2)<1]);x-=1;"""printf('%d FizzBuzz %d ',x,x-1)x-=3;end%"""

Try it online! (Python 2)

Try it online! (Python 3)

Try it online! (Octave)

answeredJun 9, 2020 at 22:18
Luca Citi's user avatar
\$\endgroup\$

Your Answer

More generally…

  • …Please make sure to answer the question and provide sufficient detail.

  • …Avoid asking for help, clarification or responding to other answers (use comments instead).

Draft saved
Draft discarded

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.