EKG sequence convergence: Difference between revisions

Content added Content deleted
(add couple of related task)
Line 308: Line 308:
EKGs of (5 7) converge at term 21
EKGs of (5 7) converge at term 21
EKGs of (2 5 7 9 10) converge at term 45
EKGs of (2 5 7 9 10) converge at term 45
</pre>

=={{header|Phix}}==
{{trans|C}}
<lang Phix>constant LIMIT = 100
constant starts = {2, 5, 7, 9, 10}
sequence ekg = {}
string fmt = "EKG(%2d): ["&join(repeat("%d",min(LIMIT,30))," ")&"]\n"
for s=1 to length(starts) do
ekg = append(ekg,{1,starts[s]}&repeat(0,LIMIT-2))
for n=3 to LIMIT do
-- a potential sequence member cannot already have been used
-- and must have a factor in common with previous member
integer i = 2
while find(i,ekg[s])
or gcd(ekg[s][n-1],i)<=1 do
i += 1
end while
ekg[s][n] = i
end for
printf(1,fmt,starts[s]&ekg[s][1..min(LIMIT,30)])
end for
-- now compare EKG5 and EKG7 for convergence
constant EKG5 = find(5,starts),
EKG7 = find(7,starts)
string msg = sprintf("do not converge within %d terms", LIMIT)
for i=3 to LIMIT do
if ekg[EKG5][i]=ekg[EKG7][i]
and sort(ekg[EKG5][1..i-1])=sort(ekg[EKG7][1..i-1]) then
msg = sprintf("converge at term %d", i)
exit
end if
end for
printf(1,"\nEKG5(5) and EKG(7) %s\n", msg)</lang>
{{out}}
<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( 5): [1 5 10 2 4 6 3 9 12 8 14 7 21 15 18 16 20 22 11 33 24 26 13 39 27 30 25 35 28 32]
EKG( 7): [1 7 14 2 4 6 3 9 12 8 10 5 15 18 16 20 22 11 33 21 24 26 13 39 27 30 25 35 28 32]
EKG( 9): [1 9 3 6 2 4 8 10 5 15 12 14 7 21 18 16 20 22 11 33 24 26 13 39 27 30 25 35 28 32]
EKG(10): [1 10 2 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]

EKG5(5) and EKG(7) converge at term 21
</pre>
</pre>