Revision ba6d0ac1-7e7d-48a9-bf43-14110d76e8ce - Code Golf Stack Exchange
# Regex (Perl/PCRE+`(?^=)`<sup><sup>[RME](https://github.com/Davidebyzero/RegexMathEngine)</sup></sup>), 36 bytes
```
(?=((2円x+?|^x)(?^=2円+$))+)(2円x+)3円+$
```
[Try it on replit.com](https://replit.com/@Davidebyzero/regex-Abundant-numbers-with-lookinto) (RegexMathEngine)
Takes its input in unary, as the length of a string of `x`s.
This is based on my 43 byte .NET version; for the full explanation, see below. Lookinto, a new feature in RegexMathEngine, allows `(?<=(?=2円+$)^.*)` to be shortened to the equivalent `(?^=2円+$)`.
This is not just the shortest, but the fastest under RegexMathEngine, even though the 43 byte .NET regex it's based on is the slowest under .NET (beaten by both 44 byte regexes in speed).
# Regex (Java / .NET) / [Retina](https://github.com/m-ender/retina), 43 bytes
```
(?=((2円x+?|^x)(?<=(?=2円+$)^.*))+)(2円x+)3円+$
```
[Try it online!](https://tio.run/##bVLvb9owEP3ev@IWdcIm4AamfZkboW3apEnrNpWPQCWTOODWcTLbKYGufzs7h@xHJfLlcnfP7/ye7148ivF9/nBUZV1ZD/eYM1WxIYf/K41X@mzNyo1ssXNRN2utMsi0cA5uhDLwBH3NeeExPFYqhxI7ZO6tMhsQduMWKwp@a6udg09tJmuvKjx5AfBZaQlFauSu@yURy6pcMuxHlH9oikJamd9KkUsL6w72skj@nOzTglLez3Ujz3fbwE@IS9eoQeRflZGE0lepabSmqiCOyZ@N0I5EV8PhMKL06SX0xECIP0vwhAz@LwMSXCHDGnEP3MXpYGkGIXr@fKo9/xDeS2vApv0fqi3rMMBRjm6sq0pLYeCQFsgoOcwzYQxKV6ZuPKQQ1PY1Mt87L0umDOXQ6@xgbCvcN9l6vGZnMUBviMa7I8cJZBDRS@z7ixVobAcUc7VWnkRjfATEezAJdr4Yj2tgWS2sk5gQvUhWdARmcrbJtDQbv72ewgwCEt5hmKyQMdsKu1i1vZ4uM5MVh/fWir1jhdKatKNBO@hMASgqG7ThNVKTcDDXqZlgiGMUeCN8tkWHSmSzrDxlpNtcgwv@EclPG8N2VtSkH5FV9f57cSvMRuKkZGQoDUoLICWONzl6F972QHuTK3RsZ5WXJDwq5YfU2ya8z792jR76gkSvXYSWUP4cvouwVUcySwlZTtt49uuupWR2nWJlOY0v6R0bUhrTrkmXb@LLY1iiYzKevk1@Aw "Java (JDK) – Try It Online") - Java (very slow)
[Try it online!](https://tio.run/##RY/RaoQwEEV/RWTAzKaRaN/WBhf63C9QF8TOroE0kZhF2a3fbnVL6eM5c7ncGdxEfuzJmBW8qjxdaW6OR0sTOyUrKxVjdT7z8vs8Iyvf1GbqnAOe0wMix@cR61cOa3J6id/d16ANfcZYwF3J4uI8tV3PwETaRqDtcAv4gFaBEeNgdIhFXOgLgzbt3M0GYUKU7wGuoK1ks2wFDKyqtA3N0xRghaE/znbmHB97h08/2tD1NLJkTg5g8Vff8TF5HUj0bgzLNisr/jkS1m2/Gm0pArssyypFJqX8AQ "PowerShell – Try It Online") - Regex (.NET)
[Try it online!](https://tio.run/##HckxCsMwDADAXb8IyCBZONhJMxQSPPYTwqjQDl06lA4a8q4@oB9zQ9e71/39eF5L15tAdJBgjTxy0uICWL4fxwyBLHmETnUj0sml7s2Z6rodopMgtzEyC/@TdRbsmAdgGxvAxQYI5j2neclwPi0/ "Retina – Try It Online") - Retina
This is 2 bytes shorter than [Martin Ender's answer](https://codegolf.stackexchange.com/a/110732/17216). Its structure is quite different, and it's more portable to different regex engines.
It works by first adding up as many proper divisors of N as it can, in strict order from smallest to largest, while having a sum not greater than N. Then it checks if there exists a larger proper divisor than the last one added to the sum; if there does, N is an abundant number.
```
# For the purpose of these comments, the input number will be referred to as N.
# No anchor needed, because 2円 cannot be captured if
# matching is started anywhere besides the beginning.
(?=
( # Add up all values of 2円 until there is no more room in
# N to keep going
(2円x+?|^x) # 2円 = the smallest value greater than the previous value
# of 2円 which satisfies the following condition; if we're
# at the beginning, 2円 = 1
(?<= # Lookbehind - evaluated from right to left
(?=2円+$) # Assert that N is divisible by 2円
^.* # Jump to the beginning of the input string, then execute
# the above lookahead
)
)+
)
(2円x+) # Match if there exists another proper divisor of N, 3,円
3円+$ # such that 3円 is greater than the last value of 2円. If
# there does, that means there was insufficient room in N
# to add up all of its proper divisors, and N is abundant.
```
# Regex (Python<sup>[`regex`](https://bitbucket.org/mrabarnett/mrab-regex/src/hg/)</sup><sub><sup> / .NET</sup></sub>)<sub><sup> / [Retina](https://github.com/m-ender/retina)</sup></sub>, 47 bytes
```
(?=((3円x+?|^x)(?<=(?=2円+$)^.*(2円)))+)(2円x+)4円+$
```
**[Try it online!](https://tio.run/##LY7BjsIwDETv/Qor4mCTtkqAU7tRP4SChCBApJBWSQ6h2n/vJiwn2zPzRp7f8Tm5/Wpe8@QjhHeovX7o1INXnjG24qAQx33iw@85EQ4/Kivjjm/o3G5x3BERpzwTp/HAN2tmjrJr5KmHRYnqPnmwYFxpbkO8GddVYJVtw2xNRNYwqqCEXAn5i3toNC6iPYoT1f9bLiMuKYNg7vB5rw364q9P9DWwxLbuYxZ36WD2BaMiLEr239vV2t0UY7SKRgoh/gA "Python 3 – Try It Online") - Python <sub><sup>`import regex`</sup></sub>**
[Try it online!](https://tio.run/##RY/RaoQwEEV/RWTAzKaRaPdpbXChz/0CdUG22RpIJxKzKGv9dqstpU/DOVwud3o3aj902toVvKq8/tBTczqRHtk5WVmpGKufJ15@XSZk5YvaTJ1zwEt6YHWOiBy3O3GsjxzW5PwUv7rP3lj9HmMBDyWLm/O6vXYMbGQoAkP9PeAMrQIrht6aEIu4MDcGbXp1dwrChijfA1xBW8lm2QoYkKoMhebHFEDC6j/OduYc573Dp29tuHZ6YMmUHIDwVz9wHr0JWnRuCMs2Kyv@ORLktn@tIR0BLcuySpFJKb8B "PowerShell – Try It Online") - .NET
[Try it online!](https://tio.run/##Dcg7CsMwDADQXbcIyCBZONj5DIUEj72EMCq0Q5cOpYOGnqsH6MWcTA/e@/F5vm6l610gOkiwRh45aXEBLP@fY4ZAljxCp7oT6exSv82Z6rafo5MgtzGSTswsfOrCugh2zAOwjQ3gagME857TvGa4LOsB "Retina – Try It Online") - Retina
This is a port of the 43 byte Java/.NET version, avoiding a nested backreference in order to add Python<sup>[`regex`](https://bitbucket.org/mrabarnett/mrab-regex/src/hg/)</sup> support. The way in which this is done (placing the copy inside the lookbehind) removes Java support.
# Regex (Perl / [PCRE2](https://www.pcre.org/) <sub><sup>v10.35+</sup></sub>), 49 bytes
This is a direct port of the .NET regex, emulating variable-length lookbehind using a recursive subroutine call.
```
(?=((2円x+?|^x)((?<=(?=^2円+$|(?3)).)))+)(2円x+)4円+$
```
[Try it online!](https://tio.run/##RY5BT8MwDIX/ijVFa6y0NC1wIcnaSePKiRvdooFWKVLYSlKkoqz89ZKVSbtYfs/P9tcdnH2cPn@AOAHBnj72FkguolRys35dr0ZoT44Sr7iQK4EBTBsVhs6ZYw@L5rgQI9RW@c6aniZZkvrvd9/HFZ1mRVrg4esSqm4ujz4@EY3icsvHj3tXW1liqO1bsVWx8q24/jVXSYxU8zh2jGEwLVCaDAkMEEOIoH4hJy7HQPxyOcPNbBG8EP@sxIhxHLV@ftloPdFKUdqUA6vOuwEpraSK1q4pGTnT6h7xDhEZzhFsHhiZJp4VnPM/ "Perl 5 – Try It Online") - Perl (slow)
[Attempt This Online!](https://ato.pxeger.com/run?1=XVDBSsNAEMVrv2IJC5kxSZtUD8JmyUWFgnhQb027xLhpAukmJCkE2n6JF0H0n_RrnKQ9edjdNzPvzZvZ9886rz9-L1ZhRIDxhklmzSw2ZXWjN6rRdZmkGuzZ9DJSKk_KTqXVti5K3cQQo4ifZq3tMptORkm10QPBdNp0LSh1v3i4UwpdFiC1pMZiklUN8EL6gpcyI3oLzy-3i0cUuGdFBrxctl1TakMIvWAlpRUbC4nc7l6pQmnXd70AB73u67J602B5lkt0Qfq02plu0IZzEi2pAd3-SkwYG53NOeYmlGOdkOMgWTMYV94mXZoDb1wyG_bXSQd2b7vcICLbDyMWqNO8GuYSjFYJBBtixo04ks3x3z8Biq9dl3k3PwFEEiCe9050WPcIEIWSUut47vADRFeIU_JwcKRgfO3wk_Dj_Hz7XuD7_in6Aw) - PCRE2 v10.40+
```
(?=
(
(2円x+?|^x)
((?<= # (?3) calls this
(?= # Circumvent the constant-width limitation of lookbehinds
# in PCRE by using a lookahead inside the lookbehind
^2円+$ # This is the payload of the emulated variable-length
# lookbehind, same as the one in the .NET regex
|
(?3) # Recursive call - this is the only alternative that can
# match until we reach the beginning of the string
)
. # Go back one character at a time, trying the above
# lookahead for a match each time
))
)+
)
(2円x+)
4円+$
```
# Regex (<sub><sup>Java / Python<sup>[`regex`](https://bitbucket.org/mrabarnett/mrab-regex/src/hg/)</sup> / .NET</sup></sub>)<sub><sup> / [Retina](https://github.com/m-ender/retina)</sup></sub>, 50 bytes
```
(?=((?=(3円|^))(2円x+?)(?<=(?=3円+$)^.*))+)(3円x+)4円+$
```
[Try it online!](https://tio.run/##bVJdb9MwFH3vr7hEQ7Wb1usHvOBFFSCQkBig7bHtJDdxWm@OE2xnzVr628t1Gj4mLVJk33uuz/U5vvfiUYzus4eTKqrSerjHmKmSDTj8n6m90i/mrNzIBpFeVa@1SiHVwjm4FsrAAbqc88Lj8liqDApEyK23ymxA2I1brCj4rS13Dj41qay8KvFkD@Cz0hLyxMhduyURS8tMMsQjyj/UeS6tzG6kyKSFdVv2PEn@nOzCnFLe9XVDz3fbwE@IS9aoQWRflZGE0leJqbWmKieOyZ@10I5El4PBIKL08Lz0zECIf5HggAz@LwMSXCLDGuseuIuT/tL0w@r58Zw7/hDeS2vAJt0O1RZVaOAoRzfWZamlMLBPcmSUHG5TYQxKV6aqPSQQ1HY5cvvkvCyYMpRDp7MtY1vhvsnG4zVbiwE6QzTeHTnORQYrOokdvliBRjhUMVdp5Uk0wkfAeg9mjMgX43EMLKuEdRIDohfjFR2CmbwIMi3Nxm@vpjCHUAnvcJmskDHdCrtYNZ2eNjKTFYf31oonx3KlNWmG/abfmgKQlzZow2skZszBXCVmgksco8Br4dMtOlQgm2XFOSLt5Boc8I9Ifp4YtrOiIl2LtKyevuc3wmwkdhoPDaVBaQ6kwPYmQ@/C2@5pZ3KJju2s8pKER6V8n3hbh/f5B1fooc9J9NpFaAnlx/D1wlSdyDwh4V/Oft1RSpbTJp5TMr9KMLmcxRf0jg0ojRGZNTFdvokvTmGWTuPR9O34Nw "Java (JDK) – Try It Online") - Java (very slow)
[Try it online!](https://tio.run/##LY7BysIwEITvfYoleMiatiR/PbWGPohVkN@ogZiWTQ61@O41UQ8Lu/PNDDs94330zWof00gRwjOUZG5m7oA0McZW3mueZ2heJ0Q@/M2iR97vdRKHRmzwVG8RRSLNLHDYic2aYgfVVurYwaJlcR0JHFify@sQL9a3BTjt6jA5GzmrGBaQTT6b6OxvhlsfuTvII5bfLZWhUJiCYK/w@bAO5kz/d04lsJlt/QdmurQwUY5hFhatut/tS@MvmjFcZaWklG8 "Python 3 – Try It Online") - Python <sub><sup>`import regex`</sup></sub>
[Try it online!](https://tio.run/##RY/RaoQwEEV/RWTAzKaRuNuntcGFPvcL1AWx2RpIJxKzKGv9dqstpQ/zcM4dLjO9G7UfOm3tCl6VXn/oqT6fSY/skqysUGyf6vR1RWTVceIFsuJFbbI6ccBrekDkW3KaOFbPHNbk8hS/us/eWP0eYw4PJfOb87ppOwY2MhSBof4ecIZGgRVDb02IRZybG4Mmbd2dgrAhOu4LXEFTynrZChiQKg2F@sfkQMLqP8525hznvcOnb01oOz2wZEoOQPirHziP3gQtOjeEZTsry/85EuS2l60hHQEty7JKkUkpvwE "PowerShell – Try It Online") - .NET
This is a port of the 43 byte Java/.NET version, avoiding a nested backreference in order to add Python<sup>[`regex`](https://bitbucket.org/mrabarnett/mrab-regex/src/hg/)</sup> support, while keeping Java support.
# Regex (<sub><sup>Perl / </sup></sub>[PCRE](https://www.pcre.org/)), 58 bytes
Porting the PCRE2 regex to PCRE1 requires working around some bugs:
```
(?=((?=(3円|^))(2円x+?)((?<=(?=z|^3円+$|(?4)).)))+)(3円x+)5円+$
```
The `(2円x+?|^x)` had to be changed to `(?=(3円|^))(2円x+?)`, copying `3円` into `2円` and `2円` into `3円` (emulating a nested backref using a forward backref), because using a backreference inside its own group forces that group to be atomic in PCRE1, and in PCRE2 up to v10.34. And a fake always-false alternative `z` had to be added to the recursive subroutine to prevent the error message "recursive call could loop indefinitely" in PCRE1.
[Try it online!](https://tio.run/##RU7BboMwDP0Vq4pKrMAIZb0spFCpu@6022ijbipSpKxlCZPYUvbrLLBKO9jye372e@3JmvX4/gXECvDm8nY0QFIRoCx22@ftZoDmYilxkotiI9CDbgJC31p97mBRnxdigMpI1xrd0SiJYvf56rpwouIkizM8fUyi8p/lgccHolBMv1xwPNrKFCv0lXnJ9jJ0vhc3X32DRBdyXoeJMfS6AUqjPoIegggR5A@kxKboiVsu53BzthA8E39ZiRbDMCj1@LRTaqSlpFPV@fWASOtVz0oMTCED@3091DkjV1reI94hIguKvGdYrxkZR55knPNf "Perl 5 – Try It Online") - Perl (slow)
**[Try it online!](https://tio.run/##hVPvb9owEP3OX3FlbbGbUCUNq2hCirqu0z6wdkJU2gQ0YsEBa8GJnDBoC//62DmGNv0hLQLJPj@/e/fuHKZpfRKGmw9chPF8zKCVhpIdT88r4XQkIUj7Q/ChW/20WPz@5cx/3t//6E1PF1OyIW2fqP/AWd1RSgYnS6NNMdLyMfqwuhs4xv6KtBuUHlNKDUQ4S4MOPhr7G/qarWrCUeoHqWF7lWclWT7miZJSDkkuJi9jPMEoG83e4s4rXOSQ4jIPmJSJJGEishyK0o5mLMtGE0YfMY/rhgiAVgu2UbUs4kyMYw8ky@dSgO2tC8rZiAtC4bEC@KV9zBYzQVJat4e@5cEcMc5JkEOidurCBBcFuODU6vAgneceKMPhSDIPyupQTppLfVuybB7neq3qiKKM5SYk8xzTTvKpPkn@sDBPZN8Z6lTI6hfkQZjMUh4zkprw/bJ7FXy@6V10OrDSu9vel6YJhzqhXuwyWFRT8QjInmR050NUeBoRrAbRJlQVUSFNwgiVFNfhYOzCQTYQ2N0Sp86zJY7wAlHiH5RThTkTlsdcMKK7woWpfaIeYuyd54UovCYsrHGUJ5wUoOMwQG8JpSYI24Q0yfBYn0RcjEmtXtsmVp@wlUGI2fPLfXFdoYLtd3jBKPAGoBAXkz9z6aapdIIt9K4vbMMeejhRMyycZCbUljUlDEvJ8HDol@4/GcF9oWam5QvbA8Pg5Yp1V9Us7DrLliwkkplwfdvpmIA5OHat@G3HwQSnVPKumVuWcx8sWK12pOhDMRFX3e5NN7i@@XbRu/z6WsCO4oECOqMqJbWBqOn@eG@guo04qupFce9dqm36Foo5PPyPmBd8VReu1NBVX/Ku33esZLdGrCvFyLp6FCVjqIQ@Pfbtq6usN1a9cWpVmo5VbzaalbMTq37WaP4No3g0yTb1WHXiHw "C++ (gcc) – Try It Online") - PCRE1
[Try it online!](https://tio.run/##fVFNT8MwDL3vV1hRpMa03VoGp6zqBZAmIQ7AjUJUSrYGujRKM2mC8duHO7jAgYMtf7zn5ziudYdF6VoH3EMBbMZgCs7rtfLadXWjRTSbnpRKtXUXVNNvnOm0r0SFsrqdDVECEdmKimqtR4AN2oZBKHW1vL5UChPIkUbSYDkx1qhBB8Fc4/X0uW7egienOrMxgSXA0pyhhN8wr5utH0xv/4e9frcy6kxWvRfcFJnkXbGitQZxd3@xvEGJH2BWgncPQ/CdthRhmj8WBassQwIP22fqUDnJkjTHka93rutftGApSwguid/0WxtG7uKUSA80gHz2KCcAR2X7k3O7KI59iuIYSRrE8bSbOjSt4D4hsfHOug4i2kUJt4gIH@OKBnXT9uNeEugpuYQxB27lJ8l8/vkPgfIgykKMVs33T4iiOt3FJVJlUVD1ff9UzWO@F@UZ4pRUYkLMdzFW5zE/HLI0z7LsCw "PHP – Try It Online") - PCRE2 v10.33**
[Attempt This Online!](https://ato.pxeger.com/run?1=XVBfS8MwEMfXfYpQAs3Zdms3BTEtfVFhID6ob8sWak3XQpeWNoPh5ifxZSD6nfTTeNn25MMld5ffn7t8fLZlu_89m8cpJoR2JCHOyCFD0nZqKTvV1lmumDsanqdSllltZN6s2qpWnWACuHgc9a5PXIwCm3KpLEAbpU3PpLyb3t9KCT6JACVRmA-KpmO0SkJO66RAeM-enm-mD8BhS6qC0XrWm65WGjMIonmSOEI7gOB-_YIv2PZDP4jA8tWmrZtXxZzA8RHOkZ83a20sNx4jaYYCeIZzPiDk4KxPNdVxcnjHzPMArQk7rLzKTF4y2vloZvdXmWHuxvWpBgCytSNWoPKysXNxgqtEnNiaUM3f0eb93z8x4F9rUwRXP9csTZgNMdktAJgYb7wUsBMn2H3bLcTEozuWXgAM0cxDxGTjgbj06FFhf7q-wyAKw_BY_QE) - PCRE2 v10.40+
---
# Regex (<sub><sup>.NET</sup></sub>)<sub><sup> / [Retina](https://github.com/m-ender/retina)</sup></sub>, 44 bytes
```
^(?!((?<=(?=(?(3円+$)((?>2円?)3円)))(^x*))x)*$)
```
[Try it online!](https://tio.run/##RY/RaoQwFER/xcoFc7VZ4u7b2tSFPvcL1AWxd2sgTSRmUVb8dhtbSmFezjAMM4OdyI09ab2Bk5WjT5qb89nQxC7JdmXlE2Pli2RlEKtPGWDg1/pYYn1CRHadU8QZU8AtuTzHb/ZrUJo@YizgIUVxs47armegI2UiUGa4e1yglaD5OGjlYx4X6sagPXT2bjzXPjrugUxCW4lmDQUMjKyU8c2PU4Dhmv443znLcNk73OG99V1PI0vmJAWDv/YDl8kpT7y3o1/DrLz454gbG85qZSgCs67rJnguhPgG "PowerShell – Try It Online") - Regex (.NET)
[Try it online!](https://tio.run/##DcsxCkIxEEXR/u0iMIGZhEhi/IWgpnQTIYyghY2FWMzKXIAbi4HbnOK@H5/n61Zmv0cEQ/Q62IKkXiyCyu9rlOFZkwXMwc0xt9OZ24p7jSTLl75v0quI8FivmASSSdlBdDeAqzp4tZlT3TKOh@0P "Retina – Try It Online") - Retina
```
^(?!(x(?=(x*$)(?<=(?(^2円+)(2円(?>3円?))))))*$)
```
[Try it online!](https://tio.run/##RY9Ra4QwEIT/ipUFN2dzRPt2Ns1Bn/sL1AOxezWQJhJzKCf@dqtXSuftm1mGnd6N5IeOjFnBy9LTF0316WRpxHOyXlA94YRK4nQAhupVosJLlacMqxzVW/Wi2ENbuibn5/jdfffa0GfMCrhLUVydp6btEEykbQTa9rfAZmgkGD70RoeYx4W@IjTH1t1s4CZE@X6QSmhKUS9bAYKVpbahfjgFWG7oj7Od05TNe4c/fjSh7WjAZEoOYNmvfWfz6HUg3rkhLNtbWfHPEbduG2u0pQjssiyr4JkQ4gc "PowerShell – Try It Online") - Regex (.NET)
[Try it online!](https://tio.run/##HcoxCgIxEEbh/r9FIIGZhEiy6xaC65ReIoQRtLCxkC3mZB7Ai8XF137v/dier1sd7Z4QDSloJ4ucW7UEX78f8wWBNFvE6CSOjGTdF88k55WEepsSU5tILm0W/rfr8MWB9dCBqzoEtVHyvBScjssP "Retina – Try It Online") - Retina
This is based on [Martin Ender's answer](https://codegolf.stackexchange.com/a/110732/17216), but saving 1 byte by testing zero as a potential divisor of \$n\$ (it never will be, so this is harmless), thus skipping the need to explicitly exclude \$n\$ itself as a divisor. Another upshot of this is that we can extend the valid domain from `^\x+$` to `^\x*$`, giving the correct answer (non-match, i.e. false) for an input of zero.
I have included versions with both smallest-to-largest and largest-to-smallest divisor summing order. The length for both is 44 bytes. Only the largest-to-smallest version is commented below, because it's simpler to explain, and I already commented the original 45 byte smallest-to-largest version [in my ECMAScript answer](https://codegolf.stackexchange.com/a/178952/17216).
```
^(?! # Attempt to add up all the divisors. Since this is a regex and we
# can only work within the available space of the input, that means
# if the sum of the divisors is greater than N, the attempt to add
# all the divisors will fail at some point, causing this negative
# lookahead to succeed, showing that N is an abundant number.
(x # Cycle through all positive values of tail that are less than N,
# testing each one to see if it is a divisor of N. Start at N-1.
(?= # Do the below operations in a lookahead, so that upon popping back
# out of it our position will remaing the same as it is here.
(x*$) # 2円 = tail, a potential divisor; go to end to that the following
# lookbehind can operate on N as a whole.
(?<= # Switch to right-to-left evaluation so that we can operate both
# on N and the potential divisor 2円. This requires variable-length
# lookbehind, a .NET feature. Please read these comments in the
# order indicated, from [Step 1] to [Step 4].
(?(^2円+) # [Step 1] If 2円 is a divisor of N, then...
( # [Step 2] Add it to 3,円 the running total sum of divisors:
# 3円 = 3円 + 2円
2円 # [Step 4] Iff we run out of space here, i.e. iff the sum would
# exceed N at this point, the match will fail, making the
# negative lookahead succeed, showing that we have an
# abundant number.
(?>3円?) # [Step 3] Since 3円 is a nested backref, it will fail to match on
# the first iteration. The "?" accounts for this, making
# it add zero to itself on the first iteration. This must
# be done before adding 2,円 to ensure there is enough room
# for the "?" not to cause the match to become zero-length
# even if 3円 has a value.
)
)
)
)
)* # By using "*" instead of "+" here, we can avoid categorizing zero
# as an abundant number.
$ # We can only reach this point if all proper divisors of N, all the
# way down to 2円 = 1, been successfully summed into 3,円 which would
# mean N is not an abundant number.
)
```
As far as speed goes, the smallest-to-largest version is faster than the largest-to-smallest version (both in .NET and their ports below). Both are faster than the 43 byte version under .NET.
## Regex (Perl/PCRE+`(?^=)`<sup><sup>[RME](https://github.com/Davidebyzero/RegexMathEngine)</sup></sup>), 42 bytes
```
^(?!(x(?=(x*))(?^=(?(?=2円+$)(3円?+2円))))*$)
```
[Try it on replit.com](https://replit.com/@Davidebyzero/regex-Abundant-numbers-with-lookinto-43-bytes) (RegexMathEngine)
This is a port of the largest-to-smallest 44 byte .NET regex, using lookinto instead of variable-length lookbehind. Although the Perl/PCRE syntax for lookahead conditionals is more verbose, this is compensated for by being able to use a possessive quantifier.
## Regex (Perl/PCRE+`(?^=)`<sup><sup>[RME](https://github.com/Davidebyzero/RegexMathEngine)</sup></sup>), 44 bytes
```
^(?!((?^0=(x*))(?^=(?(?=2円+$)(3円?+2円)))x)*$)
```
[Try it on replit.com](https://replit.com/@Davidebyzero/regex-Abundant-numbers-with-lookinto-44-bytes) (RegexMathEngine)
This is a port of the smallest-to-largest 44 byte .NET regex, again using lookinto instead of variable-length lookbehind. This one is less conducive to being ported, and I had to use `(?^0=)`, lookinto accessing the in-progress current match. I've not decided how to finalize the current behavior of this, so it's likely this version will not work with a future version of lookinto.
## Regex (PCRE2), 73 bytes
This is a direct port of the 44 byte .NET regex (based on Martin Ender's regex). It doesn't port well to PCRE, because it changes the value of a capture group inside the lookbehind – but upon the return of any subroutine in PCRE, all capture groups are reset to the values they had upon entering the subroutine. So the only way to port it is to change the main loop [from iteration into recursion](https://chat.stackexchange.com/transcript/message/49026684#49026684), resulting in a large and very slow regex:
```
^(?!(x(?=(x*$)((?<=(?=^(?(?=2円+$)(?=(4円?+2円))).*(?=2円$)(?1)|(?3)).)))|$))
```
[Try it online!](https://tio.run/##fVNhb9owEP3Or7hGUWO3gQJFVbsQIW2jE9PWVinbF@isEAzxGpzIMR1T6W9nL@naaV01JCz73bt353tOkRa7/qBIC3INheQcOdSiwsilMLLI4kQy76h1MBAijTMrknxVqEyaKZvyYBodlZ5PHv4LgGIpK4K2UtuSCXE@@jQUgvvU4ZCEcNBY/DDKSnY9fj@MIt@5ehcNu3Tn8ODvAERKG2vLvIohvg6j69Hlhcdf8pypRm5DaSVKaZlTJEa2ZnFyaw0WkamVso5PTrPzL83IZG1Klev/074/htpVZJEb5qqwHbhZuMBly6qP0QUP@D2pBXOzSWlNJjV2vNm5CcO6P5DL9QwRwH7bb3Z4lS83RZbPJXOajg96gPwkX@PKOPS7SJpAAGv7JmgQ1ZX177Or@2Edx@7wkNM9CIQGiO0x9zasvVvFNkmZa3zUrYyUMYa58XxXc/zIlY@0LC6tkMZAngfPMu4tbbfg7IXhVTT8IC4uBaZ9GaFUdU3FZZLm1d0Cwjg6AVVncnXw8KcTaOzvv6pRk7035LUmeDneCO/F6Dijug0Ab5/8o9oYkps0XpdWzhGLnkx7JfY5zjCmlZzTl/F585SSNIYKxEufirws1Sz7SUonuYH1FnuJ/bxOHaeS8sUCrtNczUnnlmpaWeR6TjYnC8JMLpXWSi9BpZju4kw9l4IOaihtIfZxNCa83lf6v5m4srbzofHw4nvC@Hff2GCPbdggZJsDlzM26Ic4AMU67R4CQmjaGxxOu/CwdVDDFdrhWzY4BgR463K@27Wb3Xa7cXp80jjr9ZpnvZNf "PHP – Try It Online") - PCRE2 v10.33 (PHP)
[Try it on regex101](https://regex101.com/r/HZiNAS/3) - PCRE2 v10.38+
[Attempt This Online!](https://ato.pxeger.com/run?1=fVJNj9MwEJU49le4lrWx26RNCgeEa-UAZVUJtass4rJZrGxw2tDUiRwXKm174HfsZaUVPwp-DeOUD7FIRIoz896bNx47dw_Nurn__uTLNIYAEYMEwmOMRqgxaiWNaqosV9QbjwaxlOussjKvt01ZKZPSlPE0Gbeejzx4CwDlSjmBtkrblkr5ev5mJiXzUcTAEox5r_hsSqvo5dtXsyTx8cXLZDZBnzDjfxNg0tpMW-o5hXw3Sy7ny4XHGO-VupStshQ3uVGjmyzfWAOLrMptabGPcBDhf2RG5TvTlrX-v-zjiQodU9SGklKEnFSigMFat7f5gnF2i8qCkuqqtaZSGiIWRNdC4FRjBuJ2dwMMwH7oBxFz9WrfVPUHRXGAfZBzqM_rHYwHyXQCRVdgAGt4zXsIdZ31z5zoqeh4iIZDhm5BgGADiPYp2YjunraZzdeUGB_6uktTGRzc3vOJZvAgok6yKmutVMaAPesLcZHMzuViKeG8lwn_bUs26HCAGmjlxiyZyte1m40jOI6II5cjovnxz06g5uysq-lI_ALh0aOWctuuKHNtjr3jo38J8K87WwTPv83f07hP9zQWdD8gjNJ4KiABFNZ0MgQIqPRZPEwnMNpo0MEOjdiBxk8BAvhAGDsZ3v_6hMEkDE_JDw) - PCRE2 v10.40+ (PHP)
[Attempt This Online!](https://ato.pxeger.com/run?1=hVVfb9pIEH-7Bz7FxK3CLpgcJm0VYRyUSzgFKQ2V66i9Ame5Zg3WmbVlr5U0JZ_kXu6lj_1AvU9z410bTKh0Fpid38zOn9_MLn9_95Moz4qvu_T971OtEyV-ynqdM23-w36xYEHIGby7tEc993JyNXLvbseO-2F85VzDWeNFyP0oXzAYyE0nq_OGv_JScJPpHCywtd_u7__6fJr_8eXLR2f15n5FvuUi6Jz9GP9JhkfkgQwt8tB6SQkZDiwUEMX3rNdGCFWzV8P2rEcpPWlJuEANuiHDU4QQ3rykVDn89xeHPo-l6dBKLDdpG2Yt0UwswrhItA6lIV_uY2GMKPPWh3bnjZALSHApXJamcUr8mGcCZOGtNcsyb8noV4zT7_toAIMBlGixlDjji8iElIk85WCYTwcuC9mPF0yHz3EcQWwFXpQxCl8bgI8MVfqc9l6_mZsSDgMgsg_ukpWO3NKKKGdENfLu8vrCPmvRUqlDFj6yOCBV8vBrhdTskW8ZpXhIDENViR_nAvqwrZYWNWpyG8gM-qA9Y0Bu1jTcpc24RlXuWy4aioy1F3JS1ZtMkfqIcZLQjjG3uibkaHPacwUyg1KxYYkLaSxTUa1CRZILExQpBQXQSlklrz3hr9yFJzxs23atvKUsyyOhqxLMcv7fjz-NJBIEGUMllo5JLcWqKgFHvgq1TsKIkZK_9-8cmyZ66ebTyJ64zsh-O769cEZXFXw1cS5ubmBTinfO7zocy_jlbxm1S3fdPkqxWSV19fGR79KuVqZ1ULmLsieYG6Tx2k08IVjKSYoTcXt3c1M6COIU5EA-FlxLenG-IrwXiGp7yHXFNDXRxqi6JpPEbbyLgT0Rh0QanfgudodQqgM3dEjiDNVKg3fNgjQ7zTJw8XCjyBptjqx6Z_t9XoDDn_iFtrRvAybSx-A7X-qEFuE4u1fSlBttY27ieK6RXJLp0HxoFolhKRkq51Zt_5aI0OLF1A0sbpjQbof1itUkFNOzT7dktT4PGCvEbsrPriF7zG9ZxFaXPs8t6MJmU4VAVpTLkW1PbPd28vbCubx-nk_l45ECElUUTpoz3lTtMg9MdwcbT2t4qK-lM8Bsjo__N5s9jxoefHNvXKvTJtKcUbwN68Gefs5srS3K4qmhrht1YTwf8yBljOxkWr8TlBJP0vZGVlHwKlJ_Lv9863Z63a4S_gM) - PCRE2 v10.40+ (C++)