-
Notifications
You must be signed in to change notification settings - Fork 0
/
060.js
87 lines (74 loc) · 2.54 KB
/
060.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// The primes 3, 7, 109, and 673, are quite remarkable. By taking any two
// primes and concatenating them in any order the result will always be prime.
// For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of
// these four primes, 792, represents the lowest sum for a set of four primes
// with this property.
// Find the lowest sum for a set of five primes for which any two primes
// concatenate to produce another prime.
function isPrime(n) {
if (n < 2) return false;
var q = Math.floor(Math.sqrt(n));
for (var i = 2; i <= q; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
function nthPrime(n) {
var num = 1;
var i = 0;
while (i < n) {
num++;
while (!isPrime(num)) {
num++;
}
i++;
}
return num;
}
var nthPrimeMemo = (function(){
var memo = [];
return function(n) {
var num = memo[n];
if (typeof num === 'undefined') {
num = nthPrime(n);
memo[n] = num;
}
return num;
};
})();
function concatenable(a, b) {
return isPrime(parseInt('' + a + b, 10)) && isPrime(parseInt('' + b + a, 10));
}
function findLowestSetOfPrimes(limit) {
var primes = [];
var i, j, k, l, m;
for (i = 1; i < limit; i++) {
primes[0] = nthPrimeMemo(i);
for (j = i+1; j < limit; j++) {
primes[1] = nthPrimeMemo(j);
if (!concatenable(primes[0], primes[1])) continue;
for (k = j+1; k < limit; k++) {
primes[2] = nthPrimeMemo(k);
if (!concatenable(primes[1], primes[2])) continue;
if (!concatenable(primes[0], primes[2])) continue;
for (l = k+1; l < limit; l++) {
primes[3] = nthPrimeMemo(l);
if (!concatenable(primes[2], primes[3])) continue;
if (!concatenable(primes[1], primes[3])) continue;
if (!concatenable(primes[0], primes[3])) continue;
for (m = l+1; m < limit; m++) {
primes[4] = nthPrimeMemo(m);
if (!concatenable(primes[3], primes[4])) continue;
if (!concatenable(primes[2], primes[4])) continue;
if (!concatenable(primes[1], primes[4])) continue;
if (!concatenable(primes[0], primes[4])) continue;
console.log( primes );
}
}
}
}
}
}
findLowestSetOfPrimes(2000);