Lucas-Lehmer test for Mersenne numbers (Java)

From LiteratePrograms

Jump to: navigation, search
Other implementations: Erlang | Haskell | J | Java | Lisp | Python | Ruby | Scheme

This program is a code dump.
Code dumps are articles with little or no documentation or rearrangement of code. Please help to turn it into a literate program. Also make sure that the source of this code does consent to release it under the MIT or public domain license.


import java.math.BigInteger;
class LucasLehmer {
    public static boolean lucasLehmer(int p) {
        BigInteger s = BigInteger.valueOf(4);
        BigInteger m = BigInteger.valueOf(2).pow(p).subtract(BigInteger.ONE);
        for (int i = 0; i < p - 2; i++)
            s = s.multiply(s).subtract(2).mod(m);
        return s.equals(BigInteger.ZERO);
    }
}
Download code
Views