Jump to content

EKG sequence convergence: Difference between revisions

Line 655:
EKG(5) and EKG(7) converge at term 21
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[NextInSequence, EKGSequence]
NextInSequence[seq_List] := Module[{last, new = 1, holes, max, sel, found, i},
last = Last[seq];
max = Max[seq];
holes = Complement[Range[max], seq];
sel = SelectFirst[holes, Not[CoprimeQ[last, #]] &];
If[MissingQ[sel],
i = max;
found = False;
While[! found,
i++;
If[Not[CoprimeQ[last, i]],
found = True
]
];
Append[seq, i]
,
Append[seq, sel]
]
]
EKGSequence[start_Integer, n_] := Nest[NextInSequence, {1, start}, n - 2]
 
Table[EKGSequence[s, 10], {s, {2, 5, 7, 9, 10}}] // Grid
 
s = Reverse[Transpose[{EKGSequence[5, 1000], EKGSequence[7, 1000]}]];
len = LengthWhile[s, Apply[Equal]];
s //= Reverse[Drop[#, len]] &;
Length[s] + 1</lang>
{{out}}
<pre>1 2 4 6 3 9 12 8 10 5
1 5 10 2 4 6 3 9 12 8
1 7 14 2 4 6 3 9 12 8
1 9 3 6 2 4 8 10 5 15
1 10 2 4 6 3 9 12 8 14
 
21</pre>
 
=={{header|Nim}}==
1,111

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.