19
\$\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.

\$\endgroup\$
  • \$\begingroup\$ Beauty lies in the eye of the beer holder. But, without beer, those bonus points looks rather negative? As in < 0. \$\endgroup\$ – Kjetil S. May 12 at 13:16
  • \$\begingroup\$ Golfing languages are esoteric languages, that are designed for golfing. How can you tell the difference? \$\endgroup\$ – Λ̸̸ May 12 at 13:19
  • \$\begingroup\$ My bad, I see now that the bonus points works as subtractions from the number of bytes. \$\endgroup\$ – Kjetil S. May 12 at 13:19
  • \$\begingroup\$ @Λ̸̸ esoteric languages which were built specifically for golfing are golfing languages. The rest are esoteric \$\endgroup\$ – Abhay Aravinda May 12 at 13:27
  • 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\$ – Dingus May 12 at 15:46

17 Answers 17

13
\$\begingroup\$

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

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: the O to sum the stack will also add the previous value that was divisible (since we've duplicated it with D, 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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ you forgot to mention the score \$\endgroup\$ – Abhay Aravinda May 12 at 14:09
  • \$\begingroup\$ @AbhayAravinda Added \$\endgroup\$ – Kevin Cruijssen May 12 at 14:15
  • \$\begingroup\$ Had to edit the question due to lot of ambiguity. Consider reading the new question. Apologies for inconvenience caused. \$\endgroup\$ – Abhay Aravinda May 12 at 16:10
  • \$\begingroup\$ @AbhayAravinda I've removed the score \$\endgroup\$ – Kevin Cruijssen May 12 at 17:06
12
\$\begingroup\$

Fortran (GFortran)/Ruby, 80 79 75 bytes

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

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.

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

Python 2/Python 3, 81 61 57 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 unneeded int

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

Uses the differences of the / operator in Python 2 and 3. In Python 2 3/2 is 1 (integer division) while in Python 3 it is 1.5.

Try it online (Python 2)!

Try it online (Python 3)!

Python 2/Python 3, 81 bytes

import sys
print([(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 :-)

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ int(3/2*2) instead of sys.version_info[0] saves a couple of bytes. \$\endgroup\$ – Kevin Cruijssen May 12 at 14:47
  • \$\begingroup\$ @KevinCruijssen Thanks. I was messing about with the integer division idea then got caight up on a phone call! \$\endgroup\$ – ElPedro May 12 at 14:48
  • \$\begingroup\$ Had to edit the question due to lot of ambiguity. Consider reading the new question. Apologies for inconvenience caused. \$\endgroup\$ – Abhay Aravinda May 12 at 16:11
  • \$\begingroup\$ No problem. I have removed the original bonus. \$\endgroup\$ – ElPedro May 12 at 16:35
  • \$\begingroup\$ 59 bytes if you print one on each line. \$\endgroup\$ – dingledooper May 13 at 1:17
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 boolean true to integer 1 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

| improve this answer | |
\$\endgroup\$
  • 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\$ – Philipp May 14 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\$ – Kaddath May 14 at 13:33
7
\$\begingroup\$

JavaScript/PHP 5.4+, 83 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'][+!'0']("%s\n",++$i%(2+!'0')?$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 +!'0'.
The string '0' is a truthy value in JavaScript, but a falsy value in PHP.
Using this, one can do !'0' to detect if the code is in JavaScript (false) or PHP (true).
Since Javascript would coerce false to a string, the + is needed to ensure it is a numeric value.

Using this same value, one can just do 2+!'0', resulting in 3 for PHP (2+!false = 2+true = 3) and 2 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 an Uncaught 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.

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Philipp May 14 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\$ – Ismael Miguel May 14 at 13:30
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 than as.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.

| improve this answer | |
\$\endgroup\$
  • 3
    \$\begingroup\$ this deserves an equivalent of the IOCCC Abuse of the rules award! :) \$\endgroup\$ – SeanC May 13 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\$ – Darrel Hoffman May 13 at 17:20
  • \$\begingroup\$ just using $sv is shorter. strtoi might work instead of as.double? \$\endgroup\$ – Giuseppe May 13 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\$ – JDL May 13 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\$ – JDL May 14 at 7:26
4
\$\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 is 569X in Zsh, and hB in Bash. Unsetting the -X option in Zsh results in a parameter of 569. 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 for a 50 byte program runnable in 45 "languages" (N = 2..46)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ At first I thought you were abusing similarities (like I have done), but then I looked. +1 \$\endgroup\$ – Wezl May 12 at 14:29
  • \$\begingroup\$ Had to edit the question due to lot of ambiguity. Consider reading the new question. Apologies for inconvenience caused. \$\endgroup\$ – Abhay Aravinda May 12 at 16:11
4
\$\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 with true 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

| improve this answer | |
\$\endgroup\$
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 of jump in Befunge 98. By jumping in 98 the part 1+:. (add 1, duplicate, print) is only executed in Befunge 93.

| improve this answer | |
\$\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 works here.

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Cris Luengo May 15 at 3:30
  • \$\begingroup\$ @CrisLuengo Yes. I added a link to the explanation. Sorry, my first time. \$\endgroup\$ – Ayxan May 15 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)

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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Remove the +0 part for -2, for(i=0;i++<1e3;)console.log(i%(2+(this>{}))?i:'FizzBuzz'). \$\endgroup\$ – Λ̸̸ May 13 at 7:18
  • \$\begingroup\$ @Λ̸̸ Yea forgot the part when optimizing \$\endgroup\$ – l4m2 May 13 at 9:41
2
\$\begingroup\$

Python 2, 136 133 130 bytes

Saved 3 bytes thanks to ceilingcat!!!
Saved 3 bytes thanks to Abhay 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), 136 133 130 bytes

Saved 3 bytes thanks to ceilingcat!!!
Saved 3 bytes thanks to Abhay 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!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @ceilingcat Nice one - thanks! :-) \$\endgroup\$ – Noodle9 May 13 at 21:34
  • \$\begingroup\$ Also, for the python part print(''.join(i%2andior"FizzBuzz"for... \$\endgroup\$ – Abhay Aravinda May 15 at 1:43
  • \$\begingroup\$ Also, based on other C answers, moving i to outside main allows i++<1e3 saving a byte \$\endgroup\$ – Abhay Aravinda May 15 at 2:04
  • \$\begingroup\$ @AbhayAravinda Well spotted golfs - thanks! :-) \$\endgroup\$ – Noodle9 May 15 at 8:04
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" #<^
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 132 128 101 99 84 82 81 bytes

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

Try it online!

C++ (gcc), 132 128 101 99 84 82 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

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Christian Gibbons May 14 at 23:29
  • \$\begingroup\$ @ceilingcat Good idea on that one, but bit-wise not is unnecessary if you pick a different value for j on that first line. \$\endgroup\$ – Christian Gibbons May 15 at 0:40
  • \$\begingroup\$ 84 bytes \$\endgroup\$ – Ayxan May 19 at 18:35
  • \$\begingroup\$ @Ayxan Oh, very nice. I forgot that C++ character literals were actual chars rather than ints. \$\endgroup\$ – Christian Gibbons May 19 at 18:45
0
\$\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 to eval, 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)

| improve this answer | |
\$\endgroup\$
0
\$\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 each FizzBuzz.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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