Regex (Java), 41 4040 38 bytes
x(x*),((x(?=(\4\1|(?!\5\3)\1)()))*)\4?(x*)
Try it online!Try it online!
This is a port of the Perl/PCRE regex to a flavor that has no conditionals. Emulating a conditional costs 55 3 bytes here. The quotient and remainder are returned in the capture groups \2
and \6\5
, respectively.
x(x*), # \1 = divisor-1; tail = dividend
( # \2 = what will be the quotient
( # \3 = the following (after the first iteration has finished),
x # which is always 1
x # tail -= 1
(?=
( # \4 = running total
\4 # recall the previous contents of \4 (only if it is set)
\1 # \4 += divisor-1
|
(?!\5\3) # matchMatch this alternative only if \4this is unsetthe first iteration of
\1 # the loop, meaning \4 =is divisor-1unset; \3 can never not match here if
)
# \3 is set (because \3==1) unless \1==0, in which case it
# \5doesn't =matter setif tothe indicateabove alternative is taken instead,
# because in that case, the value of \4 isisn't setchanged anyway.
\1 # \4 = divisor-1
)
)*
)* # Loop the above as many times as possible (zero or more); if
# it loops zero times, \4 will be unset (we'll treat that as 0)
)
\4? # tail -= \4, or leave tail unchanged if \4 is unset
(x*) # \6\5 = remainder