Last active
February 19, 2017 16:08
-
-
Save Satyam/5d8cd9c78e71b9b9e1f39e01aed97048 to your computer and use it in GitHub Desktop.
Prime birthdays on prime years
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const THIS_YEAR = 2017; | |
const MAX_AGE = 123; | |
// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes | |
function sieveOfErathosthenes(max) { | |
const notPrimes = []; | |
const top = Math.sqrt(max); | |
const primes = []; | |
for (let i = 2; i < top; i += 1) { | |
if (!notPrimes[i]) { | |
for (let j = i * i; j < max; j += i) { | |
notPrimes[j] = true; | |
} | |
} | |
} | |
for (let i = 2; i < max; i += 1) { | |
if (!notPrimes[i]) primes.push(i); | |
} | |
return primes; | |
} | |
const primes = sieveOfErathosthenes(THIS_YEAR + MAX_AGE); | |
// For all years starting a century+ ago ... | |
for (let yearOfBirth = THIS_YEAR - MAX_AGE; yearOfBirth <= THIS_YEAR; yearOfBirth += 1) { | |
let headingPrinted = false; | |
let age = 1; | |
let total = 0; | |
let sets = 0; | |
// for all ages below the maximum | |
while (age < MAX_AGE) { | |
// Check whether both the age and the year are prime numbers | |
const indexYear = primes.indexOf(age + yearOfBirth); | |
const indexAge = primes.indexOf(age); | |
if (indexYear !== -1 && indexAge !== -1) { | |
let i = 0; | |
let gapYear; | |
let gapAge; | |
// Check if the successive years and ages are spaced the same | |
do { | |
i += 1; | |
gapYear = primes[indexYear + i] - primes[indexYear]; | |
gapAge = primes[indexAge + i] - primes[indexAge]; | |
} while (gapAge === gapYear); | |
// If the index is two or more, then we have consecutive prime matches | |
// Print the results | |
if (i >= 2) { | |
total += i; | |
sets += 1; | |
if (!headingPrinted) { | |
headingPrinted = true; | |
console.log(`Born in ${yearOfBirth}`); | |
} | |
console.log(` ${i} consecutive:`); | |
for (let j = 0; j < i; j += 1) { | |
console.log(` ${primes[indexAge + j]} in ${primes[indexYear + j]}`); | |
} | |
// Skip over to the year of the last successful match | |
age = primes[indexAge + (i - 1)]; | |
} | |
} | |
// Try out the next age | |
age += 1; | |
} | |
if (total) { | |
console.log(` which makes ${sets} sets of primes for a total of ${total}`); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Born in 1896 | |
4 consecutive: | |
97 in 1993 | |
101 in 1997 | |
103 in 1999 | |
107 in 2003 | |
which makes 1 sets of primes for a total of 4 | |
Born in 1900 | |
2 consecutive: | |
73 in 1973 | |
79 in 1979 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1902 | |
2 consecutive: | |
29 in 1931 | |
31 in 1933 | |
2 consecutive: | |
97 in 1999 | |
101 in 2003 | |
which makes 2 sets of primes for a total of 4 | |
Born in 1904 | |
2 consecutive: | |
83 in 1987 | |
89 in 1993 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1908 | |
2 consecutive: | |
41 in 1949 | |
43 in 1951 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1912 | |
2 consecutive: | |
61 in 1973 | |
67 in 1979 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1914 | |
2 consecutive: | |
17 in 1931 | |
19 in 1933 | |
3 consecutive: | |
73 in 1987 | |
79 in 1993 | |
83 in 1997 | |
2 consecutive: | |
89 in 2003 | |
97 in 2011 | |
which makes 3 sets of primes for a total of 7 | |
Born in 1920 | |
2 consecutive: | |
11 in 1931 | |
13 in 1933 | |
2 consecutive: | |
29 in 1949 | |
31 in 1951 | |
2 consecutive: | |
53 in 1973 | |
59 in 1979 | |
2 consecutive: | |
79 in 1999 | |
83 in 2003 | |
2 consecutive: | |
107 in 2027 | |
109 in 2029 | |
which makes 5 sets of primes for a total of 10 | |
Born in 1926 | |
2 consecutive: | |
5 in 1931 | |
7 in 1933 | |
2 consecutive: | |
47 in 1973 | |
53 in 1979 | |
4 consecutive: | |
61 in 1987 | |
67 in 1993 | |
71 in 1997 | |
73 in 1999 | |
2 consecutive: | |
101 in 2027 | |
103 in 2029 | |
2 consecutive: | |
113 in 2039 | |
127 in 2053 | |
which makes 5 sets of primes for a total of 12 | |
Born in 1928 | |
2 consecutive: | |
3 in 1931 | |
5 in 1933 | |
2 consecutive: | |
83 in 2011 | |
89 in 2017 | |
which makes 2 sets of primes for a total of 4 | |
Born in 1932 | |
2 consecutive: | |
17 in 1949 | |
19 in 1951 | |
2 consecutive: | |
67 in 1999 | |
71 in 2003 | |
which makes 2 sets of primes for a total of 4 | |
Born in 1934 | |
2 consecutive: | |
53 in 1987 | |
59 in 1993 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1938 | |
2 consecutive: | |
11 in 1949 | |
13 in 1951 | |
2 consecutive: | |
59 in 1997 | |
61 in 1999 | |
2 consecutive: | |
73 in 2011 | |
79 in 2017 | |
which makes 3 sets of primes for a total of 6 | |
Born in 1940 | |
2 consecutive: | |
47 in 1987 | |
53 in 1993 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1942 | |
2 consecutive: | |
31 in 1973 | |
37 in 1979 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1944 | |
2 consecutive: | |
5 in 1949 | |
7 in 1951 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1946 | |
2 consecutive: | |
3 in 1949 | |
5 in 1951 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1950 | |
2 consecutive: | |
23 in 1973 | |
29 in 1979 | |
2 consecutive: | |
43 in 1993 | |
47 in 1997 | |
2 consecutive: | |
61 in 2011 | |
67 in 2017 | |
which makes 3 sets of primes for a total of 6 | |
Born in 1956 | |
5 consecutive: | |
31 in 1987 | |
37 in 1993 | |
41 in 1997 | |
43 in 1999 | |
47 in 2003 | |
2 consecutive: | |
71 in 2027 | |
73 in 2029 | |
which makes 2 sets of primes for a total of 7 | |
Born in 1958 | |
2 consecutive: | |
53 in 2011 | |
59 in 2017 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1962 | |
2 consecutive: | |
37 in 1999 | |
41 in 2003 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1964 | |
2 consecutive: | |
23 in 1987 | |
29 in 1993 | |
2 consecutive: | |
47 in 2011 | |
53 in 2017 | |
which makes 2 sets of primes for a total of 4 | |
Born in 1968 | |
2 consecutive: | |
29 in 1997 | |
31 in 1999 | |
2 consecutive: | |
59 in 2027 | |
61 in 2029 | |
which makes 2 sets of primes for a total of 4 | |
Born in 1974 | |
2 consecutive: | |
19 in 1993 | |
23 in 1997 | |
3 consecutive: | |
107 in 2081 | |
109 in 2083 | |
113 in 2087 | |
which makes 2 sets of primes for a total of 5 | |
Born in 1980 | |
4 consecutive: | |
13 in 1993 | |
17 in 1997 | |
19 in 1999 | |
23 in 2003 | |
2 consecutive: | |
31 in 2011 | |
37 in 2017 | |
2 consecutive: | |
83 in 2063 | |
89 in 2069 | |
4 consecutive: | |
101 in 2081 | |
103 in 2083 | |
107 in 2087 | |
109 in 2089 | |
which makes 4 sets of primes for a total of 12 | |
Born in 1986 | |
4 consecutive: | |
7 in 1993 | |
11 in 1997 | |
13 in 1999 | |
17 in 2003 | |
2 consecutive: | |
41 in 2027 | |
43 in 2029 | |
3 consecutive: | |
97 in 2083 | |
101 in 2087 | |
103 in 2089 | |
which makes 3 sets of primes for a total of 9 | |
Born in 1988 | |
2 consecutive: | |
23 in 2011 | |
29 in 2017 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1990 | |
2 consecutive: | |
73 in 2063 | |
79 in 2069 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1992 | |
3 consecutive: | |
5 in 1997 | |
7 in 1999 | |
11 in 2003 | |
which makes 1 sets of primes for a total of 3 | |
Born in 1994 | |
2 consecutive: | |
3 in 1997 | |
5 in 1999 | |
which makes 1 sets of primes for a total of 2 | |
Born in 1998 | |
2 consecutive: | |
29 in 2027 | |
31 in 2029 | |
which makes 1 sets of primes for a total of 2 | |
Born in 2002 | |
2 consecutive: | |
61 in 2063 | |
67 in 2069 | |
which makes 1 sets of primes for a total of 2 | |
Born in 2004 | |
2 consecutive: | |
79 in 2083 | |
83 in 2087 | |
2 consecutive: | |
107 in 2111 | |
109 in 2113 | |
which makes 2 sets of primes for a total of 4 | |
Born in 2010 | |
2 consecutive: | |
17 in 2027 | |
19 in 2029 | |
2 consecutive: | |
53 in 2063 | |
59 in 2069 | |
2 consecutive: | |
71 in 2081 | |
73 in 2083 | |
2 consecutive: | |
101 in 2111 | |
103 in 2113 | |
which makes 4 sets of primes for a total of 8 | |
Born in 2016 | |
2 consecutive: | |
11 in 2027 | |
13 in 2029 | |
2 consecutive: | |
47 in 2063 | |
53 in 2069 | |
3 consecutive: | |
67 in 2083 | |
71 in 2087 | |
73 in 2089 | |
which makes 3 sets of primes for a total of 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment