6
\$\begingroup\$

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.

\$\endgroup\$
9
  • 9
    \$\begingroup\$ So much for Ackerman(9,9) :P \$\endgroup\$
    – JPvdMerwe
    Commented Jan 29, 2011 at 12:14
  • 3
    \$\begingroup\$ 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). \$\endgroup\$
    – Alexandru
    Commented Jan 29, 2011 at 17:28
  • 16
    \$\begingroup\$ print '10' - since you haven't specified the base, this is using base-Graham's Number \$\endgroup\$
    – Skizz
    Commented Mar 10, 2011 at 11:38
  • 6
    \$\begingroup\$ 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. \$\endgroup\$ Commented Apr 12, 2011 at 0:09
  • 2
    \$\begingroup\$ Voted to close as not constructive. One year, and still no winning criteria. \$\endgroup\$ Commented Jan 14, 2012 at 1:36

33 Answers 33

31
\$\begingroup\$

JavaScript (10)

alert(1/0)

Prints "Infinity", did I win? :)

\$\endgroup\$
14
  • 4
    \$\begingroup\$ But, this is codegolf :P \$\endgroup\$
    – user11
    Commented Feb 25, 2011 at 22:40
  • 3
    \$\begingroup\$ what about alert('∞') ? \$\endgroup\$
    – Clyde Lobo
    Commented Mar 10, 2011 at 4:10
  • 40
    \$\begingroup\$ Prove infinity is an integer :-) \$\endgroup\$
    – Paul
    Commented 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
    Commented Dec 7, 2011 at 20:52
  • 5
    \$\begingroup\$ @Paul typeof(1/0) == typeof(1) :P \$\endgroup\$
    – Alpha
    Commented Dec 20, 2011 at 2:24
21
\$\begingroup\$

bc (1 character)

9

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

\$\endgroup\$
4
  • 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
    Commented Feb 4, 2011 at 0:32
  • 7
    \$\begingroup\$ +1 (although 99 has a far better magnitude/length ratio) \$\endgroup\$ Commented 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
    Commented Feb 7, 2011 at 9:51
  • 5
    \$\begingroup\$ @PeterTaylor while true, 999 is almost 10 times as good. \$\endgroup\$
    – Mr. Llama
    Commented Jan 13, 2012 at 20:44
17
\$\begingroup\$

bash (41)

approx. 10,000,000 digits:

ulimit -t 60;while true;do echo -n 5;done
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Can save a few characters there: ulimit -t 60;while :;do printf 5;done (true:, echo -nprintf). \$\endgroup\$ Commented Nov 4, 2014 at 2:05
16
\$\begingroup\$

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
\$\endgroup\$
5
  • \$\begingroup\$ Is that going to run in under a minute? Probably not. \$\endgroup\$
    – moinudin
    Commented Jan 29, 2011 at 17:16
  • \$\begingroup\$ If not, do 888, or 777... \$\endgroup\$ Commented Jan 29, 2011 at 17:29
  • 3
    \$\begingroup\$ 889 takes 13 seconds and is 121 million digits. \$\endgroup\$ Commented 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\$ Commented Jan 29, 2011 at 17:44
  • \$\begingroup\$ hex is faster because that skips expensive binary->decimal conversions. \$\endgroup\$ Commented Jun 4, 2017 at 20:11
12
\$\begingroup\$

Ruby - 8 chars

p $$**$$

260120641601536 digits, today on my system. ($$ is the process ID).

\$\endgroup\$
11
\$\begingroup\$
while(1){print 1}

Time to write is O(n) compared to length of output number.

\$\endgroup\$
5
  • 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\$ Commented Feb 3, 2011 at 22:18
  • \$\begingroup\$ I chose 1 just so it sounds nicer when you read it out loud :-) \$\endgroup\$
    – user230
    Commented 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
    Commented Feb 27, 2012 at 17:46
  • \$\begingroup\$ @Alpha numbers cannot be copyrighted \$\endgroup\$ Commented 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
    Commented Aug 11, 2023 at 15:30
