What's my lucky factor? Lucky numbers (A000959) are numbers generated by applying the following sieve:
Begin with the list of natural numbers:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, ...

Now, remove every second element (\$n = 2\$, the smallest element in the list aside from 1):
1,    3,    5,    7,    9,     11,     13,     15,     17,     19,     21,     23,     25, ...

Now, remove every third element (\$n = 3\$, the next remaining element after 2):
1,    3,          7,    9,             13,     15,             19,     21,             25, ...

Now, remove every seventh element (\$n = 7\$, the next remaining element after 3):
1,    3,          7,    9,             13,     15,                     21,             25, ...

Then continue removing the \$n\$th remaining numbers, where \$n\$ is the next number in the list after the last surviving number. Next in this example is 9, then 13 and so on.
Eventually, this converges to the lucky numbers:
1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, 87, 93, 99, ...


However, we've already looked at the lucky numbers. Today, we'll be looking at a related sequence: the lucky factors (A264940). These are the values of \$n\$ that remove a specific integer \$x\$.
For example, \$x = 2\$ is removed when \$n = 2\$, so \$2\$'s lucky factor is \$2\$
Additionally, \$x = 19\$ is removed when \$n = 7\$, so \$19\$'s lucky factor is \$7\$
If \$x\$ is lucky, its lucky factor is \$0\$.
The first 50 elements of this sequence are
0, 2, 0, 2, 3, 2, 0, 2, 0, 2, 3, 2, 0, 2, 0, 2, 3, 2, 7, 2, 0, 2, 3, 2, 0, 2, 9, 2, 3, 2, 0, 2, 0, 2, 3, 2, 0, 2, 7, 2, 3, 2, 0, 2, 13, 2, 3, 2, 0, 2


This is a standard sequence challenge. You may choose to:

Take a positive integer \$x\$ and output the lucky factor of \$x\$
Take a positive integer \$x\$ and output the lucky factors of each integer \$1 \le i \le x\$
Output the infinite list of lucky factors

This is code-golf so the shortest code in bytes wins

Test cases
 x  n
15  0
57  9
26  2
41  3
50  2
13  0
48  2
20  2
19  7
22  2
24  2
27  9
60  2
54  2
49  0
 2  2
 7  0
45 13
55 15
Python 2, 72 bytes
Takes a positive integer \$ x \$ and outputs the lucky factor of \$ x \$. Outputs via exit code.

x=input()
R=range(1,x+1)
for i in R:i+=i&lt;2;del R[i-1::i];x&gt;R[-1]&lt;exit(i)

Try it online!