Find a way to output a large integer with few characters. Solutions will be scored based on the magnitude of the number and shortness of code.
EDIT: Let's impose a time limit of a minute on sensible hardware, where sensible is a PC you can buy off the shelves.
33 Answers 33
JavaScript (10)
alert(1/0)
Prints "Infinity", did I win? :)
-
4\$\begingroup\$ But, this is codegolf :P \$\endgroup\$user11– user112011年02月25日 22:40:42 +00:00Commented Feb 25, 2011 at 22:40
-
3\$\begingroup\$ what about
alert('∞')
? \$\endgroup\$Clyde Lobo– Clyde Lobo2011年03月10日 04:10:32 +00:00Commented Mar 10, 2011 at 4:10 -
40\$\begingroup\$ Prove infinity is an integer :-) \$\endgroup\$Paul– Paul2011年03月15日 10:12:59 +00:00Commented Mar 15, 2011 at 10:12
-
24\$\begingroup\$ @Paul: Chuck Norris counted to infinity, twice. I asked him if it was an integral number and he said: "I don't count fractions, just fractures" I think that proves it. \$\endgroup\$Kris– Kris2011年12月07日 20:52:53 +00:00Commented Dec 7, 2011 at 20:52
-
5\$\begingroup\$ @Paul typeof(1/0) == typeof(1) :P \$\endgroup\$Alpha– Alpha2011年12月20日 02:24:53 +00:00Commented Dec 20, 2011 at 2:24
bc (1 character)
9
Without a scoring method, this has a fair magnitude/length ratio indeed.
-
1\$\begingroup\$ Yikes, downmodded for that. It was intended as a joke, and yet... come on, please at least explain why you think the reasoning is invalid. \$\endgroup\$J B– J B2011年02月04日 00:32:18 +00:00Commented Feb 4, 2011 at 0:32
-
7\$\begingroup\$ +1 (although 99 has a far better magnitude/length ratio) \$\endgroup\$Peter Taylor– Peter Taylor2011年02月07日 09:45:48 +00:00Commented Feb 7, 2011 at 9:45
-
9\$\begingroup\$ Maybe I should say it outright instead of hinting: in its current state the question is subjective and gives us no way to compare answers. \$\endgroup\$J B– J B2011年02月07日 09:51:23 +00:00Commented Feb 7, 2011 at 9:51
-
5\$\begingroup\$ @PeterTaylor while true,
999
is almost 10 times as good. \$\endgroup\$Mr. Llama– Mr. Llama2012年01月13日 20:44:42 +00:00Commented Jan 13, 2012 at 20:44
bash (41)
approx. 10,000,000 digits:
ulimit -t 60;while true;do echo -n 5;done
-
2\$\begingroup\$ Can save a few characters there:
ulimit -t 60;while :;do printf 5;done
(true
→:
,echo -n
→printf
). \$\endgroup\$Jeremy Kerr– Jeremy Kerr2014年11月04日 02:05:11 +00:00Commented Nov 4, 2014 at 2:05
Python 18 characters
print hex(8**9**9)
It's about 290 million digits.
Turns out python print
of decimal numbers is really slow, but hex
is fast:
> time python -c "print hex(8**9**9)" | wc
1 1 290565371
real 0m40.514s
-
\$\begingroup\$ Is that going to run in under a minute? Probably not. \$\endgroup\$moinudin– moinudin2011年01月29日 17:16:17 +00:00Commented Jan 29, 2011 at 17:16
-
\$\begingroup\$ If not, do 888, or 777... \$\endgroup\$Keith Randall– Keith Randall2011年01月29日 17:29:05 +00:00Commented Jan 29, 2011 at 17:29
-
3\$\begingroup\$ 889 takes 13 seconds and is 121 million digits. \$\endgroup\$Keith Randall– Keith Randall2011年01月29日 17:35:49 +00:00Commented Jan 29, 2011 at 17:35
-
9\$\begingroup\$ Looks like having a power of 2 as the leftmost digit helps a lot, perhaps the python internal implementation of ** takes advantage of sparse bignums. 899 takes just about a minute to compute, 350M digits. \$\endgroup\$Keith Randall– Keith Randall2011年01月29日 17:44:44 +00:00Commented Jan 29, 2011 at 17:44
-
\$\begingroup\$
hex
is faster because that skips expensive binary->decimal conversions. \$\endgroup\$CalculatorFeline– CalculatorFeline2017年06月04日 20:11:01 +00:00Commented Jun 4, 2017 at 20:11
Ruby - 8 chars
p $$**$$
260120641601536 digits, today on my system. ($$ is the process ID).
while(1){print 1}
Time to write is O(n)
compared to length of output number.
-
9\$\begingroup\$ Surely, writing 9 is always going to be better than writing 1, right? Also, Code Golf 101: always strip out all optional spaces. That means all the spaces around the brackets, in this case. \$\endgroup\$C. K. Young– C. K. Young2011年02月03日 22:18:31 +00:00Commented Feb 3, 2011 at 22:18
-
\$\begingroup\$ I chose 1 just so it sounds nicer when you read it out loud :-) \$\endgroup\$user230– user2302011年02月03日 22:20:55 +00:00Commented Feb 3, 2011 at 22:20
-
1\$\begingroup\$ @ChrisJester-Young Using 9 may turn in a copyright violation of Revolution 9 from The Beatles. =P \$\endgroup\$Alpha– Alpha2012年02月27日 17:46:21 +00:00Commented Feb 27, 2012 at 17:46
-
\$\begingroup\$ @Alpha numbers cannot be copyrighted \$\endgroup\$The Empty String Photographer– The Empty String Photographer2023年08月10日 14:40:00 +00:00Commented Aug 10, 2023 at 14:40
-
\$\begingroup\$ @TheEmptyStringPhotographer True. My comment was made in jest, the last characters are a tongue-out emoji denoting joking. Even if they were, that wouldn't apply here since we're just printing characters on an output stream rather than the recording of the track, so they'd be very different pieces in a non-competing space. \$\endgroup\$Alpha– Alpha2023年08月11日 15:30:56 +00:00Commented Aug 11, 2023 at 15:30
C 86 (including NL)
main(i){
char b[8<<14];
memset(b,'9',8<<14);
for(i=0;i<8<<15;i++)
write(1,b,8<<14);
}
Prints 34_359_973_368 digits on my i7 620M. Challenge this score!
$ gcc -O3 a.c; time ./a.out | wc -c
34359738368
real 0m31.974s
user 0m0.500s
sys 0m41.031s
bash, 35 chars, unimaginably big number
The question doesn't specify the output representation, so I'm going to go charging far past any of the numbers output by previous answers (and way way way past even Graham's number):
for((i=999;i--;))do printf 9→9;done
An even shorter answer, although with a smaller number (still larger than Graham's number) is:
(12 chars)
echo 3→3→3→3
-
\$\begingroup\$ Who downvoted this? And did you downvote the question too? - because if there's a problem it's there, not here. \$\endgroup\$Peter Taylor– Peter Taylor2011年02月20日 15:21:14 +00:00Commented Feb 20, 2011 at 15:21
Mathematica, 5
10! !
10 factorial, factorial. Prints 22,228,104 digits in about 30 seconds on my machine.
-
2\$\begingroup\$ Why not
99! !
? Same count. Much larger number. \$\endgroup\$captncraig– captncraig2011年12月21日 16:49:50 +00:00Commented Dec 21, 2011 at 16:49 -
1\$\begingroup\$ @CMP larger numbers do not print in under a minute, and Mathematica will not even attempt to fully evaluate
99! !
. \$\endgroup\$Mr.Wizard– Mr.Wizard2011年12月21日 16:53:44 +00:00Commented Dec 21, 2011 at 16:53 -
3\$\begingroup\$ Sounds fair to me then. \$\endgroup\$captncraig– captncraig2011年12月21日 18:05:50 +00:00Commented Dec 21, 2011 at 18:05
PHP
1,500,050,000,000 digits in 53.383 seconds (1.5 trillion digits) in 61 characters:
$a=bcpow('1000','10000');for($i=0;$i<50000000;$i++)echo $a;
-
9\$\begingroup\$ To what medium are you writing this number at a rate of about 26 GB/s? \$\endgroup\$Lars Haugseth– Lars Haugseth2011年02月25日 15:14:43 +00:00Commented Feb 25, 2011 at 15:14
-
3\$\begingroup\$ /dev/null (it is web scale after all). \$\endgroup\$Xeoncross– Xeoncross2011年12月17日 21:54:18 +00:00Commented Dec 17, 2011 at 21:54
LISP (18)
10,000,000 digits in few seconds (SBCL).
(expt 10 10000000)
-
\$\begingroup\$ With the same amount of characters, can't you just do
(expt 99 99999999)
? \$\endgroup\$bcsb1001– bcsb10012015年03月20日 19:41:49 +00:00Commented Mar 20, 2015 at 19:41
Clojure - 28 chars (for unreasonably large numbers)
Strategy is to repeatedly apply the function f(x)=x^x. Works fine because Clojure automatically uses BigInteger arthmetic when this starts to overflow the normal integer range.
(nth(iterate #(expt % %)X)Y)
Choose X and Y depending on how unreasonably large you want the answer to be and how long you want it to run....:
For X=2:
Y=0 -> 2
Y=1 -> 4
Y=2 -> 256
Y=3 -> about 3.2*10^619 (in less than 1ms)
Y=4 -> unreasonably large
For X=9:
Y=0 -> 9
Y=1 -> 387420489
Y=2 -> about 10^320000000
Y=3 -> ermmm..... even bigger than unreasonably large?
A couple of things to note:
(iterate #(expt % %)X)
creates an infinite lazy sequence of ridiculously large exponentials. Y just determines which term of the sequence you want to look at (as you can see above, even the very early terms get very large very fast...)- if not already imported you need to (use 'clojure.contrib.math) for the expt function
Bash, 10 characters
seq -s9 $$
2011.8 digits per code character today on my machine.
-
\$\begingroup\$ How does it work?
seq --help
says:-s, --separator=STRING
. $$ is the PID of the current shell. So if it was 3, it would be 19293 - ah - I understand. I would vote it up, if there was an objective winning criteria. \$\endgroup\$user unknown– user unknown2012年01月14日 11:03:37 +00:00Commented Jan 14, 2012 at 11:03 -
\$\begingroup\$ Nice. Add a -w for an even higher number.... but since there are no winning criteria, it's impossible to know if that's better or worse \$\endgroup\$Not that Charles– Not that Charles2015年10月12日 20:30:51 +00:00Commented Oct 12, 2015 at 20:30
Python: 83 characters, 32,089,643 digit number on my PC in a minute
import sys
sys.setrecursionlimit(1e9)
def p(n):sys.stdout.write(str(n));p(n*9)
p(9)
Note that it will either run out of time (in which case kill it) or eventually throw stack overflow errors, so you need to pipe stderr to /dev/null
.
Windows PowerShell(7)
I guess this is cheating, but techncially the value printed is an integer:
'9'*9e6
Exactly 9000000 digits.
Can be made larger with (19):
'9'*[int]::MaxValue
but raises an OutOfMemoryException on my poor 32-bit machine. This, however, will work fine as long as it's left running:
for(){write-host -n 9}
PHP, 115 characters:
<? $f=fopen("/tmp/int.txt","w");for($i=$t=0;$i<2.4e7;$i++,$t++){if($t>1e4){fputs($f,$a);$t=0;}$a.=$i;}fputs($f,$a);
Outputs at least 1,024,000,00 characters to /tmp/int.txt.
File size (after running): http://codepad.viper-7.com/rgDglK
Output to Screen, 76 characters
<? for($i=$t=0;$i<2.4e7;$i++,$t++){if($t>1e4){echo $a;$t=0;}$a.=$i;}echo $a;
Has the ability to output 208, 896, 814, 305 characters, tested but unconfirmed through the output. You can confirm it by only calculating the length of the output through the second link. Each page may require a few refreshes due to errors.
Output: http://codepad.viper-7.com/rJJerv (Will crash the page eventually)
Length: http://codepad.viper-7.com/zWxX0C
Both the file size verification and the length verification take less than a minute on my old laptop, the output verification crashes the brower ~20 seconds into loading. The question did not state that the script had to work via command line, so I would not expect it to give perfect results when run that way.
-
\$\begingroup\$ I don't think I can verify your output of 208GB ;) \$\endgroup\$Alexandru– Alexandru2011年01月30日 14:11:44 +00:00Commented Jan 30, 2011 at 14:11
-
\$\begingroup\$ Modified the code to a verifiable 1GB, it outputs it to a file for verification. \$\endgroup\$Kevin Brown-Silva– Kevin Brown-Silva2011年01月30日 18:51:39 +00:00Commented Jan 30, 2011 at 18:51
alert(Number.MAX_VALUE); : 1.7976931348623157e+308
<script>function f(m){n=1;for(i=1;i<=m;i++){n*=i;}return n;}alert(f(f(9)));</script> : Infinity
-
\$\begingroup\$ The first isn't an integer and will the second return in 1 minute? \$\endgroup\$moinudin– moinudin2011年02月04日 14:00:00 +00:00Commented Feb 4, 2011 at 14:00
-
3\$\begingroup\$ @marcog, what's the fractional part of 1.7976931348623157e+308 ? \$\endgroup\$Peter Taylor– Peter Taylor2011年02月20日 08:53:21 +00:00Commented Feb 20, 2011 at 8:53
time echo "7^7^7" | bc
...
29571409790619889611503701095991663965767866370599610471047901915338\
37220795832889549191447357443319063581523185421788310894001395744859\
694202869611751580402966282378932933502849310357073612870132343
real 0m58.837s
user 0m56.220s
sys 0m0.028s
on a 1.6 Mhz Pentium M.
cat /dev/sda
It's a 256-base Integer with 640,135,028,736 digits on my 640GB hd. Too bad some are non-printable.
-
\$\begingroup\$ This wouldn't be represented as an Integer and probably not finish wihin 60 seconds. \$\endgroup\$rubber boots– rubber boots2011年03月09日 17:36:59 +00:00Commented Mar 9, 2011 at 17:36
Hmm, lets see, the largest "decimal number" I could generate on Linux, 2.4GHz Intel/Core2 w/2GB RAM, needing:
real 0m51.341s
user 0m1.820s
sys 0m5.644s
keeping user time far below 60s.
Perl, 15 characters
print 1,"0"x2e9
gives a number staring with 100000... and with about 2,000,000,000 (2 billion) zeros in total.
Regards
rbo
Tested with
time perl -e 'print 1,"0"x2e9'>out
on a good hard drive.
-
\$\begingroup\$ You can make it shorter :
say"1"x2e9
(10 char). \$\endgroup\$Toto– Toto2011年12月15日 12:38:31 +00:00Commented Dec 15, 2011 at 12:38 -
\$\begingroup\$ @M42: Or even
say 9x2e9
. \$\endgroup\$Ilmari Karonen– Ilmari Karonen2011年12月23日 01:50:40 +00:00Commented Dec 23, 2011 at 1:50 -
\$\begingroup\$ @IlmariKaronen: Yes, you're right, and
say 9x9e9
produces a longer number. \$\endgroup\$Toto– Toto2011年12月23日 09:03:30 +00:00Commented Dec 23, 2011 at 9:03
Ruby - 12 chars
7.5e+306 / character of code
25.7 digits / character of code
p 9e307.to_i
Based on @M28 's answer
PHP 10 chars
echo 9e999
C
printf("-infinity");
What's your definition of "large"?
-
\$\begingroup\$ If you can output a unicode character (wprintf), then just output the '∞' character (U+221E) \$\endgroup\$Skizz– Skizz2011年03月10日 11:18:32 +00:00Commented Mar 10, 2011 at 11:18
-
2\$\begingroup\$ What's your definition of "integer"? ;-) \$\endgroup\$Dr. Hans-Peter Störr– Dr. Hans-Peter Störr2012年02月01日 13:15:22 +00:00Commented Feb 1, 2012 at 13:15
PHP, 18
<?for(;1;print9){}
=D
-
1\$\begingroup\$
Notice: Use of undefined constant print9 - assumed 'print9' in Command line code on line 1
Although you'll have to add a space, you can remove the 1 from the foor loop. \$\endgroup\$Mr. Llama– Mr. Llama2012年01月13日 20:37:11 +00:00Commented Jan 13, 2012 at 20:37 -
\$\begingroup\$ Use a semicolon instead. So:
<?for(;;print 9);
\$\endgroup\$Ry-– Ry-2012年02月17日 04:39:06 +00:00Commented Feb 17, 2012 at 4:39
Ruby, 11
p 9 while 1
Or you can omit the 9
and pretend that it's outputting in the ascii representation of the number "0x0A0A0A0A..."
Factor, 137 characters
USING: calendar kernel io math.order sequences ;
58 seconds hence
99999 "9" <repetition> concat
[ dup write over now after? ] loop 2drop
With comments,
! calendar => seconds hence now
! kernel => dup over loop 2drop
! io => write
! math.order => after?
! sequences => <repetition> concat
USING: calendar kernel io math.order sequences ;
! Push the stop time (58 seconds after now). This leaves 2 extra seconds
! for starting and stopping the script.
58 seconds hence
! Push a long string of "9"s. The best length is near 99999.
99999 "9" <repetition> concat
[
! Write the very long string of "9"s.
dup write
! If the stop time is after now, then loop.
over now after?
] loop
! Empty the data stack.
2drop
This script tries to print as many "9" digits as possible, without exceeding the time limit of one minute. The number of digits changes from run to run.
Output is 10 ^ 4_765_052_349 - 1, if I must display the number in an xterm.
$ time ~/park/factor/factor scratch.factor | tee out
99999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999
...
999999999999999999 0m58.56s real 0m15.63s user 0m26.21s system
$ wc -c out
4765052349 out
Output is 10 ^ 22_843_571_562 - 1, if I never display the number.
$ time ~/park/factor/factor scratch.factor | wc -c
22843571562
0m58.32s real 0m50.27s user 0m19.50s system
Additional notes:
- Each
write
converts the string (Unicode codepoints) to a byte array (UTF-8). This conversion might waste time inside the loop. A faster program might make a byte array before the loop, and write the byte array inside the loop; but I cannot write a byte array to the default output stream, which is a character stream. I would have to use many characters to open a binary stream. - A faster program might call setitimer(2) and handle SIGALRM after 58 seconds. I did not find a short way to call setitimer(2) from Factor.
Haskell (code: 22 chars, output: 309 digits)
main=print$floor 1e309
This outputs: 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
Any higher exponent yields the same result, as 1e309 gets interpreted as Infinity, and this is apperantly the maximum result of floor
.
Haskell (20 characters)
main=print.floor1ドル/0
Brainfuck, 4
Of course the answer is in base 256
-[.]
C-C after a minute. Will print a number about 255^1e10.
Perl 6, 14 bytes
.print for^Inf
There is a pattern in this integer. I would name it a waterfall because it looks like one.
what is the highest number with 100 bytes of code
, or enforce a minimum numbergenerate a number, at least 9^1000) as pure digits with as few code as you can
. Searching for minimum and maximum the same time would need a conversion function, how to judge on smaller numbers generated with less code, since you cannot ensure that the shortest code will generate the largest number automatically. \$\endgroup\$