Searching for Prime Twins with JavaScript:

Table of ContentsDoing Math with JavaScript

Prime twins (or twin primes) are odd prime numbers that come in pairs: {3, 5}, {11, 13}, {17, 19}, {29, 31}. Primes in each twin pair differ by two. (The exceptional pair {2, 3} is not usually included in prime twins.) One can prove that each prime twin pair except {3, 5} has the form {6x − 1, 6x + 1} for a certain integer x. Most mathematicians believe that there are infinitely many prime twins. This is the so-called twin prime conjecture; nobody has proved it thus far.

You are welcome to investigate the properties of prime twins right here using JavaScript. Click the Run button to find prime twins by calling the nextPrimeTwin(n) in the left column:

Here is the source code of the JavaScript function nextPrimeTwin(n) for finding the smallest prime twin greater than n. This function relies on isPrime to test whether a number is prime.

// function nextPrimeTwin(n) returns:
// * 3 if n<3 or
// * 5 if n<5 or
// * the smallest twin prime 6i-1 greater than n, for an integer i
// * NaN if such a prime is not a representable integer

function nextPrimeTwin(n) {
 if (isNaN(n) || !isFinite(n)) return NaN; 
 if (n<3) return 3;
 if (n<5) return 5;
 for (var i=6*Math.ceil(Math.floor(n+2)/6); i<9007199254740880; i+=6) {
  if (pscreen(i-1) && pscreen(i+1) && isPrime(i-1) && isPrime(i+1))
    return i-1;
 return NaN;

function pscreen(n) {
 // screen out most non-primes early on
 if (n<=109
 || n%3 && n%5 && n%7 && n%11 && n%13 && n%17 && n%19
 && n%23 && n%29 && n%31 && n%37 && n%41 && n%43 && n%47
 && n%53 && n%59 && n%61 && n%67 && n%71 && n%73 && n%79
 && n%83 && n%89 && n%97 && n%101 && n%103 && n%107 && n%109) {
  return true;  
 return false;

A Famous Prime Twin
The prime number 824633702441, which is a prime twin, is perhaps the most expensive number in history. This number helped discover the floating-point division bug in the Intel Pentium processor, which eventually has cost Intel 475 million dollars! Professor Thomas R. Nicely, the discoverer of the bug, recalls:
4 Oct 1994 A new error is noticed: the FPU values for the sum of the reciprocals of the twins, as computed on the Pentium-60 and a 486DX-33, diverge within the first 10^12. After several days, the discrepancy is tracked down to the twin prime pair (824633702441, 824633702443), and it is noted that the elementary operation 1/824633702441 is returning an incorrect value from the FPU in C++.
17 October The code is tested on a colleague's brand new Pentium, and the same error is noted. The error does not appear on 486s. The new Pentium has an Intel motherboard; mine has a Micronics motherboard.
18 October The error is reproduced on the Pentium in Power Basic and Quattro Pro, thus is not language dependent. It disappears when the FPU is disabled...
22 October A third Pentium system displays the error – a Packard-Bell system on display at Staples' office supply. It is confirmed in the Microsoft Works spreadsheet...
7 November Alexander Wolfe's article appears in Electronic Engineering Times. The matter is now fully public.
21 November Steve Young, chief financial correspondent for CNN Cable News, is the first mainstream media journalist to break the story of the Pentium FDIV flaw and its implications for Intel. The story is then picked up by other national and international media.
30 November Intel releases an in-house study of the flaw, "Statistical Analysis of Floating Point Flaw in the Pentium Processor (1994)," H. P. Sharangpani and M. L. Barton, Intel Corporation. This study minimizes the potential impact of the flaw on the vast majority of users, a conclusion with which I largely agree.
12 December IBM releases its own study of the potential impact of the flaw, challenging Intel's analysis and concluding that the flaw will seriously impact the work of a large number of users both within and outside the scientific community. My own analysis is closer to Intel's position.
20 December In response to a firestorm of public opinion, Intel announces plans for a total recall, replacement, and destruction of the flawed Pentium processors.
17 Jan 1995 Intel announces a pre-tax charge of 475 million dollars against earnings, ostensibly the total cost associated with replacement of the flawed processors.
(The above timeline is part of the Pentium Bug FAQ that has been written and placed in the public domain by Prof. Thomas R. Nicely.)

Copyright © 1999-2011,