Print largest integer you can with the fewest characters

6

3

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.

marcog

Posted 2011-01-29T11:52:43.537

Reputation: 10 244

Question was closed 2012-06-19T12:01:17.657

1here is a proposed evaluation formula magnitude / (characters^2 * time) – Ming-Tang – 2011-02-20T05:32:01.580

@Shinkirou I would measure my time in decades, so I will get a very huge score :P – None – 2011-02-25T22:41:52.527

16print '10' - since you haven't specified the base, this is using base-Graham's Number – Skizz – 2011-03-10T11:38:36.647

As per @Skizz smartass comment, I assume the integer has to consist solely of the digits 0-9 in base 10, so things like the literal strings "9e+999" or "9^9^9" or "Ackerman(9,9)" are out, even though they technically represent integers? – barrycarter – 2011-03-20T04:37:25.637

9So much for Ackerman(9,9) :P – JPvdMerwe – 2011-01-29T12:14:29.377

3Note 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). – Alexandru – 2011-01-29T17:28:40.997

6The 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. – user unknown – 2011-04-12T00:09:24.747

It depends on what shelves you have. – kinokijuf – 2011-12-25T11:43:45.470

2Voted to close as not constructive. One year, and still no winning criteria. – user unknown – 2012-01-14T01:36:30.813

Answers

31

JavaScript (10)

alert(1/0)

Prints "Infinity", did I win? :)

user11

Posted 2011-01-29T11:52:43.537

Reputation:

Wouldn't alert(Infinity); work too? – Nathan Osman – 2011-02-25T20:37:36.577

4But, this is codegolf :P – None – 2011-02-25T22:40:42.293

@Paul the only know person to fit infinity into an integer is Jon Skeet... – RudolfJelin – 2016-11-21T20:14:57.983

3what about alert('∞') ? – Clyde Lobo – 2011-03-10T04:10:32.900

1That would be too obvious :) (And that symbol encoded in UTF-8 would need 3 bytes, resulting in the same length) – None – 2011-03-10T18:56:29.853

39Prove infinity is an integer :-) – Paul – 2011-03-15T10:12:59.777

@M28: I thought code golf was about character count, not byte count. is only one character even if it requires 3 bytes to represent in UTF-8. – JAB – 2011-06-17T20:03:16.177

@kinokijuf typeof null == typeof Math – Keen – 2014-06-13T21:20:59.157

I know I'm a bit late to the party... but alert(1/0) works as well and is 10 characters. – Peter C – 2011-12-07T07:14:15.240

That's great, a little late, but I edited my answer, thanks! – None – 2011-12-07T16:52:26.217

24@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 – 2011-12-07T20:52:53.673

5@Paul typeof(1/0) == typeof(1) :P – Alpha – 2011-12-20T02:24:53.687

1@Alpha typeof(1/0) == typeof(1.23) :P – None – 2011-12-20T12:27:50.307

@M28 typeof(1) == typeof(1.23) :P – kinokijuf – 2011-12-25T11:46:36.060

21

bc (1 character)

9

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

J B

Posted 2011-01-29T11:52:43.537

Reputation: 9 638

7+1 (although 99 has a far better magnitude/length ratio) – Peter Taylor – 2011-02-07T09:45:48.180

9Maybe 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 – 2011-02-07T09:51:23.227

1Yikes, 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 – 2011-02-04T00:32:18.857

5@PeterTaylor while true, 999 is almost 10 times as good. – Mr. Llama – 2012-01-13T20:44:42.500

17

bash (41)

approx. 10,000,000 digits:

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

Eelvex

Posted 2011-01-29T11:52:43.537

Reputation: 5 204

2Can save a few characters there: ulimit -t 60;while :;do printf 5;done (true:, echo -nprintf). – Jeremy Kerr – 2014-11-04T02:05:11.967

16

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

Keith Randall

Posted 2011-01-29T11:52:43.537

Reputation: 19 865

hex is faster because that skips expensive binary->decimal conversions. – CalculatorFeline – 2017-06-04T20:11:01.017

Is that going to run in under a minute? Probably not. – marcog – 2011-01-29T17:16:17.563

If not, do 888, or 777... – Keith Randall – 2011-01-29T17:29:05.570

3889 takes 13 seconds and is 121 million digits. – Keith Randall – 2011-01-29T17:35:49.917

9Looks 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. – Keith Randall – 2011-01-29T17:44:44.607

12

Ruby - 8 chars

p $$**$$

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

steenslag

Posted 2011-01-29T11:52:43.537

Reputation: 2 070

11

while(1){print 1}

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

user230

Posted 2011-01-29T11:52:43.537

Reputation:

1@ChrisJester-Young Using 9 may turn in a copyright violation of Revolution 9 from The Beatles. =P – Alpha – 2012-02-27T17:46:21.233

9Surely, 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. – Chris Jester-Young – 2011-02-03T22:18:31.000

I chose 1 just so it sounds nicer when you read it out loud :-) – None – 2011-02-03T22:20:55.567

10

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

Alexandru

Posted 2011-01-29T11:52:43.537

Reputation: 5 485

7

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

Peter Taylor

Posted 2011-01-29T11:52:43.537

Reputation: 41 901

Who downvoted this? And did you downvote the question too? - because if there's a problem it's there, not here. – Peter Taylor – 2011-02-20T15:21:14.640

7

Mathematica, 5

10! !

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

Mr.Wizard

Posted 2011-01-29T11:52:43.537

Reputation: 2 481

2Why not 99! ! ? Same count. Much larger number. – captncraig – 2011-12-21T16:49:50.217

