Java BigInteger is Prime isFermatPrime(BigInteger f)

Here you can find the source of isFermatPrime(BigInteger f)

Description

Fermat number deterministic primality test (Fermat number is an integer in the form Fn = 22^n + 1).

License

Open Source License

Parameter

Parameter Description
f a checking number

Return

true iff f is a Fermat number and f is prime

Declaration

public static boolean isFermatPrime(BigInteger f) 

Method Source Code

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

import java.math.BigInteger;

public class Main {
    /**//from  w w  w  .  j a  v a 2s  . com
     * Fermat number deterministic primality test
     * (Fermat number is an integer in the form F<sub>n</sub> = 2<sup>2^n</sup> + 1).
     * Constant time.
     * @param f a checking number
     * @return true iff f is a Fermat number and f is prime
     */
    public static boolean isFermatPrime(BigInteger f) {
        if (f.signum() <= 0)
            return false;
        if (f.bitLength() <= 31) {
            // The only prime Fermat numbers in BigInteger range
            // are the first five: 3, 5, 17, 257, 65537.
            switch (f.intValue()) {
            case 3:
            case 5:
            case 17:
            case 257:
            case 65537:
                return true;
            default:
                return false;
            }
        }
        return false;
    }
}

Related

  1. calculateMPrime(BigInteger n, byte[] message)
  2. fnvHash(final byte[] data, final BigInteger fnvOffs, final BigInteger fnvPrime, final BigInteger mod)
  3. hasSqrtModPrime(BigInteger r, BigInteger p)
  4. isBigPrime(BigInteger number)
  5. isCoprime(BigInteger a, BigInteger b)
  6. isPrime(BigInteger value)
  7. primeProcessPart(BigInteger from)
  8. sqrtModPrime(BigInteger rSquare, BigInteger p)