Regex (ECMAScript), 35 33 bytes
^(?=(.+)\1*,\1+$)(\1\B|\1{5})\2?,
Takes its input in unary, as the length of two strings of x
s delimited by a ,
specifying (\$k,n\$).
Try it online!
This uses a similar algorithm to Leo's Husk answer. We can't directly compute \$lcm(n,k)\$ since it can be larger than both the inputs, so instead we compute \$k/gcd(n,k)\$, which is conveniently equivalent to \$lcm(n,k)/n\$. All that remains is to assert that the result is in \$[2,5,10]\$.
^ # tail = K
(?=
(x+)\1*,\1+$ # \1 = greatest common factor of K and N
)
# Assert that K == \1*2, \1*5, or \1*10, i.e. that K/\1 == 2, 5, or 10
( # \2 = one of the below choices, \1 or \1*5
\1 # tail -= \1
\B # Assert tail > 0; this prevents matching on K == \1
| # or
\1{5} # tail -= \1*5
)
\2? # optionally, tail -= \2; this option must be taken if
# \2 == \1, because in that choice, the assertion was made
# that K > \1
, # Assert tail == 0
Regex (ECMAScript), 33 bytes
^(((x+)(\3{4})?)\2?),(?!\1+$)\3+$
Try it online!
By an interesting coincidence, there's another method that has the same length. It asserts that both:
- \$k\$ does not divide \$n\$
- At least one member of \$\{k, {k\over 2}, {k\over 5}, {k\over 10}\}\$ is an integer and divides \$n\$ (of course it's redundant to try to assert that for \$k\$, as it contradicts the previous assertion, but it results in a shorter regex)
^ # tail = K
# Divide \3 = K, K/2, K/5, or K/10
( # \1 = tail == K
( # \2 = \3 or \3*5
(x+) # \3 = any positive number satisfying the assertions below
(\3{4})? # optionally add \3*4 to \2
) # tail -= \2
\2? # optionally, tail -= \2
)
, # Assert tail == 0;
# tail = N
(?!\1+$) # Assert N is not divisible by K
\3+$ # Assert N is divisible by \3
Regex (Ruby), 31 bytes
^(((x+)\3{4}?)\2?),(?!\1+$)\3+$
Try it online!
This is a straight port of the above ECMAScript regex. Ruby allows multiple back-to-back quantifiers in situations where the meaning is unambiguous, so (\3{4})?
can be done as \3{4}?
instead. In other regex engines, the ?
in \3{4}?
would be interpreted as modifying the quantifier to be lazy (non-greedy), even though on a constant quantifier that has no effect.
Note that (A{N,M})?
cannot be changed to A{N,M}?
in Ruby, because in that case the ?
does act as a lazy modifier to the quantifier.
Regex (ECMAScript), 171 151 bytes
Just for kicks, let's see if we can port the algorithm from xnor's Python answer and many subsequent answers, which is to assert that \$n≢0\pmod k\ \land\ 10n≡0\pmod k\$. The challenge here is that we can't directly compute \$10n\$, since regex can't operate on numbers larger than any of the inputs (of course, we could if \$k\ge 10n\$, but in most cases it won't). So we need to emulate the calculation of \$10n\$ modulo \$k\$.
^(?=(x*),\1*(x*))(?=(?=\2\B(x*))(x*(?=\3\3)|\2\2))(?=((?=\4(x*))x*(?=\3\6)|\4\2))(?=((?=\5(x*))x*(?=\3\8)|\5\2))(?=((?=\7(x*))x*(?=\3\10)|\7\2))\9{2}\b
Try it online!
^ # tail = K
(?=(x*),\1*(x*)) # \1 = K; \2 = N % K
(?=
(?=
\2 # tail -= \2
\B # Assert 0 < \2 < K
(x*) # \3 = tail == K - \2 == also a tool to make tail = \2
)
( # \4 = (\2 + \2) % K == (N * 2) % K
x*(?=\3\3) # \4 = head = \2 - \3
| # if above failed due to K - \2 > \2 then fall back on:
\2\2 # \4 = \2 + \2
)
)
(?=((?=\4(x*))x*(?=\3\6 )|\4\2)) # \5 = (\4 + \2) % K == (N * 3) % K
(?=((?=\5(x*))x*(?=\3\8 )|\5\2)) # \7 = (\5 + \2) % K == (N * 4) % K
(?=((?=\7(x*))x*(?=\3\10)|\7\2)) # \9 = (\7 + \2) % K == (N * 5) % K
\9{2} # tail = tail - \9*2 == K - \9*2
\b # Assert tail==K or tail==0, which is equivalent to
# asserting (N * 5 * 2) % K == 0
This could be shortened greatly in regex flavors with forward-declared backreferences, but it'd still be much longer than 33 bytes.
\d+
$*
^(?=(.+)\1*,\1+$)(\1\B|\1{5})\2?,
Try it online!