-
Notifications
You must be signed in to change notification settings - Fork 1
/
p37.wren
79 lines (67 loc) · 2.03 KB
/
p37.wren
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
class Solution {
static isPrime(n) {
var i = 2
var max = n.sqrt.floor
while (i <= max) {
if ((n % i) == 0) {
return false
}
i = i + 1
}
return true
}
static yieldLeftTruncatables(previous, power) {
var list = []
for (i in 1..9) {
for (p in previous) {
var n = (i * power) + p
if (Solution.isPrime(n)) {
list.add(n)
Fiber.yield(n)
}
}
}
Solution.yieldLeftTruncatables(list, power * 10)
}
static yieldRightTruncatables(previous) {
var list = []
for (p in previous) {
for (i in 1..9) {
var n = (p * 10) + i
if (Solution.isPrime(n)) {
list.add(n)
Fiber.yield(n)
}
}
}
Solution.yieldRightTruncatables(list)
}
static yieldMerged(a, b) {
var x = a.call()
var y = b.call()
while (true) {
var advanceA = (x <= y)
var advanceB = (y <= x)
if (x == y) {
Fiber.yield(x)
}
x = advanceA ? a.call() : x
y = advanceB ? b.call() : y
}
}
static solve() {
// Recursively build up truncatable primes
var singleDigitPrimes = (2..9).where {|n| Solution.isPrime(n)}
var leftTruncatables = Fiber.new { Solution.yieldLeftTruncatables(singleDigitPrimes, 10) }
var rightTruncatables = Fiber.new { Solution.yieldRightTruncatables(singleDigitPrimes) }
// Merge the two (ordered) lists
var merged = Fiber.new { Solution.yieldMerged(leftTruncatables, rightTruncatables) }
// Problem description says there are 11 elements in the intersection of the two sets
var sum = 0
for (i in 1..11) {
sum = sum + merged.call()
}
System.print(sum)
}
}
Solution.solve()