user
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

1. Calculate and show here the first 10 members of `EKG(2)`.
2. Calculate and show here the first 10 members of `EKG(5)`.
3. Calculate and show here the first 10 members of `EKG(7)`.
4. Calculate and show here the first 10 members of `EKG(9)`.
5. Calculate and show here the first 10 members of `EKG(10)`.
6. Calculate and show here at which term `EKG(5)` and `EKG(7)` converge   (**stretch goal**).

Related Tasks

1. Greatest common divisor
2. Sieve of Eratosthenes
3. Yellowstone sequence

Reference

* The EKG Sequence and the Tree of Numbers. (Video).
assistant
(formerly Perl 6)

**Works with**: Rakudo Star version 2018.04.1

```
sub infix:<shares-divisors-with> { ($^a gcd $^b) > 1 }

sub next-EKG ( *@s ) {
    return first {
        @s ∌ $_  and  @s.tail shares-divisors-with $_
    }, 2..*;
}

sub EKG ( Int $start ) {  1, $start, &next-EKG … *  }

sub converge-at ( @ints ) {
    my @ekgs = @ints.map: &EKG;

    return (2 .. *).first: -> $i {
        [==]  @ekgs.map(     *.[$i]     ) and
        [===] @ekgs.map( *.head($i).Set )
    }
}

say "EKG($_): ", .&EKG.head(10) for 2, 5, 7, 9, 10;

for [5, 7], [2, 5, 7, 9, 10] -> @ints {
    say "EKGs of (@ints[]) converge at term {$_+1}" with converge-at(@ints);
}

```

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(9): (1 9 3 6 2 4 8 10 5 15)
EKG(10): (1 10 2 4 6 3 9 12 8 14)
EKGs of (5 7) converge at term 21
EKGs of (2 5 7 9 10) converge at term 45

```