The sequence is from the natural numbers and is defined by:

 a(1) = 1; 
 a(2) = Start = 2;
 for n > 2, a(n) shares at least one prime factor with a(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, ... the EKG(2) sequence;
 the sequence starting 1, 3, ... the EKG(3) sequence; 
 ... the sequence starting 1, N, ... the EKG(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).a(c)) == state_of(EKG(N2).a(c)).



Task

 Calculate and show here the first 10 members of EKG(2).
 Calculate and show here the first 10 members of EKG(5).
 Calculate and show here the first 10 members of EKG(7).
 Calculate and show here the first 10 members of EKG(9).
 Calculate and show here the first 10 members of EKG(10).
 Calculate and show here at which term EKG(5) and EKG(7) converge   (stretch goal).
Related Tasks

 Greatest common divisor
 Sieve of Eratosthenes


Reference

 The EKG Sequence and the Tree of Numbers. (Video).


INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
 
BYTE FUNC Contains(BYTE ARRAY a BYTE len,b)
  BYTE i
 
  IF len=0 THEN
    RETURN (0)
  FI
  FOR i=0 TO len-1
  DO
    IF a(i)=b THEN
      RETURN (1)
    FI
  OD
RETURN (0)
 
BYTE FUNC Gcd(BYTE a,b)
  BYTE tmp
 
  IF a<b THEN
    tmp=a a=b b=tmp
  FI
 
  WHILE b#0
  DO
    tmp=a MOD b
    a=b b=tmp
  OD
RETURN (a)
 
BYTE FUNC AreSame(BYTE ARRAY a,b BYTE len)
  BYTE i
 
  IF len=0 THEN
    RETURN (1)
  FI
 
  SortB(a,len,0)
  SortB(b,len,0)
  FOR i=0 TO len-1
  DO
    IF a(i)#b(i) THEN
      RETURN (0)
    FI
  OD
RETURN (1)
 
PROC CalcEkg(BYTE start,limit BYTE ARRAY ekg)
  BYTE len,i
 
  ekg(0)=1 ekg(1)=start
  FOR len=2 TO limit-1
  DO
    i=2
    DO
      IF Contains(ekg,len,i)=0 AND Gcd(ekg(len-1),i)>1 THEN
        ekg(len)=i
        EXIT
      FI
      i==+1
    OD
  OD
RETURN
 
BYTE FUNC CalcConvergence(BYTE ARRAY a,b BYTE len)
  BYTE i
 
  FOR i=2 TO len-1
  DO
    IF a(i)=b(i) AND AreSame(a,b,i)=1 THEN
      RETURN (i+1)
    FI
  OD
RETURN (0)
 
PROC PrintSeq(BYTE start BYTE ARRAY ekg BYTE len)
  BYTE i
 
  PrintF("EKG(%B)=",start)
  FOR i=0 TO len-1
  DO
    IF i>0 THEN Put(32) FI
    PrintB(ekg(i))
  OD
  PrintE("...")
RETURN
 
PROC Main()
  DEFINE PTR="CARD"
  DEFINE LIMIT="100"
  DEFINE SEQCOUNT="5"
  DEFINE PART="10"
  DEFINE EKG1="1"
  DEFINE EKG2="2"
  BYTE ARRAY buf(500),starts=[2 5 7 9 10]
  PTR ARRAY ekg(SEQCOUNT)
  BYTE i,conv
 
  Put(125) PutE() ;clear the screen
 
  FOR i=0 TO SEQCOUNT-1
  DO
    ekg(i)=buf+LIMIT*i
    CalcEkg(starts(i),LIMIT,ekg(i))
    PrintSeq(starts(i),ekg(i),PART)
  OD
 
  conv=CalcConvergence(ekg(EKG1),ekg(EKG2),LIMIT)
  PrintF("%EEKG(%B) and EKG(%B) ",starts(EKG1),starts(EKG2))
  IF conv=0 THEN
    PrintF("do not converge within %B items",LIMIT)
  ELSE
    PrintF("converge at index %B",conv)
  FI
RETURN