Jump to content

EKG sequence convergence: Difference between revisions

→‎{{header|Python}}: Add method using math.gcd
(Added Kotlin)
(→‎{{header|Python}}: Add method using math.gcd)
Line 172:
 
=={{header|Python}}==
===Python: Using prime factor generation===
<lang python>from itertools import count, islice, takewhile
from functools import lru_cache
Line 251 ⟶ 252:
 
EKG(5) and EKG(7) converge at term 21!</pre>
 
===Python: Using math.gcd===
If this alternate definition of function EKG_gen is used (and the extra import), then the output would be the same as above:
<lang python>from math import gcd
 
def EKG_gen(start=2):
"""\
Generate the next term of the EKG together with the minimum cache of
numbers left in its production; (the "state" of the generator).
Using math.gcd
"""
c = count(start + 1)
last, so_far = start, list(range(2, start))
yield 1, []
yield last, []
while True:
for index, sf in enumerate(so_far):
if gcd(last, sf) > 1:
last = so_far.pop(index)
yield last, so_far
break
else:
so_far.append(next(c))</lang>
 
{{out}}
(Output is the same as above).
 
=={{header|zkl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.