10
\$\begingroup\$

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
\$\endgroup\$
7
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Who downvoted this? And did you downvote the question too? - because if there's a problem it's there, not here. \$\endgroup\$ Commented Feb 20, 2011 at 15:21
7
\$\begingroup\$

Mathematica, 5

10! !

10 factorial, factorial. Prints 22,228,104 digits in about 30 seconds on my machine.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Why not 99! ! ? Same count. Much larger number. \$\endgroup\$
    – captncraig
    Commented 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
    Commented Dec 21, 2011 at 16:53
  • 3
    \$\begingroup\$ Sounds fair to me then. \$\endgroup\$
    – captncraig
    Commented Dec 21, 2011 at 18:05
5
\$\begingroup\$

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;
\$\endgroup\$
2
2
\$\begingroup\$

LISP (18)

10,000,000 digits in few seconds (SBCL).

(expt 10 10000000)
\$\endgroup\$
1
  • \$\begingroup\$ With the same amount of characters, can't you just do (expt 99 99999999)? \$\endgroup\$
    – bcsb1001
    Commented Mar 20, 2015 at 19:41
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Bash, 10 characters

seq -s9 $$

2011.8 digits per code character today on my machine.

\$\endgroup\$
2
  • \$\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\$ Commented 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\$ Commented Oct 12, 2015 at 20:30
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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}
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ I don't think I can verify your output of 208GB ;) \$\endgroup\$
    – Alexandru
    Commented Jan 30, 2011 at 14:11
  • \$\begingroup\$ Modified the code to a verifiable 1GB, it outputs it to a file for verification. \$\endgroup\$ Commented Jan 30, 2011 at 18:51
1
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ The first isn't an integer and will the second return in 1 minute? \$\endgroup\$
    – moinudin
    Commented Feb 4, 2011 at 14:00
  • 3
    \$\begingroup\$ @marcog, what's the fractional part of 1.7976931348623157e+308 ? \$\endgroup\$ Commented Feb 20, 2011 at 8:53
1
\$\begingroup\$
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.

\$\endgroup\$
1
\$\begingroup\$
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.

\$\endgroup\$
1
  • \$\begingroup\$ This wouldn't be represented as an Integer and probably not finish wihin 60 seconds. \$\endgroup\$ Commented Mar 9, 2011 at 17:36
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ You can make it shorter : say"1"x2e9 (10 char). \$\endgroup\$
    – Toto
    Commented Dec 15, 2011 at 12:38
  • \$\begingroup\$ @M42: Or even say 9x2e9. \$\endgroup\$ Commented Dec 23, 2011 at 1:50
  • \$\begingroup\$ @IlmariKaronen: Yes, you're right, and say 9x9e9 produces a longer number. \$\endgroup\$
    – Toto
    Commented Dec 23, 2011 at 9:03
1
\$\begingroup\$

Ruby - 12 chars

7.5e+306 / character of code

25.7 digits / character of code

p 9e307.to_i
\$\endgroup\$
1
\$\begingroup\$

Based on @M28 's answer

PHP 10 chars

echo 9e999
\$\endgroup\$
1
\$\begingroup\$

C

printf("-infinity");

What's your definition of "large"?

\$\endgroup\$
2
  • \$\begingroup\$ If you can output a unicode character (wprintf), then just output the '∞' character (U+221E) \$\endgroup\$
    – Skizz
    Commented Mar 10, 2011 at 11:18
  • 2
    \$\begingroup\$ What's your definition of "integer"? ;-) \$\endgroup\$ Commented Feb 1, 2012 at 13:15
1
\$\begingroup\$

PHP, 18

<?for(;1;print9){}

=D

\$\endgroup\$
2
  • 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
    Commented Jan 13, 2012 at 20:37
  • \$\begingroup\$ Use a semicolon instead. So: <?for(;;print 9); \$\endgroup\$
    – Ry-
    Commented Feb 17, 2012 at 4:39
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Haskell (20 characters)

main=print.floor$1/0
\$\endgroup\$
1
\$\begingroup\$

Brainfuck, 4

Of course the answer is in base 256

-[.]

C-C after a minute. Will print a number about 255^1e10.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.