Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Update TIOs to have no-newline end; apply Grimmy's name change; use my standard regex language name format
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

Regex (ECMAScript regex), (削除) 42 (削除ここまで) 31 bytes

Try it online! Try it online!

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:

Try it online! Try it online!

ECMAScript regex, (削除) 42 (削除ここまで) 31 bytes

Try it online!

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.

Alternative 31 byte version, by Grimy:

Try it online!

Regex (ECMAScript), (削除) 42 (削除ここまで) 31 bytes

Try it online!

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 Grimmy: ^((x+)(?=2円$)x)*$. All three of these regexes are of the same length.

Alternative 31 byte version, by Grimmy:

Try it online!

Add an alternative 31 byte version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

Alternative 31 byte version, by Grimy :

^(?!(xx+)1円+$|((xx)+)(2円x)*$)xx

Try it online!

# 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

Try it online!

# 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.
Explain the power of 2 minus 1 test
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

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.

Try it online!

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.

Try it online!

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.

Try it online!

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.

Prevent the incorrect match of 1
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Replace Retina TIO code with much faster JavaScript
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Remove the @
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Optimize the "Try it online" Retina code
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
deleted 5 characters in body
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
oops, had only updated the pretty printed version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Use Neil's great suggestion; deleted 34 characters in body
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading

AltStyle によって変換されたページ (->オリジナル) /