Regex (Perl / Java / PCRE2 v10.34 or later / .NET), 29 bytes
((?=((?>\2?)(x*))x(x\3)+$)x)*
Try it online! - Perl
Try it online! - Java
Attempt This Online! - PCRE2 v10.40+
Try it online! - .NET
Regex (Perl / Java / PCRE2 v10.34 or later / .NET), 29 bytes
Uses an atomic group (?>\2?)
insteadObsoleted by the 29 byte regex below, which supports a superset of possessive quantifier \2?+
to support 4 different regex engines.
Regex (Perl / Java / PCRE / Pythonregex
/ Ruby / .NET), 3333 29 bytes
((?=.*(?=(\3?)=\3$|^)(\2(x*)x+)x(x\4\2+$)+$)x)*
Try it online!Try it online! - Perl
Try it online!Try it online! - Java
Try it online!Try it online! - PCRE1
Try it online!Try it online! - PCRE2 v10.33
Attempt This Online!Attempt This Online! - PCRE2 v10.40+
Try it online!Try it online! - Python import regex
Try it online!Try it online! - Ruby
Try it online!Try it online! - .NET
Copies a value back and forth between two groups, to avoid using aFor regex engines that lack nested backreference, to add support for PCRE1 (which would otherwise force the group to be atomic) and Python/Ruby (which don'tbackreferences but support nestedforward-declared backreferences). This automatically adds , we switch to a different approach.NET support too In this version, since the possessive quantifier is no longer needed with an atomiceach iteration, while inside a lookahead being used to copy, stores the new value of tail in the capture group \3
into, so that the next iteration can then recall that value rather directly. Since this directly sets \2\3
instead of building it up incrementally, there's no need to emulate a nested backreference.
# tail = N = input number;
# no need to anchor, as all inputs return a result
(
(?=
(?=.*(\3?)=\3$|^) # \2tail = a copy of \3, or 0N if \3 is unset
(\2(x*x+)(\2+$) # \4\2 = {conjectured largest proper divisor of tail-\2} - 1;
#tail; \3 = \2 + \4; tail -= \3; note that the subtraction of 1 from this\2
# divisor compensates for the "tail -= 1" on each iteration (below)
x(x\4)+$ # assert tail - 1 is divisible by \4 + 1
)
x # head +=Increment 1;the tailreturn -=1value
)* # Loop the above as many times as possible, minimum zero