Tiny programs for constants computation

(The smallest C codes to compute classical mathematical constants)

(Postcript version of this page : tinycode.ps, pdf version : tinycodes.pdf).

This page contains some very tiny programs in relation with the computation of classical mathematical constants and prime numbers.

Wanted : any shorter C codes for any constants, or tiny C codes for z(3), or any other classical constant. Send any new tiny code to : xgourdon@yahoo.fr or to psebah@yahoo.fr.

All the following programs (except the wonderful tiny program that computes Euler constant g) use the so called Spigot-Algorithm [1], that is, algorithms for which the digits are calculated and printed one at the time.

1  Computation of p

1.1  C programs

The following tiny C code computes digits of p. Boris Gourevitch kindly sent me the information that this program is from Dik T. Winter (cwi institute, Holland).
int a=10000,b,c=8400,d,e,f[8401],g;main(){
The program has 158 characters long. Finding the algorithm used is not so easy. Sebastian Wedeniws worked hard to find a shorter program (142 characters) :
main(){int a=1e4,c=3e3,b=c,d=0,e=0,f[3000],g=1,h=0;
Jean-Charles Meyrignac pointed me out a tiny code from the page http://www.isr.umd.edu/~jasonp/pigjerr. The program is by Gjerrit Meinsma, of 141 characters long, and computes 1000 digits of pi.
long k=4e3,p,a[337],q,t=1e3;

1.2  Assembly programs

Michal Majer improved his assembly program and afforded an tiny executable (MSDOS i386) of just 121 bytes that computes 9280 digits of pi. The program can be downloaded here : pi.com. Assembly source code is also available.

2  Computation of e

This is a tiny C program from Xavier Gourdon (Jul 1999) to compute 9000 decimal digits of e on your computer.

main(){int N=9009,n=N,a[9009],x;while(--n)a[n]=1+1/n;

This program has 117 characters (try to do better !). It can be changed to compute more digits (change the value 9009 to more) and to be faster (change the constant 10 to another power of 10 and the printf command). A not so obvious question is to find the algorithm used.

3  Computation of log(2)

The two following programs are short C program from Pascal Sebah (Jan 2000) to compute 2400 decimal places of log(2). In the two algorithms, the number of digits is given by log10(a).n where a is the working base (here a = 103 or less if you want to compute more digits) and c is the numbers of iteration required, the size of the array f[] is c+1.

The first program is based on the hypergeometric representation of x-1log(1-x) with x = 1/2 (see [1]) :

main(){int a=1000,b=0,c=7973,d,f[7974],n=800,k;

The second program is almost the same but deduced from an alternating series of log(2) given in the essay on this number, it's about three time faster than the previous one :

main(){int a=1000,b=0,c=2658,d=75,f[2659],n=800,k;

More digits may be given by those programs, just change the value of n and proportionally the value of c. For example, the last program, with n = 4000 and c = 13288, will give about 12000 digits.

4  Computation of square root of 2 and j

With exactly the same pattern, P. Sebah also gave tiny codes for the computations of 2 and the Golden Ratio j = (1+5)/2. The meaning of the parameters is the same as for log(2), the number of calculated digits is 2400.

The algorithm to compute 2 is based on the relation

2 = 7

1- 1



and on the expansion

(1-x)-m = 1+mx+ m(m+1)

it produces the following program :

main(){int a=1000,b=0,c=1413,d,f[1414],n=800,k;

And the algorithm to find the Golden Ratio is based on

j = 1+5
= 1

1- 1




main(){int a=1000,b=0,c=3434,d,f[3435],n=800,k;

5  Generalization

In fact the previous pattern is more general and may be easily adapted to many constants C defined by a Gaussian hypergeometric function [1] :

aF(a,b;c;z) = a

k = 0 
dk = a

1+ a.b
+ a(a+1).b(b+1)

dk-1 (a+k-1)(b+k-1)

with (a,b,c,z,a) being real numbers.

For example

dk-1 (m+k-1)

and with m = 1/2, x = 1/50 and a = 14 it becomes

14(1- 1
)-1/2 = 14F( 1
,b,b; 1
dk-1 (1/2+k-1)
= dk-1 2k-1
< dk-1

This is the justification of the following line used in the program to compute 2:


and the number of iteration is proportional to log(10)/log(50) 0.589.

6  Computation of Euler constant g

On april 1st of 2003, we receiced from Norihito Sasaki a wonderful tiny C program that computes digits of Euler constant. The program is 307 characters long and computes 1688 decimals of Euler constant :

int*d,*e,w[2850],p=475,b=10000,r=3,i,j,x;s(int k){d=w+k/14%4*p;
The approach does not make use of the spigot-algorithm and thus, is of a different family compared to tiny codes presented in the previous sections. The formula used is
g = a1 S(x1) + a2 S(x2) + a3 S(x3) +O(e-min{x1,x2,x3})
where S(x) denotes the series
S(x) =

n > 0 
(-1)n-1 xn
n! n
In the C code presented here, the parameters are defined by
x1 = 4096 = 212,    x2 = 3888 = 24 ×35,    x3 = 4374 = 2 ×37
a1 = -23,    a2 = 84,    a3 = -60.
The formula comes from the fact that for any x > 0,
g = S(x) - log(x) + O(e-x)
(formula used by Sweeney in 1963, see The Euler constant g for more details), and (1) is obtained by a linear combination of this formula. The parameters ai and xi are rational numbers (or even integers) choosen so that

ai = 1     and   

xiai = 1.
This property permits to have the logarithmic term vanishing, making (1) true.

To obtain more digits of g with this program, modifications of the parameters (ai) and (xi) are possible. For example, to obtain 22797 digits of g, one can choose

(x1,x2,x3) = (52488, 55296, 59049)     and     (a1,a2,a3) = (110, -30, -79).
A list of other possible choices for these parameters, together with a more precise description of the approach, are available in very nice  notes by Norihito Sasaki.

7  Prime numbers

In may 2003, Pascal Sebah and Sébastien Dauby wrote two very short programs to generate consecutive prime numbers 2, 3, 5, 7, . The first program is a very tiny C code of just 68 characters but is very slow (it just relies on the trivial definition of prime numbers : p is prime if it has no divisors i such 1 < i < p)

main(){for(int p=1,i;++p<1e5;i>p?printf("%d ",p):0)for(i=2;p%i++;);}

A more decent version (in terms of speed) consists in a tiny implementation of the Erathostene sieve of just 119 characters that permits to list a significant number of primes :

main(){int a[100000],i=1e5,n=i,p=1;while(--i)a[i]=1;
while(++p<n)for(a[p]?printf("%d ",i=p):0;p<4e4&&i*p<n;a[i++*p]=0);}

8  Interesting tiny programs in other mathematical areas

The following interesting tiny C program (477 bytes) is from Fabrice Bellard and prints the biggest prime number that was known in the year 2000 : 26972593-1 (note : this is no longer the biggest known prime number). Fabrice Bellard won the International Obfuscated C Code Contest of Year 2000 with this code.

int m=754974721,N,t[1<<22],a,*p,i,e=4625195,j,s,b,c,U;f(d){for(s

It should be noted that this program uses integer FFT so that it can run in a reasonable time.

The following informations are quoted from Fabrice Bellard page :

I compiled it successfully with gcc with i86 Linux and Sparc Solaris. It takes about 9 minutes on a Pentium 120 and 2 minutes on a Pentium II/450. In order to compile it, your C compiler must support the 64 bit long long type.

This program basically converts from base 2 to base 10. It is a non trivial task because it deals with numbers of millions of digits. The usual method (with repeated divisions by 10N) would be far too slow. So I decided to use a Integer Fast Fourier Transform. I believe it is one of the smallest implementation of such an algorithm.

More can be found on Fabrice Bellard page.


M. Abramowitz and I. Stegun, Handbook of Mathematical Functions, Dover, New York, (1964)

S. Rabinowitz, A Spigot-Algorithm for p, Abstract of the American Mathematical Society, (1991), vol. 12, p. 30

Back to

numbers, constants and computation

File translated from TEX by TTH, version 2.32.
On 28 May 2003, 20:44.