Regex (ECMAScript or better), 17 bytes
^((x+)(?=\2$))+x$
- Completely even
Takes its input in unary, as a string of x
characters whose length represents the number.
Try it online! - ECMAScript
Try it online! - ECMAScript 2018
Try it online! - Java
Try it online! - Perl
Try it online! - PCRE
Try it online! - Boost
Try it online! - Python
Try it online! - Ruby
Try it online! - .NET
Remarkably, there are three completely different 17 byte solutions for Powers of 2, which blew my mind even when I thought there were two. For this challenge, only one is 17 bytes, but for plain Powers of 2 they all are.
^(?!(x(xx)+)\1*$)
- 17 bytes - Powers of 2
^(?!(x(xx)+)\1*$)x
- 18 bytes - Powers of 2, correct at zero
^(?!(x(xx)+)\1*$)xx
- 19 bytes - Completely even
This is what I came up to solve a level of Regex Golf on 2014-02-21, and was also independently discovered by several others, including earlier than 2013-12-20. It's the one most similar to the well-known primality test, which is probably why most people came up with this one instead of the others.
It asserts that \$n\$ has no odd divisors of \$3\$ or greater yielding a positive quotient. It has a false positive for zero, since although zero can be divided by any odd number, the quotient is zero, not positive. This can be fixed at the cost of an extra byte: ^(?!(x(xx)+|)\1*$)
or ^(?!(x(xx)+)\1*$)x
.
Since the entire test is inside a (negative) lookahead, it can be adapted to answer this challenge by adding xx
at the end, which enforces that only \$n≥2\$ can match.
^(?!(x*)(\1\1)+$)
- 17 bytes - Powers of 2
^(?!(x*)(\1\1)+$)xx
- 19 bytes - Completely even
This was discovered by Grimmy in 2019-02-05, without fanfare. I on the other hand was amazed, as this has no such flaw as the other negative assertion – it does not match zero.
It asserts that \$n\$ has no divisors yielding an odd quotient of \$3\$ or greater. As a negative assertion, it does not consume the power of 2 that it matches, and thus is great for use in larger regexes where it doesn't need to be wrapped in a lookahead to allow other tests to be done on the same value of \$tail\$.
The downside is that in standard regex engines, it's significantly slower than ^(?!(x(xx)+|)\1*$)
. In my regex engine though, they're both statically optimized into a bitwise power of 2 test unless optimizations are disabled.
^((x+)(?=\2$))*x$
- 17 bytes - Powers of 2
^((x+)(?=\2$))+x$
- 17 bytes - Completely even
I discovered this one in 2014-02-21, after being mentally primed by solving teukon's Dominoes 2 puzzle (which is now included in Regex Golf); teukon independently came up with it later that same day. It was the very first problem in unary that we solved by repeatedly decreasing \$tail\$ in a loop while retaining an invariant property at every step, and is probably the simplest function that is best golfed by being solved that way.
It repeatedly divides \$tail\$ by \$2\$ (asserting each time that there is no remainder) as many times as possible, and then asserts that the end result is \$1\$. This one is useful in larger regexes when it is desirable to consume the identified power of 2.
It is the most suitable for solving this challenge, “Is it a completely even number?”, as the only change necessary is upping the minimum iteration count of the loop from 0
to 1
by changing the *
quantifier to a +
, at a cost of 0 bytes. This enforces that \$n\$ must be evenly divided by \$2\$ at least once before yielding an end result of \$1\$.