EKG sequence convergence: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 38: | Line 38: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="11l">F ekg(n, limit) |
||
Set[Int] values |
Set[Int] values |
||
assert(n >= 2) |
assert(n >= 2) |
||
Line 72: | Line 72: | ||
convIndex = i |
convIndex = i |
||
L.break |
L.break |
||
print(‘EKG(5) and EKG(7) converge at index ’convIndex‘.’)</ |
print(‘EKG(5) and EKG(7) converge at index ’convIndex‘.’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 86: | Line 86: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit |
||
BYTE FUNC Contains(BYTE ARRAY a BYTE len,b) |
BYTE FUNC Contains(BYTE ARRAY a BYTE len,b) |
||
Line 200: | Line 200: | ||
PrintF("converge at index %B",conv) |
PrintF("converge at index %B",conv) |
||
FI |
FI |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/EKG_sequence_convergence.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/EKG_sequence_convergence.png Screenshot from Atari 8-bit computer] |
||
Line 215: | Line 215: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
with Ada.Containers.Generic_Array_Sort; |
with Ada.Containers.Generic_Array_Sort; |
||
Line 330: | Line 330: | ||
end if; |
end if; |
||
end; |
end; |
||
end EKG_Sequences;</ |
end EKG_Sequences;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 344: | Line 344: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 418: | Line 418: | ||
printf("\nEKG5(5) and EKG(7) do not converge within %d terms\n", LIMIT); |
printf("\nEKG5(5) and EKG(7) do not converge within %d terms\n", LIMIT); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 434: | Line 434: | ||
===The Function=== |
===The Function=== |
||
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)] |
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Generate EKG Sequences. Nigel Galloway: December 6th., 2018 |
// Generate EKG Sequences. Nigel Galloway: December 6th., 2018 |
||
let EKG n=seq{ |
let EKG n=seq{ |
||
Line 445: | Line 445: | ||
yield! seq[1;n]; let g=fU n in yield! EKG g (fG g|>Seq.minBy snd)} |
yield! seq[1;n]; let g=fU n in yield! EKG g (fG g|>Seq.minBy snd)} |
||
let EKGconv n g=Seq.zip(EKG n)(EKG g)|>Seq.skip 2|>Seq.scan(fun(n,i,g,e)(l,β)->(Set.add l n,Set.add β i,l,β))(set[1;n],set[1;g],0,0)|>Seq.takeWhile(fun(n,i,g,e)->g<>e||n<>i) |
let EKGconv n g=Seq.zip(EKG n)(EKG g)|>Seq.skip 2|>Seq.scan(fun(n,i,g,e)(l,β)->(Set.add l n,Set.add β i,l,β))(set[1;n],set[1;g],0,0)|>Seq.takeWhile(fun(n,i,g,e)->g<>e||n<>i) |
||
</syntaxhighlight> |
|||
</lang> |
|||
===The Task=== |
===The Task=== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
EKG 2 |> Seq.take 45 |> Seq.iter(printf "%2d, ") |
EKG 2 |> Seq.take 45 |> Seq.iter(printf "%2d, ") |
||
EKG 3 |> Seq.take 45 |> Seq.iter(printf "%2d, ") |
EKG 3 |> Seq.take 45 |> Seq.iter(printf "%2d, ") |
||
Line 456: | Line 456: | ||
EKG 10 |> Seq.take 45 |> Seq.iter(printf "%2d, ") |
EKG 10 |> Seq.take 45 |> Seq.iter(printf "%2d, ") |
||
printfn "%d" (let n,_,_,_=EKGconv 2 5|>Seq.last in ((Set.count n)+1) |
printfn "%d" (let n,_,_,_=EKGconv 2 5|>Seq.last in ((Set.count n)+1) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 465: | Line 465: | ||
</pre> |
</pre> |
||
===Extra Credit=== |
===Extra Credit=== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
prıntfn "%d" (EKG 2|>Seq.takeWhile(fun n->n<>104729) ((Seq.length n)+1) |
prıntfn "%d" (EKG 2|>Seq.takeWhile(fun n->n<>104729) ((Seq.length n)+1) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 476: | Line 476: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2019-10-06}} |
{{works with|Factor|0.99 2019-10-06}} |
||
< |
<syntaxhighlight lang="factor">USING: combinators.short-circuit formatting fry io kernel lists |
||
lists.lazy math math.statistics prettyprint sequences |
lists.lazy math math.statistics prettyprint sequences |
||
sequences.generalizations ; |
sequences.generalizations ; |
||
Line 498: | Line 498: | ||
{ 2 5 7 9 10 } 20 show-ekgs nl |
{ 2 5 7 9 10 } 20 show-ekgs nl |
||
"EKG(5) and EKG(7) converge at term " write |
"EKG(5) and EKG(7) converge at term " write |
||
5 7 100 converge-at .</ |
5 7 100 converge-at .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 511: | Line 511: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 582: | Line 582: | ||
} |
} |
||
fmt.Println("\nEKG5(5) and EKG(7) do not converge within", limit, "terms") |
fmt.Println("\nEKG5(5) and EKG(7) do not converge within", limit, "terms") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 596: | Line 596: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (findIndex, isPrefixOf, tails) |
||
import Data.Maybe (fromJust) |
import Data.Maybe (fromJust) |
||
Line 644: | Line 644: | ||
) |
) |
||
) |
) |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>EKG (2) is [1,2,4,6,3,9,12,8,10,5,15,18,14,7,21,24,16,20,22,11] |
<pre>EKG (2) is [1,2,4,6,3,9,12,8,10,5,15,18,14,7,21,24,16,20,22,11] |
||
Line 654: | Line 654: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang="j"> |
|||
<lang j> |
|||
Until =: 2 :'u^:(0-:v)^:_' NB. unused but so fun |
Until =: 2 :'u^:(0-:v)^:_' NB. unused but so fun |
||
prime_factors_of_tail =: ~.@:q:@:{: |
prime_factors_of_tail =: ~.@:q:@:{: |
||
Line 691: | Line 691: | ||
assert (,2) -: prime_factors_of_tail 6 8 NB. (nub of) |
assert (,2) -: prime_factors_of_tail 6 8 NB. (nub of) |
||
assert 3 4 5 -: numbers_not_in_list 1 2 6 |
assert 3 4 5 -: numbers_not_in_list 1 2 6 |
||
</syntaxhighlight> |
|||
</lang> |
|||
Somewhat shorter is ekg2, |
Somewhat shorter is ekg2, |
||
<syntaxhighlight lang="j"> |
|||
<lang j> |
|||
index_of_lowest =: [: {. _ ,~ [: I. 1 e."1 prime_factors_of_tail e."1 q:@:numbers_not_in_list |
index_of_lowest =: [: {. _ ,~ [: I. 1 e."1 prime_factors_of_tail e."1 q:@:numbers_not_in_list |
||
Line 706: | Line 706: | ||
assert (ekg^:9&> -: ekg2^:9&>) 2 5 7 9 10 |
assert (ekg^:9&> -: ekg2^:9&>) 2 5 7 9 10 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java"> |
||
import java.util.ArrayList; |
import java.util.ArrayList; |
||
import java.util.Collections; |
import java.util.Collections; |
||
Line 789: | Line 789: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 812: | Line 812: | ||
'''Preliminaries''' |
'''Preliminaries''' |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
# jq optimizes the recursive call of _gcd in the following: |
# jq optimizes the recursive call of _gcd in the following: |
||
def gcd(a;b): |
def gcd(a;b): |
||
Line 821: | Line 821: | ||
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''The Task''' |
'''The Task''' |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
def areSame($s; $t): |
def areSame($s; $t): |
||
($s|length) == ($t|length) and ($s|sort) == ($t|sort); |
($s|length) == ($t|length) and ($s|sort) == ($t|sort); |
||
Line 868: | Line 868: | ||
; |
; |
||
task</ |
task</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 882: | Line 882: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
function ekgsequence(n, limit) |
function ekgsequence(n, limit) |
||
Line 910: | Line 910: | ||
[println(rpad("EKG($i): ", 9), join(ekgsequence(i, 30), " ")) for i in [2, 5, 7, 9, 10]] |
[println(rpad("EKG($i): ", 9), join(ekgsequence(i, 30), " ")) for i in [2, 5, 7, 9, 10]] |
||
println("EKGs of 5 & 7 converge at term ", convergeat(5, 7)) |
println("EKGs of 5 & 7 converge at term ", convergeat(5, 7)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}}<pre> |
{{output}}<pre> |
||
EKG(2): 1 2 4 6 3 9 12 8 10 5 15 18 14 7 21 24 16 20 22 11 33 27 30 25 35 28 26 13 39 36 |
EKG(2): 1 2 4 6 3 9 12 8 10 5 15 18 14 7 21 24 16 20 22 11 33 27 30 25 35 28 26 13 39 36 |
||
Line 922: | Line 922: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="scala">// Version 1.2.60 |
||
fun gcd(a: Int, b: Int): Int { |
fun gcd(a: Int, b: Int): Int { |
||
Line 970: | Line 970: | ||
} |
} |
||
println("\nEKG5(5) and EKG(7) do not converge within $LIMIT terms") |
println("\nEKG5(5) and EKG(7) do not converge within $LIMIT terms") |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 984: | Line 984: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[NextInSequence, EKGSequence] |
||
NextInSequence[seq_List] := Module[{last, new = 1, holes, max, sel, found, i}, |
NextInSequence[seq_List] := Module[{last, new = 1, holes, max, sel, found, i}, |
||
last = Last[seq]; |
last = Last[seq]; |
||
Line 1,011: | Line 1,011: | ||
len = LengthWhile[s, Apply[Equal]]; |
len = LengthWhile[s, Apply[Equal]]; |
||
s //= Reverse[Drop[#, len]] &; |
s //= Reverse[Drop[#, len]] &; |
||
Length[s] + 1</ |
Length[s] + 1</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 2 4 6 3 9 12 8 10 5 |
<pre>1 2 4 6 3 9 12 8 10 5 |
||
Line 1,022: | Line 1,022: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import algorithm, math, sets, strformat, strutils |
||
#--------------------------------------------------------------------------------------------------- |
#--------------------------------------------------------------------------------------------------- |
||
Line 1,064: | Line 1,064: | ||
echo fmt"EKG(5) and EKG(7) converge at index {convIndex}." |
echo fmt"EKG(5) and EKG(7) converge at index {convIndex}." |
||
else: |
else: |
||
echo "No convergence found in the first {convIndex} terms."</ |
echo "No convergence found in the first {convIndex} terms."</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,076: | Line 1,076: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use List::Util qw(none sum); |
||
sub gcd { my ($u,$v) = @_; $v ? gcd($v, $u%$v) : abs($u) } |
sub gcd { my ($u,$v) = @_; $v ? gcd($v, $u%$v) : abs($u) } |
||
Line 1,103: | Line 1,103: | ||
print "EKG($_): " . join(' ', EKG($_,10)) . "\n" for 2, 5, 7, 9, 10; |
print "EKG($_): " . join(' ', EKG($_,10)) . "\n" for 2, 5, 7, 9, 10; |
||
print "EKGs of 5 & 7 converge at term " . converge_at(5, 7) . "\n"</ |
print "EKGs of 5 & 7 converge at term " . converge_at(5, 7) . "\n"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>EKG(2): 1 2 4 6 3 9 12 8 10 5 |
<pre>EKG(2): 1 2 4 6 3 9 12 8 10 5 |
||
Line 1,114: | Line 1,114: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">LIMIT</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span> |
<span style="color: #008080;">constant</span> <span style="color: #000000;">LIMIT</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span> |
||
Line 1,147: | Line 1,147: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nEKG5(5) and EKG(7) %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">msg</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nEKG5(5) and EKG(7) %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">msg</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,163: | Line 1,163: | ||
If this alternate definition of function EKG_gen is used then the output would be the same as above. |
If this alternate definition of function EKG_gen is used then the output would be the same as above. |
||
Instead of keeping a cache of prime factors this calculates the gretest common divisor as needed. |
Instead of keeping a cache of prime factors this calculates the gretest common divisor as needed. |
||
< |
<syntaxhighlight lang="python">from itertools import count, islice, takewhile |
||
from math import gcd |
from math import gcd |
||
Line 1,196: | Line 1,196: | ||
for start in 2, 5, 7, 9, 10: |
for start in 2, 5, 7, 9, 10: |
||
print(f"EKG({start}):", str([n[0] for n in islice(EKG_gen(start), 10)])[1: -1]) |
print(f"EKG({start}):", str([n[0] for n in islice(EKG_gen(start), 10)])[1: -1]) |
||
print(f"\nEKG(5) and EKG(7) converge at term {find_convergence(ekgs=(5,7))}!")</ |
print(f"\nEKG(5) and EKG(7) converge at term {find_convergence(ekgs=(5,7))}!")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,211: | Line 1,211: | ||
Despite EKG(5) and EKG(7) seeming to converge earlier, as seen above; their hidden states differ.<br> |
Despite EKG(5) and EKG(7) seeming to converge earlier, as seen above; their hidden states differ.<br> |
||
Here is those series out to 21 terms where you can see them diverge again before finally converging. The state is also shown. |
Here is those series out to 21 terms where you can see them diverge again before finally converging. The state is also shown. |
||
< |
<syntaxhighlight lang="python"># After running the above, in the terminal: |
||
from pprint import pprint as pp |
from pprint import pprint as pp |
||
for start in 5, 7: |
for start in 5, 7: |
||
print(f"EKG({start}):\n[(<next>, [<state>]), ...]") |
print(f"EKG({start}):\n[(<next>, [<state>]), ...]") |
||
pp(([n for n in islice(EKG_gen(start), 21)]))</ |
pp(([n for n in islice(EKG_gen(start), 21)]))</syntaxhighlight> |
||
'''Generates:''' |
'''Generates:''' |
||
Line 1,269: | Line 1,269: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|Rakudo Star|2018.04.1}} |
{{works with|Rakudo Star|2018.04.1}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub infix:<shares-divisors-with> { ($^a gcd $^b) > 1 } |
||
sub next-EKG ( *@s ) { |
sub next-EKG ( *@s ) { |
||
Line 1,292: | Line 1,292: | ||
for [5, 7], [2, 5, 7, 9, 10] -> @ints { |
for [5, 7], [2, 5, 7, 9, 10] -> @ints { |
||
say "EKGs of (@ints[]) converge at term {$_+1}" with converge-at(@ints); |
say "EKGs of (@ints[]) converge at term {$_+1}" with converge-at(@ints); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,305: | Line 1,305: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program can generate and display several EKG sequences (with various starts).*/ |
||
parse arg nums start /*obtain optional arguments from the CL*/ |
parse arg nums start /*obtain optional arguments from the CL*/ |
||
if nums=='' | nums=="," then nums= 50 /*Not specified? Then use the default.*/ |
if nums=='' | nums=="," then nums= 50 /*Not specified? Then use the default.*/ |
||
Line 1,343: | Line 1,343: | ||
if done then iterate |
if done then iterate |
||
if wordpos(j, $)==0 then return j /*return an EKG integer.*/ |
if wordpos(j, $)==0 then return j /*return an EKG integer.*/ |
||
end /*j*/</ |
end /*j*/</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<!-- (output is shown '''<sup>5</sup>/<sub>6</sub>''' size.) !--> |
<!-- (output is shown '''<sup>5</sup>/<sub>6</sub>''' size.) !--> |
||
Line 1,360: | Line 1,360: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">class Seq(terms, callback) { |
||
method next { |
method next { |
||
terms += callback(terms) |
terms += callback(terms) |
||
Line 1,406: | Line 1,406: | ||
var c = converge_at(arr) |
var c = converge_at(arr) |
||
say "EKGs of #{arr} converge at term #{c}" |
say "EKGs of #{arr} converge at term #{c}" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,420: | Line 1,420: | ||
=={{header|Vlang}== |
=={{header|Vlang}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="vlang">fn gcd(aa int, bb int) int { |
||
mut a,mut b:=aa,bb |
mut a,mut b:=aa,bb |
||
for a != b { |
for a != b { |
||
Line 1,476: | Line 1,476: | ||
} |
} |
||
println("\nEKG5(5) and EKG(7) do not converge within $limit terms") |
println("\nEKG5(5) and EKG(7) do not converge within $limit terms") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,494: | Line 1,494: | ||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/sort" for Sort |
||
import "/math" for Int |
import "/math" for Int |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 1,538: | Line 1,538: | ||
} |
} |
||
} |
} |
||
System.print("\nEKG5(5) and EKG(7) do not converge within %(limit) terms.") </ |
System.print("\nEKG5(5) and EKG(7) do not converge within %(limit) terms.") </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,553: | Line 1,553: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
As can be seen, EKG(5) and EKG(7) converge at N = 21. |
As can be seen, EKG(5) and EKG(7) converge at N = 21. |
||
< |
<syntaxhighlight lang="xpl0">int N, A(1+30); |
||
func Used; int M; \Return 'true' if M is in array A |
func Used; int M; \Return 'true' if M is in array A |
||
Line 1,595: | Line 1,595: | ||
[Tbl:= [2, 5, 7, 9, 10]; |
[Tbl:= [2, 5, 7, 9, 10]; |
||
for I:= 0 to 4 do EKG(Tbl(I)); |
for I:= 0 to 4 do EKG(Tbl(I)); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,608: | Line 1,608: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Using gcd hint from Go. |
Using gcd hint from Go. |
||
< |
<syntaxhighlight lang="zkl">fcn ekgW(N){ // --> iterator |
||
Walker.tweak(fcn(rp,buf,w){ |
Walker.tweak(fcn(rp,buf,w){ |
||
foreach n in (w){ |
foreach n in (w){ |
||
Line 1,616: | Line 1,616: | ||
} |
} |
||
}.fp(Ref(N),List(),Walker.chain([2..N-1],[N+1..]))).push(1,N) |
}.fp(Ref(N),List(),Walker.chain([2..N-1],[N+1..]))).push(1,N) |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">foreach n in (T(2,5,7,9,10)){ println("EKG(%2d): %s".fmt(n,ekgW(n).walk(10).concat(","))) }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,626: | Line 1,626: | ||
EKG(10): 1,10,2,4,6,3,9,12,8,14 |
EKG(10): 1,10,2,4,6,3,9,12,8,14 |
||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang="zkl">fcn convergeAt(n1,n2,etc){ ns:=vm.arglist; |
||
ekgWs:=ns.apply(ekgW); ekgWs.apply2("next"); // pop initial 1 |
ekgWs:=ns.apply(ekgW); ekgWs.apply2("next"); // pop initial 1 |
||
ekgNs:=List()*vm.numArgs; // ( (ekg(n1)), (ekg(n2)) ...) |
ekgNs:=List()*vm.numArgs; // ( (ekg(n1)), (ekg(n2)) ...) |
||
Line 1,641: | Line 1,641: | ||
} |
} |
||
convergeAt(5,7); |
convergeAt(5,7); |
||
convergeAt(2,5,7,9,10);</ |
convergeAt(2,5,7,9,10);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |