35
2
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, 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. 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 :)
Related: 1, 2
– FlipTack – 2016-12-25T14:19:15.1272If I could I'd vote this as a dupe of the isprime challenge, as it doesn't really add anything new. – flawr – 2016-12-25T14:32:20.277
9@flawr They are very similar - but for this challenge, there is less likely to be a builtin and there are lots of interesting approaches to determining whether a number is representable as
2^n-1
– FlipTack – 2016-12-25T14:39:25.7271I believe the definition of a Mersenne number also mandates that n be prime (a condition that has also been proven necessary, but not sufficient, for (2^n)-1 to be prime.) – SuperJedi224 – 2016-12-25T15:04:02.133
4@SuperJedi224
n
is always prime, but knowing that changes nothing, the definition is still correct. – FlipTack – 2016-12-25T15:13:21.267OP, must the code be able to give a correct answer with a probability of 1? i.e Always be correct 100% of the time? – Buffer Over Read – 2016-12-28T02:11:36.967
2@TheBitByte Yes - if you're implementing some probability-based algorithm which doesn't work 100% of the time, you can still post it, but it wouldn't be competing :) – FlipTack – 2016-12-28T16:06:11.433