## Online computation of the first primes and twin primes

Using the famous Sieve of Eratosthenes (discovered by the Greek Eratosthenes 276 BC-194 BC),  it is possible to perform an online computation of the first prime numbers with a small JavaScript program (you can view the source of this program using a command like View/Source in your browser). Therefore your browser must support JavaScript if you want to test this small application! At the end of the array of primes, the number p(x) of primes less than x founded is given. The primes may be computed up to x = 50000 (it may take a few seconds to perform) but this bound can be increased inside the program.

A nice interactive illustration of how the Eratosthenes' sieve works may be found here (also in JavaScript).

## Compute primes // // Very easy Sieves for finding all primes and twin primes by // Pascal Sebah (2000). // X and X2 are the bounds up to where the primes or twin primes are computed. // (if X or X2>100000 display must be changed). Only the odd numbers are sieved. // var X = 1000; var X2 = 1000; var piX = 0; var piX2 = 0; var Brun = 0.; // Sieve primes function Sieve () { var b, i, j, s, sX=Math.floor(Math.sqrt(X)); b = new Array (X+1); for (i=3; i<=X; i++) b[i] = 1; // Sieve for (i=3; i<=sX; i+=2) { if (b[i]==1) { s = 2*i; for (j=s+i; j<=X; j=j+s) b[j] = 0; } } // Get the primes piX = 1; var digits = ' 2'; for (i=3; i<=X; i+=2) { if (b[i]==1) { piX++; // Display if (i<10) digits = digits+' '+i; else if (i<100) digits = digits+' '+i; else if (i<1000) digits = digits+' '+i; else if (i<10000) digits = digits+' '+i; else digits = digits+' '+i; } } return digits; } // Sieve twin primes function Sieve2 () { var b, i, j, s, sX=Math.floor(Math.sqrt(X2)); b = new Array (X2+1); for (i=3; i<=X2; i++) b[i] = 1; // Sieve for (i=3; i<=sX; i+=2) { if (b[i]==1) { s = 2*i; for (j=s+i; j<=X2; j=j+s) b[j] = 0; } } // Get the twin primes piX2 = 0; Brun = 0.; var digits = ''; for (i=3; i<X2-1; i+=2) { j = i+2; if (b[i]==1 && b[j]==1) { piX2++; Brun += 1./i+1./j; // Display if (i<10) digits = digits+' '+i+' '+j; else if (i<100) digits = digits+' '+i+' '+j; else if (i<1000) digits = digits+' '+i+' '+j; else if (i<10000) digits = digits+' '+i+' '+j; else digits = digits+' '+i+' '+j; } } return digits; } // Change X or X2 function Prec (type, Digits) { if (type==1) X = Digits; else X2 = Digits; } // Printing : S is the width of the textarea function PrintD (toPrint, type) { var i, S=60, L = Math.floor(toPrint.length/S), section=''; for (i=0;i<toPrint.length; i+=S) section = section+toPrint.slice (i, i+S)+'\n'; if (type==1) document.forms.zone.value = section + '\n' + ' pi('+X+') = '+piX; else document.forms.zone.value = section + '\n' + ' pi2('+X2+') = '+piX2 +'\n' + ' Brun = ' + Brun; } // Sieve and print function primes (type) { if (type==1) digits = Sieve (); else digits = Sieve2 (); pfinal = digits.slice (1, digits.length); PrintD (pfinal, type); }

To compute the first primes click on the Compute primes button

 Primes up to x =   1000    5000    10000     100000

If you change the default number of primes click again on the Compute primes button to update the array of primes. If the number of searched primes is important it may take a few seconds to get the result of the computation.

## Compute twin primes

The same method allows to compute the twin primes, that is consecutive primes distant of 2. For example, 11 and 13 are twin primes. The number p2(x) of twin primes less than x and the computation of Brun's constant (that is the sum of the inverse of the twin primes) are also given at the end of the file. To find p2(x) for large values of x (say up to 1015 or more), note that most algorithms are variants of Eratosthenes' sieve.

To compute the first twin primes click on the Compute twin primes button

 Twin Primes up to x =   1000    5000    10000     100000

If you change the default number of twin primes click on the Compute twin primes button to update the array of twin primes.