Spigot program for computing e…

February 11, 2007 | General | By: Mark VandeWettering

I went ahead and coded up a version of the spigot algorithm for computing e. It makes a nice three line .signature program.

Here are the first 1000 digits (reformatted for legibility):

2.718 28182 84590 45235 36028 74713 52662 49775 72470 93699
95957 49669 67627 72407 66303 53547 59457 13821 78525 16642
74274 66391 93200 30599 21817 41359 66290 43572 90033 42952
60595 63073 81323 28627 94349 07632 33829 88075 31952 51019
01157 38341 87930 70215 40891 49934 88416 75092 44761 46066
80822 64800 16847 74118 53742 34544 24371 07539 07774 49920
69551 70276 18386 06261 33138 45830 00752 04493 38265 60297
60673 71132 00709 32870 91274 43747 04723 06969 77209 31014
16928 36819 02551 51086 57463 77211 12523 89784 42505 69536
96770 78544 99699 67946 86445 49059 87931 63688 92300 98793
12773 61782 15424 99922 95763 51482 20826 98951 93668 03318
25288 69398 49646 51058 20939 23982 94887 93320 36250 94431
17301 23819 70684 16140 39701 98376 79320 68328 23764 64804
29531 18023 28782 50981 94558 15301 75671 73613 32069 81125
09961 81881 59304 16903 51598 88851 93458 07273 86673 85894
22879 22849 98920 86805 82574 92796 10484 19844 43634 63244
96848 75602 33624 82704 19786 23209 00216 09902 35304 36994
18491 46314 09343 17381 43640 54625 31520 96183 69088 87070
16768 39642 43781 40592 71456 35490 61303 10720 85103 83750
51011 57477 04171 89861 06873 96965 52126 71546 88957 03503

and here is the little .signature program:

#define N (1000)         /* compute N-1 digits of e, by brainwagon@gmail.com */
main(i,j,q){int A[N];printf("2.");for(j=0;j<N;j++)A[j]=1;for(i=0;i<N-2;i++){q=0
;for(j=N-1;j>=0;){A[j]=10*A[j]+q;q=A[j]/(j+2);A[j]%=(j+2);j--;}putchar(q+48);}}

You can click here to get the code without the hassle of cutting and pasting.

Addendum: A bit more tinkering lead to this program, an even shorter program (it legitimately fits in two lines of code).

Addendum2: I got one for pi working. It’s almost four full lines, and I can’t help but think it could be significantly shortened. Maybe when I have some idle moments. The previously linked versions of this program (as well as its output) contained a bug, and the digits that I had below were not correct. They have been corrected

3.141 59265 35897 93238 46264 33832 79502 88419 71693 99375
10582 09749 44592 30781 64062 86208 99862 80348 25342 11706
79821 48086 51328 23066 47093 84460 95505 82231 72535 94081
28481 11745 02841 02701 93852 11055 59644 62294 89549 30381
96442 88109 75665 93344 61284 75648 23378 67831 65271 20190
91456 48566 92346 03486 10454 32664 82133 93607 26024 91412
73724 58700 66063 15588 17488 15209 20962 82925 40917 15364
36789 25903 60011 33053 05488 20466 52138 41469 51941 51160
94330 57270 36575 95919 53092 18611 73819 32611 79310 51185
48074 46237 99627 49567 35188 57527 24891 22793 81830 11949
12983 36733 62440 65664 30860 21394 94639 52247 37190 70217
98609 43702 77053 92171 76293 17675 23846 74818 46766 94051
32000 56812 71452 63560 82778 57713 42757 78960 91736 37178
72146 84409 01224 95343 01465 49585 37105 07922 79689 25892
35420 19956 11212 90219 60864 03441 81598 13629 77477 13099
60518 70721 13499 99998 37297 80499 51059 73173 28160 96318
59502 44594 55346 90830 26425 22308 25334 46850 35261 93118
81710 10003 13783 87528 86587 53320 83814 20617 17766 91473
03598 25349 04287 55468 73115 95628 63882 35378 75937 51957
78185 77805 32171 22680 66130 01927 87661 11959 09216 42019

Addendum3: I managed to trim enough from it to allow me to put an actual email return address. It’s got a bug. I’ll try again tomorrow.

Addendum4: Sigh. Tom pointed out the problem to me. Once that was out of the way, I figured out a number of optimizations to strip it down to fit into three lines,
which I think is an entirely credible result. Here’s the (for now) final version. It’s got an entire fourth line for you to stick good stuff onto.

[tags]Mathematics,Programming[/tags]