sindresorhus / project-ideas

Need a JavaScript module or looking for ideas? Welcome ✨
544 stars 9 forks source link

Module to check if a number is a mersenne prime #30

Open robbbz opened 9 years ago

robbbz commented 9 years ago

See https://en.wikipedia.org/wiki/Mersenne_prime. Basically check if something is of the form:

Mn = 2n − 1

I've got no idea how difficult/easy this is in Javascript though.

Qix- commented 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.

squarejaw commented 9 years ago

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.

Qix- commented 9 years ago

^ There you go. :+1:

sindresorhus commented 9 years ago

@kevva @gillstrom Any of you wanna do this one?

Aravind-Suresh commented 8 years ago

@sindresorhus I will start working on this if no one else is working.

sindresorhus commented 8 years ago

@Aravind-Suresh Go ahead :)

Aravind-Suresh commented 8 years ago

@sindresorhus Check if this is okay.

sindresorhus commented 8 years ago

@Aravind-Suresh You're pinging the wrong person. I'm not the one that requested it.

Aravind-Suresh commented 8 years ago

Oh okay. @robbbz Check if this implementation is fine.