EKG sequence convergence: Difference between revisions
Content added Content deleted
(Remove late addition to task.) |
(Added Sidef) |
||
Line 634: | Line 634: | ||
(start 10): 1 10 4 6 3 9 12 8 14 7 21 15 5 20 16 18 22 11 33 24 26 13 39 27 30 25 35 28 32 34 17 51 36 38 19 57 42 40 44 46 23 69 45 48 50 52 54 56 49 63 |
(start 10): 1 10 4 6 3 9 12 8 14 7 21 15 5 20 16 18 22 11 33 24 26 13 39 27 30 25 35 28 32 34 17 51 36 38 19 57 42 40 44 46 23 69 45 48 50 52 54 56 49 63 |
||
</pre> |
|||
=={{header|Sidef}}== |
|||
{{trans|Perl 6}} |
|||
<lang ruby>class Seq(terms, callback) { |
|||
method next { |
|||
terms += callback(terms) |
|||
} |
|||
method nth(n) { |
|||
while (terms.len < n) { |
|||
self.next |
|||
} |
|||
terms[n-1] |
|||
} |
|||
method first(n) { |
|||
while (terms.len < n) { |
|||
self.next |
|||
} |
|||
terms.first(n) |
|||
} |
|||
} |
|||
func next_EKG (s) { |
|||
2..Inf -> first {|k| |
|||
!(s.contains(k) || s[-1].is_coprime(k)) |
|||
} |
|||
} |
|||
func EKG (start) { |
|||
Seq([1, start], next_EKG) |
|||
} |
|||
func converge_at(ints) { |
|||
var ekgs = ints.map(EKG) |
|||
2..Inf -> first {|k| |
|||
(ekgs.map { .nth(k) }.uniq.len == 1) && |
|||
(ekgs.map { .first(k).sort }.uniq.len == 1) |
|||
} |
|||
} |
|||
for k in [2, 5, 7, 9, 10] { |
|||
say "EKG(#{k}) = #{EKG(k).first(10)}" |
|||
} |
|||
for arr in [[5,7], [2, 5, 7, 9, 10]] { |
|||
var c = converge_at(arr) |
|||
say "EKGs of #{arr} converge at term #{c}" |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
EKG(2) = [1, 2, 4, 6, 3, 9, 12, 8, 10, 5] |
|||
EKG(5) = [1, 5, 10, 2, 4, 6, 3, 9, 12, 8] |
|||
EKG(7) = [1, 7, 14, 2, 4, 6, 3, 9, 12, 8] |
|||
EKG(9) = [1, 9, 3, 6, 2, 4, 8, 10, 5, 15] |
|||
EKG(10) = [1, 10, 2, 4, 6, 3, 9, 12, 8, 14] |
|||
EKGs of [5, 7] converge at term 21 |
|||
EKGs of [2, 5, 7, 9, 10] converge at term 45 |
|||
</pre> |
</pre> |
||