Smallest unused number sharing a factor This is a pretty run of the mill question.  I will define a sequence and you golf some code to output a entry given a index.


The first item in the sequence is 2.
The nth item in the sequence is the smallest positive integer other than n and 1 sharing at least one factor with n (other than 1) that has not already appeared in the list.


Test cases

Here are the first 25 items in the sequence:

1  2
2  4
3  6
4  8
5  10
6  3
7  14
8  12
9  15
10 5
11 22
12 9
13 26
14 7
15 18
16 20
17 34
18 16
19 38
20 24
21 27
22 11
23 46
24 21
25 30


Related (offset by one) OEIS
Jelly, 19 bytes

R»2ɓ²Rg⁸&gt;1Tḟ⁸ḟḢṭµ/Ṫ


Try it online!