[Question]
  [
> 
> A number is a *Mersenne Prime* if it is both prime and can be written in the form **2n-1**, where **n** is a positive integer. 
> 
> 
> 
Your task is to, given any positive integer, determine whether or not it is a Mersenne prime. You may submit either a function which returns a truthy/falsy value, or a full program which performs IO.
### Rules:
* As this is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), you should aim to do this in the **shortest byte count possible.** Builtins are allowed.
* Standard golfing loopholes apply - you cannot read the Mersenne primes from external files, or hardcode them into your program.
* Your program should work for values within your language's standard integer size.
---
### Test Cases
For reference, a list of (known) Mersenne Primes can be found [**here**](http://www.mersenne.org/primes/). Some handy test cases are:
```
2  -> False
1  -> False 
20 -> False
51 -> False
63 -> False
3    -> True
31   -> True
8191 -> True
```
---
*Merry Christmas, everybody! Have a great holiday, whatever you celebrate :)*
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 5 bytes
A positive number in the form **2n - 1** in binary only consists of **1's**.
Code:
```
b`¹pP
```
Explanation:
```
b`      # Push each digit of the binary representation of the number onto the stack
  ¹p    # Check if the input is prime
    P   # Take the product of all these digits
```
Uses the **CP-1252** encoding. [Try it online!](https://tio.run/nexus/05ab1e#@5@UcGhnQcD//xaGloYA "05AB1E – TIO Nexus") or [Verify all test cases](https://tio.run/nexus/05ab1e#qyn7X5mUUFkQ8F/nvxGXoSmXkQGXqSGXmTEXEBpyWRhaGgIA).
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
&‘<ÆP
```
[Try it online!](https://tio.run/nexus/jelly#@6/2qGGGzeG2gP@H2x81rQEi9///o410FAxNdRSMDHQUTA11FMyMY3UUoo11FIyBHAtDS8NYAA "Jelly – TIO Nexus")
### How it works
```
&‘<ÆP  Main link. Argument: x
 ‘     Yield x+1.
&      Take the bitwise AND of x and x+1.
       This yields 0 iff x is a Mersenne number, i.e., iff x+1 is a power of 2.
   ÆP  Yield 1 if x is a prime, 0 if not.
  <    Compare the results to both sides,
       This yields 1 iff x is both a Mersenne number and a prime.
```
[Answer]
# [Python](https://docs.python.org/), 45 bytes
```
lambda n:-~n&n<all(n%i for i in range(2,n))<n
```
[Try it online!](https://tio.run/nexus/python3#FchBCsIwEEbhtT3Fv7HMyAhNg6Kl3qSbEY0E0mkJLrry6jFdPd4X8MBUks7Pl8KG889aGzUlsmNEWDIioiGrfd7UizGPVnbedq4CJ@g7waX26llAXuDr3Nzd8dAc1hztS6dZVwqCjbn8AQ "Python 3 – TIO Nexus")
### How it works
The three terms of the chained comparison
```
-~n&n<all(n%i for i in range(2,n))<n
```
do the following:
* `-~n&n` computes the bitwise AND of **n + 1** and **n**. Since **n** consists solely of **1** bits if it is a Mersenne number, the bitwise AND will return **0** if (and only if) this is the case.
* `all(n%i for i in range(2,n))` returns *True* if and only if **n mod i** is non-zero for all values of **i** in **[2, …, n - 1]**, i.e., if and only if **n** has no positive divisors apart from **1** and **n**.
In other words, *all* returns *True* if and only if **n** is a composite number, i.e., **n** is either **1** or a prime.
* `n` is self-explanatory.
The chained comparison returns *True* if and only if the individual comparisons do the same.
* Since *all* returns either *True*/**1** or *False*/**0**, `-~n&n<all(n%i for i in range(2,n))` can only return *True* if `-~n&n` yields **0** (i.e., if **n** is a Mersenne number) and *all* returns *True* (i.e., if **n** either **1** or a prime).
* The comparison `all(n%i for i in range(2,n))<n` holds whenever **n > 1**, but since *all* returns *True* if **n = 1**, it does not hold in this case.
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 7 bytes
```
#p+~^h2
```
[Try it online!](https://tio.run/nexus/brachylog#@69coF0Xl2H0/7@hkTkA "Brachylog – TIO Nexus")
A Brachylog program is basically a sequence of constraints which form a chain: the first constraint is between the input and an anonymous unknown (let's call it *A* for the purpose of this discussion), the second constraint is between that anonymous unknown and a second anonymous unknown (which we'll call *B*), and so on. As such, the program breaks down like this:
```
#p      Input = A, and is prime
+       B = A + 1
~^      B = X to the power Y, C = the list [X, Y]
h       D = the head of list C (= X)
2       D = 2
```
The only way all these constraints can be satisfied simultaneously is if B is a power of 2, i.e. the input is a power of 2 minus 1, and the input is also prime. (Brachylog uses a constraint solver internally, so the program won't be as inefficient as the evaluation order looks; it'll be aware that `C` is of the form `[2, Y]` before it tries to express `B` as the exponentiation of two numbers.)
Interestingly, `#p+~^` *almost* works, because Mersenne-like primes can only use 2 as the base in non-degenerate cases ([proof](https://en.wikipedia.org/wiki/Mersenne_prime#Theorems_about_Mersenne_numbers)), but a) it fails for non-Mersenne primes *B*-1 as they can be expressed as *B*¹, and b) the existing Brachylog interpreter seems to be confused (going into an infinite, or at least long-duration, loop) by a program that's that poorly constrained. So 7 bytes seems unlikely to be beaten in Brachylog.
[Answer]
## Mathematica 26 Bytes
```
PerfectNumberQ[# (#+1)/2]&
```
See this [proof](https://primes.utm.edu/notes/proofs/EvenPerfect.html)
Works so long as there are no odd perfect numbers, and none are known to exist.
[Answer]
## Mathematica, 29 26 bytes
*Edit: Saved 3 bytes thanks to Martin Ender*
`PrimeQ@#&&IntegerQ@Log2[#+1]&`
```
PrimeQ@#&&1>BitAnd[#,#+1]&
```
I suspect this would be faster since the first 42 exponents are hard-coded:
```
MersennePrimeExponentQ@Log2[#+1]&
```
[Answer]
# [Perl 6](https://perl6.org), 29 bytes
```
{.base(2)~~/^1*$/&&.is-prime}
```
[Try it](https://tio.run/nexus/perl6#TU/LTgJBELzPV/SB8FCB7unpeYhi4sEv4CZqlB3NJrBLWDAShF/HWSKPS6e6q1JdtaoifNveZKBWCY1itRwoNVtDc1JmEe73m97HexXburPb9V/pqtFvNnt51Z0v8lnc7j/LBUzzIlbtDmwUwKyfBsDrYTbu8mK@Wg7hHsbZ9eE0rq6g1R22avCviT/zOFnGrJY9jxar@Pv0Pq3iS6L7kOyL@FNHSt5raBzFSftYltPb283uwmF70KV0WYzz6Roe6g7t690xSOfm7JDg20BtVVYWsbtMtfPia7DXAN0hHAIokhMGpfFMCJ2x5TNWXDdKa91CMV0sngKdFmW9NS44a5EYLQZHJiQFogsBPXFgTU7bYFgYkQwzisHAwRjLRgJ5EcPkmZOJWBRNWosEa9BaMmISHZzTwTIlNzIevXhkR5qCT0/SR2sNe9LiDGqvAxEJiiM5xfwD "Perl 6 – TIO Nexus")
## Expanded:
```
{             # bare block lambda with implicit parameter ｢$_｣
  .base(2)    # is its binary representation ( implicit method call on ｢$_｣ )
   ~~
  /^ 1* $/    # made entirely of ｢1｣s
  &&          # and
  .is-prime   # is it prime
}
```
since Perl 6 has arbitrarily large Ints, it doesn't pad the front of `.base(2)` with `0`s.
[Answer]
## Python, ~~83~~ ~~82~~ ~~79~~ ~~76~~ 73 bytes
```
def f(m):
 s,n=(m!=3)*4,m>>2
 while-~m&m<n:s,n=(s*s-2)%m,n>>1
 return s<1
```
## Python 2, 71 bytes
```
def f(m):
 s,n=(m!=3)*4,m/4
 while-~m&m<n:s,n=(s*s-2)%m,n/2
 return s<1
```
This function implements the [Lucas–Lehmer primality test](https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test), so while it isn't as short as some of the other Python offerings it's *much* faster at handling huge inputs.
---
Here's some test code that runs on Python 2 or Python 3.
```
from __future__ import print_function
def primes(n):
    """ Return a list of primes < n """
    # From http://stackoverflow.com/a/3035188/4014959
    sieve = [True] * (n//2)
    for i in range(3, int(n**0.5) + 1, 2):
        if sieve[i//2]:
            sieve[i*i//2::i] = [False] * ((n - i*i - 1) // (2*i) + 1)
    return [2] + [2*i + 1 for i in range(1, n//2) if sieve[i]]
def lucas_lehmer_old(p):
    m = (1 << p) - 1
    s = 4
    for i in range(p - 2):
        s = (s * s - 2) % m
    return s == 0 and m or 0
# much faster
def lucas_lehmer(p):
    m = (1 << p) - 1
    s = 4
    for i in range(p - 2):
        s = s * s - 2
        while s > m:
            s = (s & m) + (s >> p)
    return s == 0 or s == m and m or 0
def f(m):
 s,n=(m!=3)*4,m>>2
 while-~m&m<n:s,n=(s*s-2)%m,n>>1
 return s<1
# Make a list of some Mersenne primes
a = [3]
for p in primes(608):
    m = lucas_lehmer(p)
    if m:
        print(p, m)
        a.append(m)
print()
# Test that `f` works on all the numbers in `a`
print(all(map(f, a))) 
# Test `f` on numbers that may not be Mersenne primes
for i in range(1, 525000):
    u = f(i)
    v = i in a
    if u or v:
        print(i, u, v)
    if u != v:
        print('Error:', i, u, v)
```
**output**
```
3 7
5 31
7 127
13 8191
17 131071
19 524287
31 2147483647
61 2305843009213693951
89 618970019642690137449562111
107 162259276829213363391578010288127
127 170141183460469231731687303715884105727
521 6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151
607 531137992816767098689588206552468627329593117727031923199444138200403559860852242739162502265229285668889329486246501015346579337652707239409519978766587351943831270835393219031728127
True
3 True True
7 True True
31 True True
127 True True
8191 True True
131071 True True
524287 True True
```
FWIW, here's a slightly more efficient version of `f` that doesn't re-test `m` on every loop:
```
def f(m):
 s,n=m!=3and 4,m>>2
 if-~m&m<1:
  while n:
   s=(s*s-2)%m
   n>>=1
 return s<1
```
[Answer]
# R, ~~41~~ 40 bytes
```
matlab::isprime(x<-scan())&!log2(x+1)%%1
```
Oddly enough the builtin in R `mersenne` takes `n` as argument, not `2^n-1`. 
This takes `x` from STDIN, checks if it is prime using the `matlab` package and checks if the 2-log of `x+1` is a whole number by taking mod 1 and checking for 'not zero-ness'.
Also, if you use the `mersenne` builtin, it ends up being slightly shorter, but feels like cheating:
```
numbers::mersenne(log2(scan()+1))
```
Saved 1 byte thanks to @Billywob
[Answer]
## Pyke, 10 bytes
```
_PQb2+}\1q
```
[Try it here!](http://pyke.catbus.co.uk/?code=_PQb2%2B%7D%5C1q&input=7)
```
_P         -    is_prime(input)
     +     -   ^ + V
  Qb2      -    base_2(input)
      }    -  uniquify(^)
       \1q - ^ == "1"
```
[Answer]
# [Actually](https://github.com/Mego/Seriously), 9 bytes
```
;├╔'1=@p*
```
[Try it online!](https://tio.run/nexus/actually#@2/9aMqcR1OnqBvaOhRo/f9vDAA "Actually – TIO Nexus")
Explanation:
Since every number of the form 2n-1 has all 1's in its binary representation, a Mersenne prime can be identified as a prime number with that quality.
```
;├╔'1=@p*
 ├╔'1=     only unique binary digit is 1
        *  and
;     @p   is prime
```
[Answer]
## Jelly, 5 bytes
Alternate approach to @Dennis' existing 5-byte Jelly answer:
```
B;ÆPP
```
[Try it online!](https://tio.run/nexus/jelly#@@9kfbgtIOD///8WhpaGAA)
How it works:
```
B      Returns the binary representation of the input as a list [1, 0, 1, 1, ...]
 ;     And attach to this list 
  ÆP   a 1 if the input is a prime, 0 otherwise
    P  Calculates the product of this list of 1's and 0's
```
Since a Mersenne Prime is one less than a power of 2, its binary representation
is excusively 1's. The output therefor is 1 for Mersenne primes, and 0 in all other cases .
[Answer]
# Ceylon, 66 bytes
```
Boolean m(Integer c)=>c>2&&c.and(c+1)<1&&!(2:c-2).any((d)=>c%d<1);
```
Formatted (and commented):
```
// Check whether a (positive integer) number is a mersenne prime number.
//
// Question:  http://codegolf.stackexchange.com/q/104508/2338
// My Answer: http://codegolf.stackexchange.com/a/104805/2338
Boolean m(Integer c) =>
        // check whether c+1 is a power of two
        c.and(c+1)<1 &&
        // the standard primality check by trial division
         !(2 : c-2).any((d) => c%d < 1) &&
        // we need to exclude 1, which is unfortunately
        // matched by both criteria above, but is no prime.
        c>1;
```
---
With cheating (hardcoding the results in the range of Ceylon's Integer), we can get a byte shorter (65):
```
Boolean h(Integer c) =>
        c.and(c+1)<1 && #20000000800a20ac.and(c+1)>0;
```
(It looks like the syntax highlighter misunderstands Ceylon's hex numerals as start-of-comment.)
If an anonymous function is okay, this one is 49 bytes:
```
[2,3,5,7,13,17,19,31,61].map((p)=>2^p-1).contains
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 23 bytes
```
PrimeQ[BitAnd[#,#+2]#]&
```
[Try it online!](https://tio.run/##TY29CsIwFEb3@xSBQBY/0JtYf4ZI9Ql0Dhku2tYM6VCyFZ89ElzcDucMJ0t5D1lKekoVUV6tFow97A4d4@DQgRns4HCEY5z4zB@i0dN9SXl49NoYvtxSuc6voKE3HA3R5Osvh79io46m@bmEcdu3m1dTg1i/ "Wolfram Language (Mathematica) – Try It Online")
1 is handled correctly because `PrimeQ[BitAnd[1,1+2]*1] == PrimeQ@1 == False`. Otherwise, for `BitAnd[#,#+2]#` to be prime, we need that `#` is prime and `BitAnd[#,#+2] == 1`, which happens when `#` is a Mersenne number.
[Answer]
# Regex (ECMAScript), 29 bytes
```
^(?!(xx+|(x(x))+)(\1\3)+$)xxx
```
[Try it online!](https://tio.run/##Tc3PT8IwGMbxO38FLCa8L3OlA38QZuHkgQsHPYomzXjpqqVb2goV598@0cTE05N8P4fnVR6kL51uQuYbvSW3r@0bfXROWDr2H0jdxwbgJBbjUfcCywHEmLYQISKmCJt8M8X0AmOM3Wh8Qhbqx@C0VYDMG10S3FxmV4jFsdKGAIxwJLdGWwLEgbDvxuCnEob5xugAw2yIhd4BWKGYIatChYtJ22q/lmvQopHO08oGUE/8GfEP6D/YRb7M5z@MoXL1MVnZgzR623fSKpr3k9QUu9pBoe8EFTpN8XyYxIQ5akgG0Mj2MpQVOMSytr42xEytzr346nh2zTnvzfIZz2aTnPfyac5vefY7k28 "JavaScript (SpiderMonkey) – Try It Online")
Inspired by Grimy in [chat](https://chat.stackexchange.com/transcript/message/49455538#49455538)
The regex asserts that the input is greater than 3, and that it is neither of the form: `(xx+)\1+` or `((xx)+)(\1x)+`.
The first matches composite numbers.  
The second matches a number that is 1 less than a multiple of some odd number greater than 2.
The first will **not** match prime numbers, or `0` or `1`.  
The second will **not** match numbers of the form \$2^n-1\$, or numbers that are 1 less than an odd prime.
Since 2 is the only prime that is 1 less than an odd prime, the negative lookahead, together with the assertion that the input is greater than 3, will match only mersenne primes.
[Answer]
# Regex (ECMAScript), ~~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!](https://tio.run/##TY/NTgIxFEb3PAVMCNzLOEML/hBqYeWCDQtdiiYNXGaqpTRtgRHh2UcwMXH1Lc6XnJwPtVdh6bWLWXB6RX6ztZ/0VXtp6dB8puKpcgBHOen36neYtqCqUlzwtI1QQdVDmMrFsI2YVu261z9iHrcv0WtbAObB6CXB/U12iyiCZOJQakMARnpSK6MtAWJL2p0x@F1IkwdndIRu1kWh1wBWFrkhW8QSJ4PTSYe5moOWTvlAMxuheGVviH@A/gM74VM@vmKMpd8ekpndK6NXTa9sQeNmkhqx3noQ@lGS0GmKF2FSJbknRyqCxnyj4rIEj/gdOh13SYpwreDC7WKI/nIR53PNsjvGWGPERywbDThr8CFnDyz7ncEP "JavaScript (SpiderMonkey) – 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 [Grimmy](https://codegolf.stackexchange.com/users/6484/grimmy): `^((x+)(?=\2$)x)*$`. All three of these regexes are of the same length.
Alternative 31 byte version, [by Grimmy](https://chat.stackexchange.com/transcript/message/49454897#49454897):
`^(?!(xx+)\1+$|((xx)+)(\2x)*$)xx`
[Try it online!](https://tio.run/##TY/LbsIwFET3fAVECO4lTbDTF8I1rLpgw6JdllaywCRujWPZBlII356GSpW6mpHOSKPzKQ7Cr52yIfFWbaTbleZLfjeOG3nsvsj8ubIAJz4bj5oPmPegqmJc0bhfQ1sxRlhlFY76WFXNaHzCNJSvwSmTA6Zeq7WEh5vkDpF5TtixUFoCaO6k2GhlJCD2uNlrjeec69RbrQIMkyEytQUwPE@1NHkocJbVtfJLsQTFrXBeLkyA/I28I/4B@R@YGZ3T6RVjKFx5jBbmILTadJ0wuZx2o1izbemAqScumYpjbA@jKkqdtFIEUJjuRFgX4BDPfjCwrVKAqwVldh98cO2EXS4NSe4JIZ0JnZBkklHSobeUPJLkN7If "JavaScript (SpiderMonkey) – 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)+)(\2x)*$  # 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.
```
[Answer]
## Ruby, 47 bytes
```
->b{!("%b"%(b/2)=~/0/||(2...b).find{|a|b%a<1})}
```
[Answer]
# Julia, 26 bytes
```
n->isprime(n)&&ispow2(n+1)
```
[Answer]
# Python, 65 bytes
```
f=lambda n,i=3:(n^i)-all(n%i for i in range(2,n))<0 or f(n,-~i|i)
```
Outputs via Exit Code. Recursion Error for False. No error for True.
**How it works**
Since `2^n-1` in binary is made entirely from 1's, the next `2^n-1` number can be generated by `number|number+1`. 
This function uses this by recursively going through each `2^n-1`number checking to see if it's a prime number and eqaul to the input. If the number is not a mersenne prime, python will eventually throw an error as the maximum recursion depth would have been exceeded.
[Answer]
# [Pushy](https://github.com/FTcode/Pushy), 7 bytes
```
oBoIpP#
```
**[Try it online!](https://tio.run/nexus/pushy#@5/vlO9ZEKD8//9/C0NLQwA "Pushy – TIO Nexus")**
This takes advantage of the fact that mersenne numbers have only ones in their binary representation:
```
oB      \ Pop input, push its binary digits.
  oI    \ Re-push the input
    p   \ Test its primality (0/1)
     P# \ Print the product of the stack
```
The stack product will only be `1` if the number has no zeroes in its binary representation, and its primality is `True`.
[Answer]
# [Pyth](https://pyth.readthedocs.io), 8 bytes
```
&.AjQ2P_
```
**[Verify all the test cases.](https://pyth.herokuapp.com/?code=%26.AjQ2P_&input=31&test_suite=1&test_suite_input=2%0A1%0A20%0A51%0A63%0A3%0A31%0A8191&debug=0)**
# [Pyth](https://pyth.readthedocs.io), 8 bytes
```
<.&QhQP_
```
**[Verify all the test cases.](https://pyth.herokuapp.com/?code=%3C.%26QhQP_&input=31&test_suite=1&test_suite_input=2%0A1%0A20%0A51%0A63%0A3%0A31%0A8191&debug=0)**
---
# How?
### Code Breakdown #1
```
&.AjQ2P_    Full program with implicit input.
      P_    Is Prime?
   jQ2      Convert the input to binary as a list of digits.
 .A         All the elements are truthy (i.e. all are 1).
&           Logical AND.
            Output implicitly.
```
### How does that work?
A number of the form ***2n - 1*** always contains ***1*** only when written in binary. Hence, we test if all its binary digits are ***1*** and if it is prime.
### Code Breakdown #2
```
<.&QhQP_    Full program with implicit input.
      P_    Is Prime?
    hQ      Input + 1.
 .&Q        Bitwise AND between the input and ^.
<           Is smaller than? I.e. The bitwise AND results in 0 and the primality test results in 1.
            Output implicitly.
```
### How does that work?
This tests if the ***input + 1*** is a power of two (i.e. if it is a Mersenne number), and then performs the primality test. In Python, `bool` is a subclass of `int`, so truthy is treated as ***1*** and falsy is treated as ***0***. To avoid checking explicitly that one is ***0*** and the other is ***1***, we compare their values using `<` (since we only have 1 such case).
[Answer]
# Java 8, ~~53~~ ~~52~~ 49 bytes
```
n->{int i=1;for(;n%++i>0;);return(n&n+1|i^n)==0;}
```
Bug-fixed and golfed by 4 bytes thanks to *@Nevay*.
**Explanation:**
[Try it here.](https://tio.run/##nZBfS8MwFMXf9ykuBSWhtjQrOjV032BlsEdRSLtsZLY3JUkHMvvZa/bnURcVkod7z@HwO3cn9iLRncTd@n2sG2EtLITCwwRAoZNmI2oJ5XEEqLRupECoiZcAKffbwX//rBNO1VACQgEjJvPD0aIKxjfaEI43cazmGafcSNcbJHiLMftUb0iLIuPDyM8xXV81PuaSttdqDa2nIStnFG5fXkHQM8rqwzrZprp3aecl1yCJSu3AD620zxCd4L71YVoTRq/r0yxguA8lPORXDD8r0fJU4A6q3l/YF1pIYyWiTJa/azYNcLEQOMtDhlnoeP@s/teqIdAQZx46xSN7uliGyTB@AQ)
```
n->{                // Method with integer parameter and boolean return-type
  int i=1;          //  Temp integer `i`, starting at 1
  for(;n%++i>0;);   //  Loop and increase `i` as long as `n` is divisible by `i`
  return(n&n+1|i^n) //  Then return if `n` bitwise-AND `n+1` bitwise-OR `i` bitwise-XOR `n`
          ==0;      //  is exactly 0
}                   // End of method
```
[Answer]
# Python 3, 68 bytes
```
a=int(input());print(a&-~a<1and a>1and all(a%b for b in range(2,a)))
```
[Try it here](http://ideone.com/ZPaMOQ)
# Python 2, 63 bytes
```
a=input();print(a&-~a<1)and a>1and all(a%b for b in range(2,a))
```
[Try it here](http://ideone.com/jvHjxz)
---
Thanks for suggestion [Jonathan](https://codegolf.stackexchange.com/users/73111/jonathan-frech)
---
Open to any suggestions for reducing the bytecount.
[Answer]
# Japt, 6 bytes
```
¢¬×&Uj
```
[Run it](https://ethproductions.github.io/japt/?v=1.4.6&code=oqzXJlVq&input=NQoKLaE=) or [Run all test cases](https://ethproductions.github.io/japt/?v=1.4.6&code=oqzXJlVq&input=Wwo1LDIsMSwyMCw1MSw2MywgIC8vIGZhbHN5CjMsMzEsODE5MSAgICAgICAgLy8gdHJ1dGh5Cl0KCi1tUg==)
[Answer]
# [Prolog (SWI)](http://www.swi-prolog.org), 48 bytes
```
Y+X:-X=<Y;X mod Y>0,Y+1+X.
\X:-X>1,2+X,X+1/\X<1.
```
[Try it online!](https://tio.run/##KyjKz8lP1y0uz/z/P1I7wko3wtYm0jpCITc/RSHSzkAnUttQO0KPKwYkY2eoY6QdoROhbagfE2FjqPf/f4yRHhdXjCGIMDIAkaZgtpkxiIQQYAELQ0sQnZGYU6IHAA "Prolog (SWI) – Try It Online")
-7 thanks to Jo King
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 6 bytes
```
æ[›¨²c
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLDplvigLrCqMKyYyIsIiIsIiJd)
If N is prime, check if `n+1` is a power of 2.
# [Vyxal](https://github.com/Vyxal/Vyxal), 5 bytes
```
æ⁰bJΠ
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJQIiwiIiwiw6bigbBiSs6gIiwiIiwiMzEiXQ==)
Port of the Jelly 5 byte answer.
[Answer]
# Python, 93 Bytes
```
def f(a):
 for b in range(a):
  if(a+1==2**b and not[i for i in range(2,a)if a%i<1]):return 1
```
This code would work in both Python 2 and Python 3 so I have not specified a version.
[Answer]
## Racket 76 bytes
```
(define(g m)(for/or((i m))(= m(-(expt 2 i)1))))(if(and(prime? n)(g n))#t #f)
```
Ungolfed: 
```
(require math)
(define(f n)
  (define (ispowerminus1 m)
    (for/or ((i m))
      (= m (-(expt 2 i)1))))
  (if (and (prime? n)
           (ispowerminus1 n))
      #t #f))
```
Testing: 
```
(f 1)
(f 2)
(f 20)
(f 51)
(f 63)
(f 3)
(f 31)
(f 8191)
```
Output: 
```
#f
#f
#f
#f
#f
#t
#t
#t
```
[Answer]
# PHP, 53 bytes
```
for($i=$n=$argv[1];--$i&&$n%$i;);echo!($i-1|$n+1&$n);
```
takes command line argument; prints `1` for Mersenne prime, empty string else.
Run with `-r`.
**breakdown**
```
for($i=$n=$argv[1];--$i&&$n%$i;);   // loop $i down from $n-1 until $i divides $n
                        // If $n is prime, loop ends with $i=1. ($n=1 -> $i=0)
echo!($i-1|$n+1&$n);    // If $i!=1, $n is not prime. If ($n+1&$n)>0, $n is not Mersenne.
                        // If either $i-1 or $n+1&$n is truthy, the negation will be false.
```
[Answer]
# C, 94 bytes
```
g(n,i){return--i?g(2*n,i):n;}n,r;f(x){for(n=r=1;++n<x;)r=x%n?x^g(2,n)-1?r:r|2:r&2;return r>2;}
```
Returns 1 if the number is a Mersenne Prime, 0 otherwise.
]