Anonymous user
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
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
def EKG_gen(start=2):
Line 274 ⟶ 276:
break
else:
so_far.append(next(c))
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}}
|