- 12.9k
- 2
- 71
- 55
Regex (ECMAScript regex), (削除) 42 (削除ここまで) 31 bytes
The related "is a power of 2" test is ^((x+)(?=2円$))*x$. There is also a shorthand for matching powers of 2 minus 2, discovered by Grimy Grimmy: ^((x+)(?=2円$)x)*$. All three of these regexes are of the same length.
Alternative 31 byte version, by GrimyGrimmy:
Alternative 31 byte version, by Grimy :
^(?!(xx+)1円+$|((xx)+)(2円x)*$)xx
# Match Mersenne primes in the domain ^x*$
^ # N = input number
(?! # "(?!p|q)" is equivalent to "(?!p)(?!q)"; evaluate the
# logical AND of the following negative lookaheads:
(xx+)1円+$ # Assert that N is prime or 0 or 1
|
((xx)+)(2円x)*$ # Assert that N is a power of 2 minus 1; this is based
# on "(?!(x(xx)+)1円*$)" which matches powers of 2.
)
xx # Assert that N >= 2, to prevent the unwanted match of
# 0 and 1 by both of the negative lookahead statements.
Alternative 31 byte version, by Grimy :
^(?!(xx+)1円+$|((xx)+)(2円x)*$)xx
# Match Mersenne primes in the domain ^x*$
^ # N = input number
(?! # "(?!p|q)" is equivalent to "(?!p)(?!q)"; evaluate the
# logical AND of the following negative lookaheads:
(xx+)1円+$ # Assert that N is prime or 0 or 1
|
((xx)+)(2円x)*$ # Assert that N is a power of 2 minus 1; this is based
# on "(?!(x(xx)+)1円*$)" which matches powers of 2.
)
xx # Assert that N >= 2, to prevent the unwanted match of
# 0 and 1 by both of the negative lookahead statements.
ECMAScript regex, (削除) 42 (削除ここまで) 31 bytes
^(?!(xx+)1円+$)(x(x*)(?=3円$))+x$
^
(?!(xx+)1円+$) # Assert that N is prime or 0 or 1.
(x(x*)(?=3円$))+x$ # Assert that N is a power of 2 minus 1 and is >= 3.
# The >=3 part of this prevents the match of 0 and 1.
Edit: Down to 31 bytes thanks to Neil.
The basic "is a power of 2 minus 1" test is ^(x(x*)(?=2円$))*$. This works by looping the operation "subtract 1, then divide evenly by 2" until it can be done no further, then asserting that the result is zero. This can be modified to match only numbers ≥1 by changing the last * to a +, forcing the loop to iterate at least once. Inserting an x before the last $ further modifies it to match only numbers ≥3 by asserting that the final result after looping at least once is 1.
The related "is a power of 2" test is ^((x+)(?=2円$))*x$. There is also a shorthand for matching powers of 2 minus 2, discovered by Grimy : ^((x+)(?=2円$)x)*$. All three of these regexes are of the same length.
ECMAScript regex, (削除) 42 (削除ここまで) 31 bytes
^(?!(xx+)1円+$)(x(x*)(?=3円$))+x$
^
(?!(xx+)1円+$) # Assert that N is prime or 0 or 1.
(x(x*)(?=3円$))+x$ # Assert that N is a power of 2 minus 1 and is >= 3.
# The >=3 part of this prevents the match of 0 and 1.
Edit: Down to 31 bytes thanks to Neil.
ECMAScript regex, (削除) 42 (削除ここまで) 31 bytes
^(?!(xx+)1円+$)(x(x*)(?=3円$))+x$
^
(?!(xx+)1円+$) # Assert that N is prime or 0 or 1.
(x(x*)(?=3円$))+x$ # Assert that N is a power of 2 minus 1 and is >= 3.
# The >=3 part of this prevents the match of 0 and 1.
Edit: Down to 31 bytes thanks to Neil.
The basic "is a power of 2 minus 1" test is ^(x(x*)(?=2円$))*$. This works by looping the operation "subtract 1, then divide evenly by 2" until it can be done no further, then asserting that the result is zero. This can be modified to match only numbers ≥1 by changing the last * to a +, forcing the loop to iterate at least once. Inserting an x before the last $ further modifies it to match only numbers ≥3 by asserting that the final result after looping at least once is 1.
The related "is a power of 2" test is ^((x+)(?=2円$))*x$. There is also a shorthand for matching powers of 2 minus 2, discovered by Grimy : ^((x+)(?=2円$)x)*$. All three of these regexes are of the same length.
- 12.9k
- 2
- 71
- 55