99 Bottles of beer, 99 languages
C, Perl, PHP, Python, Ruby
Update: I've put together a jsfiddle that helps visualize the path through the code for each of the five languages: http://jsfiddle.net/wK6bD/4/embedded/result/
An alternative version, that removes all comments, and compresses all insignificant strings, regex and replacement patterns: http://jsfiddle.net/wK6bD/5/embedded/result/
And I saved a byte while I was at it. Turns out C had an unnecessary semi-colon ;)
#/*<?php ob_clean();"
s='''*/include<stdio.h>
main($y){char*$s,$t[3],$u[3]/*';'''
def printf(a,*b):import sys;sys.stdout.write(a%b)
for y in range(99,-1,-1):
'''
99.downto(0){|y|$y=y;"/*=;#*/;
for($y=100;$y--;){
#/*"
$u=(($t=$y)+99)%100;'*/
sprintf($t,"%d",$y);sprintf($u,"%d",($y+99)%100);
#//';#'''
printf("%s bottle%s of beer on the wall, %s bottle%s of beer.\n%s",
#/* -- THIS LINE ENDS WITH CR --
y//1or'No more','s'[:y!=1],y or'no more','''s*/
+$y>0?$t:"No more",$s=$y!=1?"s":"",$y>0?$t:"no more",$s,$y>0?
#//'''[:y!=1],y and'''
"Take one down and pass it around":
#//'''[6:38]or
"Go to the store and buy some more");
printf(", %s bottle%s of beer on the wall.\n\n",
#/* -- THIS LINE ENDS WITH CR --
y//1-1and~-y%100or'no more','''s*/
+$y!=1?$u:"no more",$y!=2?"s":"");}
#/*?><?php "
%w;q(*/
}//";#'''[:y!=2])
Please Note: This file needs to be saved with mixed line endings. All lines should be terminated with LF
(char 10, a.k.a. unix-style), except for the two lines marked -- THIS LINE ENDS WITH CR --
, which should be terminated with CR
(char 13, a.k.a. mac-style). I recommend using notepad++, or other text editor which allows you to manually edit line endings.
For PHP, it is assumed that output_buffering
is enabled. According to the documentation, this is hardcoded to be Off
when using the CLI, regardless of ini
settings, so it needs to be explicitly (re)enabled:
$ php -d output_buffering=4096 bottles.pl.php.py.rb.c
This enables a 4kb buffer, which is the 'recommended' value. If you're using one of the supplied ini
s this will already be set, so it should run as-is from any web server.
The total file size is 823 bytes (not counting the comments about the line endings), an average of 164.6 bytes per language.
Versions Tested
C - gcc 4.8.1, cl 15.00
Perl - 5.16.2
PHP - 5.4.16
Python - 2.7.6, 3.3.4
Ruby - 1.8.7, 1.9.3
Output is identical for all five languages (see below).
Perl, PHP
<?php '>#'
;for($i='no more';100>($a="$i bottle".(2^($i+=1)?'s':'').' of beer');$o="
Take one down and pass it around, $b.
".ucfirst($f="$b, $a.$o")){$b="$a on the wall";}print"$f
Go to the store and buy some more, $b.";
I like this one because of how short it is, so I'm leaving it. It also demonstrates how much of a Perl-wanna-be PHP is ;)
Sample Usage:
$ perl bottles
99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.
...
2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall.
No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
JavaScript (SpiderMonkey/NodeJS), Perl, PHP, Ruby
EDIT: Added and Ruby
EDIT 2:
- Prints a title;
- Consistent output in all languages;
- No warnings.
The Code:
//#<?php
;
$i = 99;
$php = ![];
$o = 'of beer';
$ruby = 0x0 != '';
$t = $php ? '' : '//#';
$js = !$ruby && 1 + '0' == '10';
$s = 'Go to the store and buy some more, 99 bottles of beer on the wall.';
$c = 'print($t ." 99 BOTTLES OF BEER #\\\\\\\\\\n"); while($i>=0) { $j = $i; $k = ($i-1); $b = $i!=1 ? " bottles " : " bottle "; print("\\n" .($i > 0 ? $j : "No more") .$b .$o ." on the wall, " .($i > 0 ? $j : "no more") .$b .$o .".\\n" .($i > 0 ? "Take one down and pass it around, " .($i > 1 ? $k : "no more") ." bottle" .($i != 2 ? "s " : " ") .$o ." on the wall." : $s) ."\\n"); $i-=1; }';
$c = $js ? $c.replace('."\\n"','').replace(RegExp(' [.]','g'),'+').replace(RegExp('print','g'), typeof(console) != 'undefined' ? 'console.log' : 'print') : $c;
$c = $ruby ? $c.gsub('{',"do\n").gsub('}',"\nend").gsub(' .',' << ').gsub('$i;','$i.to_s;').gsub('1)','1).to_s') : $c;
eval($c);
The output:
D:\>node polyglot
//# 99 BOTTLES OF BEER #\\
99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.
* * *
2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall.
No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
C + Ruby
#define do {
#define then {
#define end }
#define def int
#define nil {
#define print(a,b) printf("%d%s", a, b)
def bottle(i)
nil
print(i, (i==1)?" bottle":" bottles");
end
i=0;
def main()
nil
i = 99;
while(i>=1) do
bottle(i);
puts(" of beer on the wall, ");
bottle(i);
puts(" of beer.\nTake one down and pass it around,");
if(i==1) then
break;
end
bottle(i-1);
puts(" of beer on the wall.");
puts("");
i-=1;
end
puts("no more bottles of beer on the wall.");
puts("");
puts("No more bottles of beer on the wall,");
puts("no more bottles of beer.");
puts("Go to the store and buy some more,");
puts("99 bottles of beer on the wall.");
end
#if 0
main
#endif
They're actually pretty similar once you normalize the syntax! :P