Java BigInteger Value Check isMersenneNumber(BigInteger n)

Here you can find the source of isMersenneNumber(BigInteger n)

Description

Method to check if a given number is a Mersenne number (primality of the number is not checked).

License

Open Source License

Parameter

Parameter Description
n a checking number

Return

true iff log2( n + 1 ) is a non-negative integer

Declaration

public static boolean isMersenneNumber(BigInteger n) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.math.BigInteger;

public class Main {
    /**/*from ww w.j a  v  a2  s.  c  om*/
     * Method to check if a given number is a Mersenne number (primality of the number is not checked).
     * Linear time.<br><br>
     * https://en.wikipedia.org/wiki/Mersenne_prime
     * @param n a checking number
     * @return true iff log<sub>2</sub>( n + 1 ) is a non-negative integer
     */
    public static boolean isMersenneNumber(BigInteger n) {
        if (n.signum() < 0)
            return false;
        byte bytes[] = n.toByteArray();
        byte b = bytes[0];
        if ((b & (b + 1)) != 0)
            return false;
        for (int i = 1; i < bytes.length; i++)
            if (bytes[i] != -1)
                return false;
        return true;
    }
}

Related

  1. isInRange(BigInteger in, int range, double eps)
  2. isInt(final BigInteger i)
  3. isIntValue(BigInteger bi)
  4. isLessThan(BigInteger valueA, BigInteger valueB)
  5. isLong(BigInteger number)
  6. isNegative(BigInteger i)
  7. isNumberInRange(String text, BigInteger min, BigInteger max)
  8. isOdd(BigInteger in)
  9. isOdd(BigInteger x)