# Print largest integer you can with the fewest characters [closed]

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.

• So much for Ackerman(9,9) :P Jan 29 '11 at 12:14
• Note that the results highly depend on the output device (/dev/stdout - slowest since it involves graphics, /dev/null - fastest cause it doesn't do anything, | wc -l - medium). Jan 29 '11 at 17:28
• print '10' - since you haven't specified the base, this is using base-Graham's Number Mar 10 '11 at 11:38
• The question should either restrict the size of the code what is the highest number with 100 bytes of code, or enforce a minimum number generate 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. Apr 12 '11 at 0:09
• Voted to close as not constructive. One year, and still no winning criteria. Jan 14 '12 at 1:36

# JavaScript (10)

alert(1/0)


Prints "Infinity", did I win? :)

• But, this is codegolf :P
– user11
Feb 25 '11 at 22:40
• what about alert('∞') ? Mar 10 '11 at 4:10
• Prove infinity is an integer :-)
– Paul
Mar 15 '11 at 10:12
• @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.
– Kris
Dec 7 '11 at 20:52
• @Paul typeof(1/0) == typeof(1) :P Dec 20 '11 at 2:24

## bc (1 character)

9


Without a scoring method, this has a fair magnitude/length ratio indeed.

• 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.
– J B
Feb 4 '11 at 0:32
• +1 (although 99 has a far better magnitude/length ratio) Feb 7 '11 at 9:45
• 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.
– J B
Feb 7 '11 at 9:51
• @PeterTaylor while true, 999 is almost 10 times as good. Jan 13 '12 at 20:44

### bash (41)

approx. 10,000,000 digits:

ulimit -t 60;while true;do echo -n 5;done

• Can save a few characters there: ulimit -t 60;while :;do printf 5;done (true:, echo -nprintf). Nov 4 '14 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

• Is that going to run in under a minute? Probably not. Jan 29 '11 at 17:16
• If not, do 888, or 777... Jan 29 '11 at 17:29
• 889 takes 13 seconds and is 121 million digits. Jan 29 '11 at 17:35
• 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. Jan 29 '11 at 17:44
• hex is faster because that skips expensive binary->decimal conversions. Jun 4 '17 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. • 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. Feb 3 '11 at 22:18 • I chose 1 just so it sounds nicer when you read it out loud :-) – user230 Feb 3 '11 at 22:20 • @ChrisJester-Young Using 9 may turn in a copyright violation of Revolution 9 from The Beatles. =P Feb 27 '12 at 17:46 ## 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 • Who downvoted this? And did you downvote the question too? - because if there's a problem it's there, not here. Feb 20 '11 at 15:21 ## Mathematica, 5 10! !  10 factorial, factorial. Prints 22,228,104 digits in about 30 seconds on my machine. • Why not 99! ! ? Same count. Much larger number. Dec 21 '11 at 16:49 • @CMP larger numbers do not print in under a minute, and Mathematica will not even attempt to fully evaluate 99! !. Dec 21 '11 at 16:53 • Sounds fair to me then. Dec 21 '11 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;  • To what medium are you writing this number at a rate of about 26 GB/s? Feb 25 '11 at 15:14 • Dec 17 '11 at 21:54 ### LISP (18) 10,000,000 digits in few seconds (SBCL). (expt 10 10000000)  • With the same amount of characters, can't you just do (expt 99 99999999)? Mar 20 '15 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.

• 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. Jan 14 '12 at 11:03
• 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 Oct 12 '15 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. • I don't think I can verify your output of 208GB ;) Jan 30 '11 at 14:11 • Modified the code to a verifiable 1GB, it outputs it to a file for verification. Jan 30 '11 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 • The first isn't an integer and will the second return in 1 minute? Feb 4 '11 at 14:00 • @marcog, what's the fractional part of 1.7976931348623157e+308 ? Feb 20 '11 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. • This wouldn't be represented as an Integer and probably not finish wihin 60 seconds. Mar 9 '11 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. • You can make it shorter : say"1"x2e9 (10 char). – Toto Dec 15 '11 at 12:38 • @M42: Or even say 9x2e9. Dec 23 '11 at 1:50 • @IlmariKaronen: Yes, you're right, and say 9x9e9 produces a longer number. – Toto Dec 23 '11 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"? • If you can output a unicode character (wprintf), then just output the '∞' character (U+221E) Mar 10 '11 at 11:18 • What's your definition of "integer"? ;-) Feb 1 '12 at 13:15 ## PHP, 18 <?for(;1;print9){}  =D • 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. Jan 13 '12 at 20:37 • Use a semicolon instead. So: <?for(;;print 9); – Ry- Feb 17 '12 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


• 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.

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.floor$1/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.