100
\$\begingroup\$

Write a piece of code that is executable in at least three different languages. The code must output the integer 1 in language number one, 2 in language number two, 3 in language number three etc.

Rules:

  • The output must be only the integer, but trailing newlines are OK.
  • The code can't take input of any kind
  • Different major versions of the same language are considered unique. So, your code can be executable in Python 2 and Python 3, however Python 2.5 and Python 2.7 are not considered unique.
  • You may use a new language if and only if it has an esolang/wiki article, available interpreter and documentation, and has been used by at least two users on PPCG prior to using it in this challenge. It must also adhere to these 4 rules.

The score of your submission is the number of bytes in your code divided by the number of languages it can run in cubed. So, a 54 byte solution that is executable in 3 languages will have a score of 2:

54 / 3^3 = 2  

Lower score is better.

\$\endgroup\$
10
  • 6
    \$\begingroup\$ @muddyfish You might be able to fetch the minor version within the code, which would then give you free solutions for all 2.7.1 to 2.7.n. (Actually, sweerpotato does just that with major versions.) \$\endgroup\$ – Martin Ender Dec 3 '15 at 17:04
  • 15
    \$\begingroup\$ I think maybe shortest solution by language doesn't make sense here... :P \$\endgroup\$ – FryAmTheEggman Dec 3 '15 at 20:44
  • 4
    \$\begingroup\$ @Mego Neither am I, that's why I leave sarcastic comments about the problem so that other people find out about it :P \$\endgroup\$ – FryAmTheEggman Dec 4 '15 at 3:50
  • 13
    \$\begingroup\$ I just wanted to congratulate you on how well-balanced your scoring is. When code challenges mix two quantities into one score the balance is almost always off such that the best answer will just optimise one of the scores without having to consider the other one at all. The cubic weight of the number of languages was perfect here... while it was always possible to add another language it was always a very nice (but doable) challenge to do so in the number of bytes available. :) \$\endgroup\$ – Martin Ender Dec 5 '15 at 23:49
  • 25
    \$\begingroup\$ In this PPCG challenge we've been collaboratively building a polyglot. It now has a better score on this challenge than the leading entries submitted to this challenge, but it wouldn't make sense to copy a group effort that's constantly being improved over, so I'm just dropping a mention in the comments here. \$\endgroup\$ – user62131 Mar 17 '17 at 23:56

40 Answers 40

1
2
2
\$\begingroup\$

Alphapolyglot, 8 languages, 37 bytes, score 0.072265625

8
"6";;7iiiio hkaeeeaeaeawxzz ax zaCM

Features AlphaBeta, Beatnik, Commentator, Deadfish~, evil, Foo, Golfscript, and Husk. Thanks to Leo for helping with Husk.

Explanation

AlphaBeta

8"6";;7iiiio hkaeeeaeaeawxzz a       # Garbage
                              x z    # Clear register 1 and 3
                                 a   # Add 1 to register 1
                                  C  # Set register 3 to register 1
                                   M # Output register 3 as a number

Beatnik

iiiio hkaeeeaeaeawxzz ax zaCM is 5 50 9 17.

5 50 pushes 50 (2 in ASCII) 9 outputs the top of the stack as a character. 17 exits the program.

Commentator

There are 3 spaces in the program, each one increments the active accumulator.

Deadfish~

8"6";;7iiii                          # Increment 4 times
           o                         # Output as a number
             h                       # End program 
              kaeeeaeaeawxzz ax zaCM # Garbage

evil

8"6";;7iiiio hk                      # Garbage
               aeeeaeaea             # Increment and weave the accumulator to 53 (5 in ASCII)
                        w            # Output accumulator as a character
                         xzz ax zaCM # More garbage

Foo

8                                    # Garbage
 "6"                                 # Output 6
    ;;7iiiio hkaeeeaeaeawxzz ax zaCM # Garbage

Golfscript

8                                    # Push 8
 "6"                                 # Push the string 6
    ;;                               # Pop and discard twice
      7                              # Push 7
       iiiio hkaeeeaeaeawxzz ax zaCM # Garbage

Husk

The first line is executed first.

8 # Return 8

The second line isn't executed.

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

JavaScript, HTML and PHP, 72 bytes / 3^3 = 2.67

1<!top?2:2//><!--<?='--'.'><script>document.body.innerHTML=3</script>'?>

In HTML, this will print the leading 1, ignore the <!window?2:2//> tag, and the rest is an HTML comment.

In JavaScript, it evaluates 1<!window?2:2 and outputs 2 (this must be run in a console) and the rest is a comment.

In PHP, the following is output from the server: 1<!top?2:2//><!----><script>document.body.innerHTML=3</script> which replaces HTML's 1 with 3.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to PPCG.SE! Great first post! Two things: we don't usually like REPL-based answers (the Javascript program). Additionally, the PHP code does not seem to run when I put it into an online interpreter, how exactly does the PHP work? \$\endgroup\$ – GamrCorps Dec 4 '15 at 22:56
  • \$\begingroup\$ Thanks! Well, it's technically PHP + HTML (ie. if this is the source code for code.php, visiting site/code.php in a browser displays 3. \$\endgroup\$ – cuniculus Dec 5 '15 at 0:21
  • \$\begingroup\$ Try it here: mowbl.com/se.php. \$\endgroup\$ – cuniculus Dec 5 '15 at 0:23
1
\$\begingroup\$

