Skip to main content
fix comment on the code relevant to the -2 bytes just applied to the Java version
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
x(x*),                  # \1 = divisor-1; tail = dividend
(                       # \2 = what will be the quotient
    (                   # \3 = the following (after the first iteration has finished),
                        #      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
            |
                (?!\3)  # Match this alternative only if this is the first iteration of
                        # the loop, meaning \4 is unset; \3 can never not match here if
                        # \3 is set (because \3==1) unless \1==0, in which case it
                        # doesn't matter if the abovethis alternative is taken instead, of the
                        # becauseabove – in that case, the value of \4 isn't changed 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*)                    # \5 = remainder
x(x*),                  # \1 = divisor-1; tail = dividend
(                       # \2 = what will be the quotient
    (                   # \3 = the following (after the first iteration has finished),
                        #      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
            |
                (?!\3)  # Match this alternative only if this is the first iteration of
                        # the loop, meaning \4 is unset; \3 can never not match here if
                        # \3 is set (because \3==1) unless \1==0, in which case it
                        # doesn't matter if the above alternative is taken instead,
                        # because in that case, the value of \4 isn't changed 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*)                    # \5 = remainder
x(x*),                  # \1 = divisor-1; tail = dividend
(                       # \2 = what will be the quotient
    (                   # \3 = the following (after the first iteration has finished),
                        #      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
            |
                (?!\3)  # Match this alternative only if this is the first iteration of
                        # the loop, meaning \4 is unset; \3 can never not match here if
                        # \3 is set (because \3==1) unless \1==0, in which case it
                        # doesn't matter if this alternative is taken instead of the
                        # above – in that case, the value of \4 isn't changed 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*)                    # \5 = remainder
-2 bytes on Java version
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52

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

Regex (Java), 41 40 bytes

x(x*),((x(?=(\4\1|(?!\5)\1)()))*)\4?(x*)

Try it online!

This is a port of the Perl/PCRE regex to a flavor that has no conditionals. Emulating a conditional costs 5 bytes here. The quotient and remainder are returned in the capture groups \2 and \6, respectively.

x(x*),                    # \1 = divisor-1; tail = dividend
(                         # \2 = what will be the quotient
    (
        x                 # tail -= 1
        (?=
            (             # \4 = running total
                \4        # recall the previous contents of \4 (only if it is set)
                \1        # \4 += divisor-1
            |
                (?!\5)    # match this alternative only if \4 is unset
                \1        # \4 = divisor-1
            )
            ()            # \5 = set to indicate that \4 is set
        )
    )*                    # 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 = remainder

Regex (Java), 41 40 38 bytes

x(x*),((x(?=(\4\1|(?!\3)\1)))*)\4?(x*)

Try it online!

This is a port of the Perl/PCRE regex to a flavor that has no conditionals. Emulating a conditional costs 5 3 bytes here. The quotient and remainder are returned in the capture groups \2 and \5, respectively.

x(x*),                  # \1 = divisor-1; tail = dividend
(                       # \2 = what will be the quotient
    (                   # \3 = the following (after the first iteration has finished),
                        #      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
            |
                (?!\3)  # Match this alternative only if this is the first iteration of
                        # the loop, meaning \4 is unset; \3 can never not match here if
                        # \3 is set (because \3==1) unless \1==0, in which case it
                        # doesn't matter if the above alternative is taken instead,
                        # because in that case, the value of \4 isn't changed 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*)                    # \5 = remainder
update mrab-regex link; use the elephant emoji I've established to mean "outputs by capture count"
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52

Regex (Pythonregexregex / Ruby), 43 bytes

This is a port of the Perl/PCRE regex to flavors that have no support for nested backreferences. Python's built-in re module does not even support forward backreferences, so for Python this requires regexregex.

Regex 🐘 (.NET), 14 bytes

Regex (Pythonregex / Ruby), 43 bytes

This is a port of the Perl/PCRE regex to flavors that have no support for nested backreferences. Python's built-in re module does not even support forward backreferences, so for Python this requires regex.

Regex (.NET), 14 bytes

Regex (Pythonregex / Ruby), 43 bytes

This is a port of the Perl/PCRE regex to flavors that have no support for nested backreferences. Python's built-in re module does not even support forward backreferences, so for Python this requires regex.

Regex 🐘 (.NET), 14 bytes

add .NET capture-count version
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
typo fix
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
added 8 characters in body
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
use the format previously used for indicating "import regex"
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
add Ruby TIO; fix range in Python TIO
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
add Python/Ruby version
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
-1 bytes - silly me, don't need a possessive quantifier when nothing would cause it to backtrack anyway
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
Add Java version, 41 bytes; change Perl TIO to go up to 350/350 instead of just 255/255
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
Add Java version, 41 bytes; change Perl TIO to go up to 350/350 instead of just 255/255
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
linkify the ECMAScript solution, since they're now on separate pages when sorted by Votes
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
fix Perl TIO
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
Add .NET specific version
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
Oops, copy pasted this from the other explanation and forgot to edit it.
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52
Loading