Sandwich Primes

node v10.24.1
version: 3.0.0
endpointsharetweet
Sandwich Primes - ( the term I coined ;)), inspired by this post by Pat Ballew https://www.linkedin.com/posts/pat-ballew-5b1935102_mathfacts-activity-6769661475427491841-F4n-, are primes p, such that 1p1, 3p3, 7p7 and 9p9 are all primes. This code finds all sandwich primes in the set of first 10000 primes.
// adapted from https://stackoverflow.com/a/15471749/983091 var eratosthenes = function(n) { // Eratosthenes algorithm to find all primes under n var array = [], upperLimit = Math.sqrt(n), map = {}, output = []; // Make an array from 2 to (n - 1) for (var i = 0; i < n; i++) { array.push(true); } // Remove multiples of primes starting from 2, 3, 5,... for (var i = 2; i <= upperLimit; i++) { if (array[i]) { for (var j = i * i; j < n; j += i) { array[j] = false; } } } // All array[i] set to true are primes for (var i = 2; i < n; i++) { if(array[i]) { map[i]=true; output.push(i); } } return [output, map]; }; let [primes, pMap] = eratosthenes(1000000) for (let i = 0; i < 10000; i++) { let p = primes[i] if (pMap[+('1' + p + '1')] && pMap[+('3' + p + '3')] && pMap[+('7' + p + '7')] && pMap[+('9' + p + '9')]) { console.log(p) } } console.log('done')
Loading…

no comments

    sign in to comment