EKG sequence convergence
The sequence is of natural numbers defined by:
a(1) = 1
;a(2) = Start = 2
;- for n > 2,
a(n)
shares at least one prime factor witha(n-1)
and is the smallest such natural number not already used.
The sequence is called the EKG sequence (after its visual similarity to an electrocardiogram when graphed).
Variants of the sequence can be generated starting 1, N where N is any natural number larger than one. For the purposes of this task let us call:
- The sequence described above , starting
1, 2, ...
theEKG(2)
sequence; - the sequence starting
1, 3, ...
theEKG(3)
sequence; - ... the sequence starting
1, N, ...
theEKG(N)
sequence.
Convergence
If an algorithm that keeps track of the minimum amount of numbers and their corresponding prime factors used to generate the next term is used, then this may be known as the generators essential state. Two EKG generators with differing starts can converge to produce the same sequence after initial differences.
EKG(N1)
and EKG(N2)
are said to to have converged at and after generation a(c)
if state_of(EKG(N1).(c)) == state_of(EKG(N2).a(c))
.
- Task
- Calculate and show here the first ten members of
EKG(2)
. - Calculate and show here the first ten members of
EKG(5)
. - Calculate and show here the first ten members of
EKG(7)
. - Stretch goal: Calculate and show here at which term EKG(5) and EKG(7) converge.
- Reference
- The EKG Sequence and the Tree of Numbers. (Video).
Python
<lang python>from itertools import count, islice, takewhile from functools import lru_cache
primes = [2, 3, 5, 7, 11, 13, 17] # Will be extended
@lru_cache(maxsize=2000) def pfactor(n):
# From; http://rosettacode.org/wiki/Count_in_factors if n == 1: return [1] n2 = n // 2 + 1 for p in primes: if p <= n2: d, m = divmod(n, p) if m == 0: if d > 1: return [p] + pfactor(d) else: return [p] else: if n > primes[-1]: primes.append(n) return [n]
def next_pfactor_gen():
"Generate (n, set(pfactor(n))) pairs for n == 2, ..." for n in count(2): yield n, set(pfactor(n))
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). """ def min_share_pf_so_far(pf, so_far): "searches the unused smaller number prime factors" nonlocal found for index, (num, pfs) in enumerate(so_far): if pf & pfs: found = so_far.pop(index) break else: found = () return found
yield 1, [] last, last_pf = start, set(pfactor(start)) pf_gen = next_pfactor_gen() # minimum list of prime factors needed so far so_far = list(islice(pf_gen, start)) found = () while True: while not min_share_pf_so_far(last_pf, so_far): so_far.append(pf_gen.__next__()) yield found[0], [n for n, _ in so_far] last, last_pf = found
- %%
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]) ekg5 = EKG_gen(start=5) ekg7 = EKG_gen(start=7) ekg5.__next__(), ekg7.__next__() # skip initial 1 differ = list(takewhile(lambda state: state[0] != state[1], zip(ekg5, ekg7))) print(f"\nEKG(5) and EKG(7) converge at term {len(differ) + 2}!")</lang>
- Output:
EKG(2): 1, 2, 4, 6, 3, 9, 12, 8, 10, 5 EKG(5): 1, 5, 10, 2, 4, 6, 3, 9, 12, 8 EKG(7): 1, 7, 14, 2, 4, 6, 3, 9, 12, 8 EKG(5) and EKG(7) converge at term 21!