Python 1,2,3, Pyth, Seriously 40 bytes/5^3 = 0.32

#4X.q5
import sys
print(sys.version[0])

Python 1,2,3 prints their version

This is based on the same code by sweerpotato. The # starts a comment so the first line is not compiled.

Pyth prints 4

#4X.q starts a while loop, X does something unnecessary, and .q quits the program.

Seriously prints 5

#4X.q5 starts out with # which does nothing, then 4 is pushed to the stack, X clears the stack, .q does nothing, and 5 is pushed on the stack and an invisible character (hex code 7f) quits the program and prints what is on the stack.

\$\endgroup\$
1
  • \$\begingroup\$ that's a boring solution. \$\endgroup\$ – Sapphire_Brick Apr 2 '20 at 15:11
1
\$\begingroup\$

16 languages, 149 bytes, score 0.03637695312

1` FSR3U3%3&(()())#💬5💬➡MoOMoOMoOMoOMoOMoOOOM++++++++.$:H@@@@@@@.   {-   !*/}(10)aeeeaeeawaaaaaw> 11
🔟😉➕😨⎚14»R"15"*
print 13
[ [

WIP, I plan to add a lot more.

  1. Element
  2. Brain-Flak
  3. SimpleStack
  4. Cubically
  5. Emoji
  6. COW
  7. Agony (outputs via raw ASCII)
  8. Beam
  9. Commentator (currently broken but according to the language spec should work fine)
  10. Straw
  11. RProgN
  12. Emotinomicon
  13. S.I.L.O.S
  14. Charcoal
  15. Fission
  16. evil
\$\endgroup\$
1
\$\begingroup\$

Brainglot, score 20/4^3 = 0.3125

-[>+<-----]>(-)$-!+.

1. brainbool

Brainbool only sees [>+<]>+.. The loop never runs, and the > just moves the pointer, so it's basically just +.. The + flips the bit, and the . outputs the current bit, which is 1.

2. brainfuck

Brainfuck only sees -[>+<-----]>--+.. The -+ can be removed, which results in -[>+<-----]>-.. The part before the . is just the wrapping program for 50 on the esolangs wiki.

3. Extended Brainfuck Type I

Extended Brainfuck Type I only sees -[>+<-----]>-$-!+. The $-! can be removed, leaving -[>+<-----]>-+., where the -+ can be removed which turns it into -[>+<-----]>.. The part before the . is just the wrapping program for 51 on the esolangs wiki.

4. Brainlove

Brainlove sees the full program. The (-) and $-! can be removed, leaving -[>+<-----]>+.. The part before the . is just the wrapping program for 52 on the esolangs wiki.

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

C C++ Python Ruby, 163 bytes

Score = 163/4^3 = 2.55

#include <stdio.h>
#if 0
C = 3
""" "
C = 4;\
" """
#endif
#ifdef __cplusplus
#define A 49
#else
#define A 50
#endif
#define print(X) main(){putchar(A);}
print(C);
\$\endgroup\$
0
\$\begingroup\$

C, Python 2, Python 3, 99 bytes / 3 languages ^ 3 = 3.666677 points

Python 2 and 3 both use a version string, and the first character is the major version number. C prints 1, Python 2 prints 2, and Python 3 prints 3.

#include <stdio.h>/*
import sys
print(sys.version[0])
#*/
#define pass main(){printf("%d",1);}
pass
\$\endgroup\$
0
\$\begingroup\$

6 languages, 31 bytes, 31/6^3 = 0.1435

As soon as I add another language to it the score gets worse, here is what gave me the best result so far:

kill -l|wc|dc -e\?58%8/1+p#^[cc6

1-5: Various shells + dc

The shells see this, the rest of the line is a comment:

kill -l|wc|dc -e\?58%8/1+p

The shell's output for kill -L is different, their output gets piped to wc to count the number of bytes (N) and compute (N % 58)/8 + 1 which coincidentally gives the right result.

  1. Dash: Try it online!
  2. ksh: Try it online!
  3. tcsh: Try it online!
  4. Bash: Try it online!
  5. Zsh: Try it online!

6: Vim

The key k doesn't do anything with the buffer, i switches to insert mode and the following characters get typed. After # there's an ^[ character so Vim switches back to normal mode. cc deletes the line and switches to insert mode where we insert 6.

Here's how it looks in Vim:

inside vim

Try it online!

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

∞-ish, 12 7 bytes

B3+0-8%

Non-competing as language postdates challenge and this is rather cheap. This prints:

 B3+0-8%
 B3       set top face sum to Cubically version
   +0     add top face sum to notepad
     -8   subtract !issolved(cube) from notepad
       %  print notepad

Basically, B3 gets the cube state to this in 3x3x3:

   111
   000
   000
511222330444
511222330444
511222330444
   555
   555
   333

As you can see, the top face sum is 3. As the smallest possible cube is a 2x2x2, we have to subtract 1. In Cubically 4x4x4, the cube would look like this:

    1111
    0000
    0000
    0000
5111222233304444
5111222233304444
5111222233304444
5111222233304444
    5555
    5555
    5555
    3333
\$\endgroup\$
0
\$\begingroup\$

Javascript (spidermonkey), PHP CLI, Perl

41 bytes, 41/(3**3) = 1.52

$x="5y";print("".$x?$x=="5"?"3":"2":"1");

If you try to execute it using normal Javascript rather than spidermonkey, it will do ctrl+p.

\$\endgroup\$
0
1
2

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.