Print the Golden Ratio
PHP, 100 digits
I'm probably bending the rules a bit here, but PHP has dozens of constants to choose from:
<?php
echo TRUE . '.' . PM_STR . DNS_A . MSG_EOR . LC_ALL . T_FMT . LOCK_UN . SQL_DATE
. E_NOTICE . IMG_WBMP . INI_ALL . E_PARSE . SOCKET_EBADF . LOG_USER .
IMAGETYPE_JPC . IMG_PNG . GLOB_MARK . LOCK_NB . LOG_NDELAY . D_FMT . PHP_ZTS .
GLOB_ERR . AM_STR . SQL_DOUBLE . SOL_TCP . FILE_APPEND . LOG_ERR . SORT_ASC .
SOCK_RAW . LOG_INFO . LC_TIME . SQL_FLOAT . SORT_DESC . INFO_MODULES . E_ERROR .
IMG_GIF . SQL_REAL . LOG_DEBUG . DNS_NS . CODESET . CAL_FRENCH . CURLE_OK .
LDAP_OPT_RESTART . LOCK_SH . XML_PI_NODE . SQLITE_INTERRUPT . MYSQLI_ASYNC .
CURLM_OK . SNMP_NULL . SQLITE_NOMEM . LC_MESSAGES . IMG_JPG . SO_KEEPALIVE .
SOCKET_ENXIO . LOCK_EX . D_T_FMT . ENT_QUOTES . LOG_NOTICE . SOCK_RDM .
INPUT_ENV . CURLAUTH_NTLM . INPUT_SESSION . AF_INET . IMG_JPEG . SQL_CONCURRENCY
. SEEK_SET . SOCKET_EIO . LC_CTYPE . PHP_URL_QUERY . LOG_KERN . INI_SYSTEM .
IMAGETYPE_BMP . SEEK_END . JSON_HEX_QUOT . LOG_PID . LIBXML_DTDATTR .
XML_DOCUMENT_NODE . PHP_DEBUG . LOG_CRIT . ENT_IGNORE . LC_NUMERIC .
DOM_NOT_SUPPORTED_ERR . PHP_URL_FRAGMENT . FILE_TEXT . IMAGETYPE_TIFF_II .
LOG_CONS . LOG_EMERG . UPLOAD_ERR_CANT_WRITE . MSG_PEEK . SQLITE_OK . FNM_PERIOD
. AF_UNIX . CURLPROTO_FTPS . STREAM_NOTIFY_FAILURE . MYSQL_BOTH .
CURLE_FTP_ACCESS_DENIED . MSG_OOB . FTP_TEXT . LC_MONETARY .
CURLE_COULDNT_CONNECT . SQLITE_BUSY . "\n";
This probably isn't very portable code, but it works fine on my system. Here's the code that generated it:
<?php
$phi = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911375";
echo "<?php\necho TRUE . '.' . ";
$consts = get_defined_constants();
foreach($consts as $k=>$v) if (preg_match('/\d|TRUE|PHP_EOL/',$k)) unset($consts[$k]);
for ($p=0;$p<strlen($phi);$p++) {
$d = ord($phi[$p]) - 48;
$min = 999;
foreach($consts as $k=>$v) {
if (strlen("$v")==1 && $v==$d && strlen($k)<$min) {
$min = strlen($k);
$maxk = $k;
}
}
if ($min==999) break;
echo "$maxk . ";
unset($consts[$maxk]);
}
echo "\"\\n\";\n";
Perl - 37 digits
use Math::Trig;
use POSIX;
print
!0,'.',chop$=||$=,A==A,2**3,$|,++$^F,75/5/5,pi*pi%\_,4+4,a^Y,w&'?',~"\xcb",$^=~y/_//c,
ord"\b",unpack(h,"\t").@{[c,c,c,c]},$#{[b..j]},"$^W"|'$',$^H>>log$^H,cos()- -cos,$[,
$-=sqrt%SIG,$%=$],$+[*$=~/($)/],split('',$~).map(glob,"{,,,}{,}"),index(\L,L),
exp(exp)<<exp,ceil(sinh acosh$^C).keys{stat$0},rindex(\R,R),($s=ssssss)=~s/s//g,
$?=acos(--$z),$^T=pack(u,$^T.$^T.TTTT),B~~B,C<=C,length$^V,
392 bytes currently, (10.6 per digit).
Output:
1.618033988749894848204586834365638117
Self-imposed Restrictions
I've added a few additional restrictions to limit the use of language features that would trivialize the problem. For example, array dereference @{...}
and array final index $#{...}
are only used once. Each array used is generated in a different manner (compare [c,c,c,c]
, [b..j]
, split('',$~)
, map(glob,"{,,,}{,}")
). Additionally, no symbol or bareword is used more than once, although this is explicitly allowed in the challenge description. I think it's a good idea for perl (or any language with int-only special variables (are there any others?)), because it limits the number of implicit int conversions.
Pieces
!0 # returns 1
'.'
chop$=||$= # $= is 60, chop the 0, returns 6
A==A # returns 1 (because 0==0)
2**3 # returns 8
$| # auto flush, returns 0
++$^F # max system filehandle pre-incremented, returns 3
75/5/5 # returns 3
pi*pi%\_ # pi² mod a large value, returns 9
4+4 # returns 8
a^Y # bitwise 'a' xor 'Y', returns 8
w&'?' # bitwise 'w' and '?', returns 7
~"\xcb" # bitwise inversion of char 203, returns 4
$^=~y/_//c # count non-underscores in $^, returns 9
ord"\b" # returns 8
unpack(h,"\t") # unpack "\t" as a hex nibble, returns 9
@{[c,c,c,c]} # anonymous array, returns 4 in scalar context
$#{[b..j]} # final index of the range b..j, returns 8
"$^W"|'$' # bitwise '0' or '$', returns 4
$^H>>log$^H # $^H is 256, log$^H is ~5, returns 8
cos()- -cos # cos(undef) is 1, subtract -1, returns 2
$[ # array start index, returns 0
$-=sqrt%SIG # set $- to sqrt(23), returns 4
$%=$] # set $% to the version number, returns 5
$+[*$=~/($)/] # match end on *$ (aka *main::$), returns 8
split('',$~) # split "STDOUT" into chars, returns 6 in scalar context
map(glob,"{,,,}{,}") # an array of 8 empty strings, returns 8 in scalar context
index(\L,L) # finds 'L' in 'SCALAR(...)', returns 3
exp(exp)<<exp # 2.718281828 << 1, returns 4
ceil(sinh acosh$^C) # ceil(2.30129...), returns 3
keys{stat$0} # stat$0 is an array with 13 entries, which has 6 keys when
# interpreted as a hash, returns 6 in scalar context
rindex(\R,R) # finds 'R' in 'SCALAR(...)' in reverse, returns 5
($s=ssssss)=~s/s//g # replace the every 's' in 'ssssss' with nothing, returns 6
$?=acos(--$z) # set $? to 3.14159... implicit int conversion, returns 3
$^T=pack(u,$^T.$^T.TTTT) # set $^T to "8,30P,...", returns 8
B~~B # returns 1 (because B represents the same thing as B)
C<=C # returns 1 (because 0<=0)
length$^V # string length of $^V, returns 7
Python 2.7, 19 digits, 231 relevant chars
from math import ceil, pi, trunc, gamma, sin
print ''.join(map(str,
[33-32,
chr(46),
~-7,
8>>3,
trunc(gamma(4.3)),
'x'.find('x'),
22/7,
range(4).pop(),
len('am I phi?'),
52%44,
2*4,
5|2,
ord('/'),
'\b',
5+2+2,
2<<2,
eval("5+2+2"),
ceil(pi),
'\b',
'\b',
str(sin(5))[5],
5&52]))