EKG sequence convergence: Difference between revisions

→‎Python: Using math.gcd: Make entry a complete program to better highlight complexity difference.
(→‎Python: Using prime factor generation: Use next() instead of __next__())
(→‎Python: Using math.gcd: Make entry a complete program to better highlight complexity difference.)
Line 254:
 
===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:.
Instead of keeping a cache of prime factors this calculates the gretest common divisor as needed.
<lang python>from math import gcd
<lang python>from itertools import count, islice, takewhile
<lang python>from math import gcd
 
def EKG_gen(start=2):
Line 274 ⟶ 276:
break
else:
so_far.append(next(c))</lang>
 
def find_convergence(ekgs=(5,7)):
"Returns the convergence point or zero if not found within the limit"
ekg = [EKG_gen(n) for n in ekgs]
for e in ekg:
next(e) # skip initial 1 in each sequence
return 2 + len(list(takewhile(lambda state: not all(state[0] == s for s in state[1:]),
zip(*ekg))))
 
if __name__ == '__main__':
for start in 2, 5, 7:
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))}!")</lang>
 
{{out}}
Anonymous user