Open robbbz opened 9 years ago
This depends on a few things; detecting primes currently has no theoretical time cost bounds; it takes an indeterminate amount of time to assess whether or not a number is truly prime. There are, however, a slew of tests you can run against a number to check. This is how cryptographic engines generate their very, very large primes.
These tests can be run pretty quickly, and in a determinate amount of time. is-prime
is a module that does this already, though I doubt it's cryptographically safe.
In that case, all you would have to do is check if n + 1
, the input, is a power of 2 (i.e. Math.sqrt(n + 1) % 1 === 0
, and if so, return isPrime(n)
. Otherwise, return false
.
If you just want to quickly check to see if number is known to be a Mersenne Prime, you could just copy the numbers from here and use that. Otherwise you might want to use the Lucas-Lehmer test.
^ There you go. :+1:
@kevva @gillstrom Any of you wanna do this one?
@sindresorhus I will start working on this if no one else is working.
@Aravind-Suresh Go ahead :)
@sindresorhus Check if this is okay.
@Aravind-Suresh You're pinging the wrong person. I'm not the one that requested it.
Oh okay. @robbbz Check if this implementation is fine.
See https://en.wikipedia.org/wiki/Mersenne_prime. Basically check if something is of the form:
I've got no idea how difficult/easy this is in Javascript though.