1@CMP larger numbers do not print in under a minute, and Mathematica will not even attempt to fully evaluate 99! !. – Mr.Wizard – 2011-12-21T16:53:44.607

3Sounds fair to me then. – captncraig – 2011-12-21T18:05:50.310

5

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;

ircmaxell

Posted 2011-01-29T11:52:43.537

Reputation: 897

9To what medium are you writing this number at a rate of about 26 GB/s? – Lars Haugseth – 2011-02-25T15:14:43.563

3/dev/null (it is web scale after all). – Xeoncross – 2011-12-17T21:54:18.753

2

LISP (18)

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

(expt 10 10000000)

Eelvex

Posted 2011-01-29T11:52:43.537

Reputation: 5 204

With the same amount of characters, can't you just do (expt 99 99999999)? – bcsb1001 – 2015-03-20T19:41:49.473

2

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

mikera

Posted 2011-01-29T11:52:43.537

Reputation: 1 233

2

Bash, 10 characters

seq -s9 $$

2011.8 digits per code character today on my machine.

Hauleth

Posted 2011-01-29T11:52:43.537

Reputation: 1 472

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 – Not that Charles – 2015-10-12T20:30:51.750

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. – user unknown – 2012-01-14T11:03:37.453

1

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.

user unknown

Posted 2011-01-29T11:52:43.537

Reputation: 4 210

1

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.

e j

Posted 2011-01-29T11:52:43.537

Reputation: 11

This wouldn't be represented as an Integer and probably not finish wihin 60 seconds. – rubber boots – 2011-03-09T17:36:59.343

1

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.

rubber boots

Posted 2011-01-29T11:52:43.537

Reputation: 1 014

You can make it shorter : say"1"x2e9 (10 char). – Toto – 2011-12-15T12:38:31.320

@M42: Or even say 9x2e9. – Ilmari Karonen – 2011-12-23T01:50:40.490

@IlmariKaronen: Yes, you're right, and say 9x9e9 produces a longer number. – Toto – 2011-12-23T09:03:30.793

1

Ruby - 12 chars

7.5e+306 / character of code

25.7 digits / character of code

p 9e307.to_i

david4dev

Posted 2011-01-29T11:52:43.537

Reputation: 665

1

Based on @M28 's answer

PHP 10 chars

echo 9e999

Clyde Lobo

Posted 2011-01-29T11:52:43.537

Reputation: 1 395

1

C

printf("-infinity");

What's your definition of "large"?

Mateen Ulhaq

Posted 2011-01-29T11:52:43.537

Reputation: 1 889

2What's your definition of "integer"? ;-) – Hans-Peter Störr – 2012-02-01T13:15:22.587

If you can output a unicode character (wprintf), then just output the '∞' character (U+221E) – Skizz – 2011-03-10T11:18:32.593

1

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.

marcog

Posted 2011-01-29T11:52:43.537

Reputation: 10 244

1

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}

Joey

Posted 2011-01-29T11:52:43.537

Reputation: 12 260

1

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.

Kevin Brown

Posted 2011-01-29T11:52:43.537

Reputation: 5 756

I don't think I can verify your output of 208GB ;) – Alexandru – 2011-01-30T14:11:44.140

Modified the code to a verifiable 1GB, it outputs it to a file for verification. – Kevin Brown – 2011-01-30T18:51:39.080

1

PHP, 18

<?for(;1;print9){}

=D

Korvin Szanto

Posted 2011-01-29T11:52:43.537

Reputation: 121

Use a semicolon instead. So: <?for(;;print 9); – Ry- – 2012-02-17T04:39:06.527

1Notice: 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. – Mr. Llama – 2012-01-13T20:37:11.323

1

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

Mr. Llama

Posted 2011-01-29T11:52:43.537

Reputation: 2 387

1

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

www0z0k

Posted 2011-01-29T11:52:43.537

Reputation: 200

3@marcog, what's the fractional part of 1.7976931348623157e+308 ? – Peter Taylor – 2011-02-20T08:53:21.987

The first isn't an integer and will the second return in 1 minute? – marcog – 2011-02-04T14:00:00.627

1

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.

kernigh

Posted 2011-01-29T11:52:43.537

Reputation: 2 615

1

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.

AardvarkSoup

Posted 2011-01-29T11:52:43.537

Reputation: 344

1

Haskell (20 characters)

main=print.floor$1/0

00dani

Posted 2011-01-29T11:52:43.537

Reputation: 111

1

Brainfuck, 4

Of course the answer is in base 256

-[.]

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

walpen

Posted 2011-01-29T11:52:43.537

Reputation: 3 237

1

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.

Konrad Borowski

Posted 2011-01-29T11:52:43.537

Reputation: 11 185

0

Python, 21 characters

print "10"+"^10"*1337

"output a large integer"

It has 10 to the power of 10 repeated 1336 times (Tetration) number of zeros with a leading 1.

SlimJim

Posted 2011-01-29T11:52:43.537

Reputation: 131

0

Windows x86 .com

8 bytes

B4 02 B2 39 CD 21 EB F8

Skizz

Posted 2011-01-29T11:52:43.537

Reputation: 2 225

0

Interpreted Haskell using tryhaskell.org, 10 characters, 5120 digits

0xF^0xFFFF

iracigt

Posted 2011-01-29T11:52:43.537

Reputation: 101

That interpreter seems to truncate the output, which should be the 77076 digits of 15^65535. Notice that 999^999999 also has ten characters and should produce 2999563 digits. – r.e.s. – 2012-06-19T13:00:53.170