EKG sequence convergence
1
class Seq(terms, callback) {
2
method next {
3
terms += callback(terms)
4
}
5
6
method nth(n) {
7
while (terms.len < n) {
8
self.next
9
}
10
terms[n-1]
11
}
12
13
method first(n) {
14
while (terms.len < n) {
15
self.next
16
}
17
terms.first(n)
18
}
19
}
20
21
func next_EKG (s) {
22
2..Inf -> first {|k|
23
!(s.contains(k) || s[-1].is_coprime(k))
24
}
25
}
26
27
func EKG (start) {
28
Seq([1, start], next_EKG)
29
}
30
31
func converge_at(ints) {
32
var ekgs = ints.map(EKG)
33
34
2..Inf -> first {|k|
35
(ekgs.map { .nth(k) }.uniq.len == 1) &&
36
(ekgs.map { .first(k).sort }.uniq.len == 1)
37
}
38
}
39
40
for k in [2, 5, 7, 9, 10] {
41
say "EKG(#{k}) = #{EKG(k).first(10)}"
42
}
43
44
for arr in [[5,7], [2, 5, 7, 9, 10]] {
45
var c = converge_at(arr)
46
say "EKGs of #{arr} converge at term #{c}"
47
}
Copied!

Output:

1
EKG(2) = [1, 2, 4, 6, 3, 9, 12, 8, 10, 5]
2
EKG(5) = [1, 5, 10, 2, 4, 6, 3, 9, 12, 8]
3
EKG(7) = [1, 7, 14, 2, 4, 6, 3, 9, 12, 8]
4
EKG(9) = [1, 9, 3, 6, 2, 4, 8, 10, 5, 15]
5
EKG(10) = [1, 10, 2, 4, 6, 3, 9, 12, 8, 14]
6
EKGs of [5, 7] converge at term 21
7
EKGs of [2, 5, 7, 9, 10] converge at term 45
Copied!
Last modified 1yr ago
Copy link
Contents
Output: