-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe60.html
84 lines (79 loc) · 1.68 KB
/
e60.html
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
<html>
<head>
<script>
function isPrime(x) {
if (x < 2) {
return false
} else if (x % 2 == 0) {
return x == 2
}
for (var i = 3; i * i <= x; ++i) {
if (x % i == 0) {
return false;
}
}
return true;
}
function mag(x) {
return x < 10 ? 10 : 10 * mag(x / 10)
}
function concat(x, y) {
return x * mag(y) + y
}
function getPrimes(N) {
p = new Array()
for (var i = 2; i <= N; ++i) {
if (isPrime(i)) {
p.push(i)
}
}
return p
}
function satisfies(p,s) {
for (var i = 0; i < s.length; ++i) {
if (!(isPrime(concat(s[i],p)) && isPrime(concat(p,s[i])))) {
return false
}
}
return true
}
function setSum(set) {
sum = 0;
for (var i = 0; i < set.length; ++i) {
sum += set[i]
}
return sum
}
function scopy(set) {
return set.slice(0)
}
primes = getPrimes(10000)
function finishSet(set, target, start) {
if (set.length == 5) {
test = setSum(set)
return test < target ? test : target
} else {
for (var i = start; i < primes.length; ++i) {
p = primes[i]
if (satisfies(p,set)) {
cset = scopy(set)
cset.push(p)
test = finishSet(cset,target,i)
if (test < target) {
target = test
break
}
}
}
}
return target
}
function euler60() {
ans = finishSet(new Array(),10000000,0)
console.log(ans)
alert(ans)
}
</script>
</head>
<body onload="euler60()" />
</html>