Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
7  
So much for Ackerman(9,9) :P – JPvdMerwe Jan 29 '11 at 12:14
3  
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). – Alexandru Jan 29 '11 at 17:28
1  
here is a proposed evaluation formula magnitude / (characters^2 * time) – SHiNKiROU Feb 20 '11 at 5:32
11  
print '10' - since you haven't specified the base, this is using base-Graham's Number – Skizz Mar 10 '11 at 11:38
4  
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. – user unknown Apr 12 '11 at 0:09
show 4 more comments

closed as not a real question by gnibbler Jun 19 '12 at 12:01

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

33 Answers

1 2

JavaScript (10)

alert(1/0)

Prints "Infinity", did I win? :)

share|improve this answer
3  
But, this is codegolf :P – user11 Feb 25 '11 at 22:40
2  
what about alert('∞') ? – Clyde Lobo Mar 10 '11 at 4:10
30  
Prove infinity is an integer :-) – Paul Mar 15 '11 at 10:12
14  
@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
3  
@Paul typeof(1/0) == typeof(1) :P – Alpha Dec 20 '11 at 2:24
show 7 more comments

bc (1 character)

9

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

share|improve this answer
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
4  
+1 (although 99 has a far better magnitude/length ratio) – Peter Taylor Feb 7 '11 at 9:45
8  
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
2  
@PeterTaylor while true, 999 is almost 10 times as good. – GigaWatt Jan 13 '12 at 20:44

bash (41)

approx. 10,000,000 digits:

ulimit -t 60;while true;do echo -n 5;done
share|improve this answer
6  
+1 for style in using all the allotted time – Paul Mar 15 '11 at 10:20

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
share|improve this answer
Is that going to run in under a minute? Probably not. – marcog Jan 29 '11 at 17:16
If not, do 8**8**8, or 7**7**7... – Keith Randall Jan 29 '11 at 17:29
3  
8**8**9 takes 13 seconds and is 121 million digits. – Keith Randall Jan 29 '11 at 17:35
8  
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. 8**9**9 takes just about a minute to compute, 350M digits. – Keith Randall Jan 29 '11 at 17:44

Ruby - 8 chars

p $$**$$

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

share|improve this answer

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
share|improve this answer
while(1){print 1}

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

share|improve this answer
8  
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. – Chris Jester-Young 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 – Alpha Feb 27 '12 at 17:46

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
share|improve this answer
Who downvoted this? And did you downvote the question too? - because if there's a problem it's there, not here. – Peter Taylor Feb 20 '11 at 15:21

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;
share|improve this answer
8  
To what medium are you writing this number at a rate of about 26 GB/s? – Lars Haugseth Feb 25 '11 at 15:14
3  
/dev/null (it is web scale after all). – Xeoncross Dec 17 '11 at 21:54

Mathematica, 5

10! !

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

share|improve this answer
1  
Why not 99! ! ? Same count. Much larger number. – CMP Dec 21 '11 at 16:49
1  
@CMP larger numbers do not print in under a minute, and Mathematica will not even attempt to fully evaluate 99! !. – Mr.Wizard Dec 21 '11 at 16:53
2  
Sounds fair to me then. – CMP Dec 21 '11 at 18:05

LISP (18)

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

(expt 10 10000000)
share|improve this answer

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
share|improve this answer

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.

share|improve this answer

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}
share|improve this answer

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.

share|improve this answer
I don't think I can verify your output of 208GB ;) – Alexandru Jan 30 '11 at 14:11
Modified the code to a verifiable 1GB, it outputs it to a file for verification. – Kevin Brown 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

share|improve this answer
The first isn't an integer and will the second return in 1 minute? – marcog Feb 4 '11 at 14:00
3  
@marcog, what's the fractional part of 1.7976931348623157e+308 ? – Peter Taylor 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.

share|improve this answer
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.

share|improve this answer
This wouldn't be represented as an Integer and probably not finish wihin 60 seconds. – rubber boots 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.

share|improve this answer
You can make it shorter : say"1"x2e9 (10 char). – M42 Dec 15 '11 at 12:38
@M42: Or even say 9x2e9. – Ilmari Karonen Dec 23 '11 at 1:50
@IlmariKaronen: Yes, you're right, and say 9x9e9 produces a longer number. – M42 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
share|improve this answer

Based on @M28 's answer

PHP 10 chars

echo 9e999
share|improve this answer

C

printf("-infinity");

What's your definition of "large"?

share|improve this answer
If you can output a unicode character (wprintf), then just output the '∞' character (U+221E) – Skizz Mar 10 '11 at 11:18
1  
What's your definition of "integer"? ;-) – hstoerr Feb 1 '12 at 13:15

PHP, 18

<?for(;1;print9){}

=D

share|improve this answer
1  
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. – GigaWatt Jan 13 '12 at 20:37
Use a semicolon instead. So: <?for(;;print 9); – rynah 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..."

share|improve this answer

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.
share|improve this answer

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.

share|improve this answer

Haskell (20 characters)

main=print.floor$1/0
share|improve this answer

Bash, 10 characters

seq -s9 $$

2011.8 digits per code character today on my machine.

share|improve this answer
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 Jan 14 '12 at 11:03

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.

share|improve this answer

Windows x86 .com

8 bytes

B4 02 B2 39 CD 21 EB F8
share|improve this answer
1 2

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