103
\$\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\$ Commented Dec 3, 2015 at 17:04
  • 15
    \$\begingroup\$ I think maybe shortest solution by language doesn't make sense here... :P \$\endgroup\$ Commented Dec 3, 2015 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\$ Commented Dec 4, 2015 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\$ Commented Dec 5, 2015 at 23:49
  • 26
    \$\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\$ Commented Mar 17, 2017 at 23:56

41 Answers 41

1
2
2
\$\begingroup\$

Jelly / 05AB1E / Whitespace, 14 bytes, score \$0.\overline{518}\$

[S][S][S][T][T][LF] [T][LF] [S][T]1o 

[S], [T], and [LF] added for visual clarity only.

I was going to add naz to the chain, but I guess it doesn't handle whitespace very well.

Jelly explanation

[S][S][S][T][T][LF] [T][LF] [S][T] 1 # Literal 1 o # ...after which the result is output implicitly 

Try it online!

05AB1E explanation

[S][S][S][T][T][LF] [T][LF] [S][T] 1 # Push 1 o # Push 2 ** 1 # ...after which the result is output implicitly 

Try it online!

Whitespace explanation

[S][S][S][T][T][LF] # Push 3 to the stack [T][LF][S][T] # Output the top of the stack 1o # Ignored 

Try it online!

I'm not very good with these languages — if you spot anything wrong with these explanations, please feel free to improve this post!

\$\endgroup\$
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\$ Commented Dec 4, 2015 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\$ Commented Dec 5, 2015 at 0:21
  • \$\begingroup\$ Try it here: mowbl.com/se.php. \$\endgroup\$ Commented Dec 5, 2015 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\$ Commented Apr 2, 2020 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\$
1
\$\begingroup\$

not so much 6 "languages" as peculiarities between 6 ways of using awk

for __ in 'nawk' 'gawk -M' 'mawk1' 'mawk2' 'gawk -b' 'gawk'; do $( printf '%s' "$__" ) 'BEGIN { print match(sprintf("%x%u%c%x%c", ((_+=++_)^(_+=_))^(_^=_),-!!_,_,_,!_),/$/)%25%13%7 }' done 

1 2 3 4 5 6 

nawk 20200816 gawk 5.2.2 (GMP mode) mawk 1.3.4 20230808 mawk 2 gawk 5.2.2 (byte mode) gawk 5.2.2 (UTF8 mode) 
\$\endgroup\$
2
  • \$\begingroup\$ You could replace $( printf '%s' "$__" ) with $__. Or if you worry you could do IFS=$' \t\n' $__ because $'\t' or similar strings that starts with $' is now on POSIX issue 8. \$\endgroup\$ Commented Jun 18, 2024 at 10:35
  • \$\begingroup\$ @鳴神裁四点一号 : that means i need to count the extra bytes for setting the IFS, which negates most, if not all, the savings. 真的半点都省不到 \$\endgroup\$ Commented Jun 19, 2024 at 1:33
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.