Java BigDecimal Value Check isPrimeNumber(BigDecimal inBd)

Here you can find the source of isPrimeNumber(BigDecimal inBd)

Description

is Prime Number

License

Open Source License

Declaration

public static boolean isPrimeNumber(BigDecimal inBd) 

Method Source Code

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

import java.math.BigDecimal;
import java.math.BigInteger;

public class Main {
    public static final BigInteger BI_2 = new BigInteger("2");
    public static final BigInteger BI_3 = new BigInteger("3");

    public static boolean isPrimeNumber(BigDecimal inBd) {

        if (inBd == null || isNotZero(inBd.remainder(BigDecimal.ONE))) {
            return false;
        }//from w  w  w  .  j ava2  s.  c  o m

        return isPrimeNumber(inBd.toBigInteger());
    }

    public static boolean isPrimeNumber(BigInteger inBi) {

        if (inBi == null) {
            return false;
        }

        if (!inBi.isProbablePrime(5))
            return false;

        if (!BI_2.equals(inBi) && BigInteger.ZERO.equals(inBi.mod(BI_2)))
            return false;

        for (BigInteger i = BI_3; i.multiply(i).compareTo(inBi) < 1; i = i.add(BI_2)) {
            if (BigInteger.ZERO.equals(inBi.mod(i)))
                return false;
        }
        return true;

    }

    public static boolean isNotZero(BigDecimal inValue) {

        return inValue == null || BigDecimal.ZERO.compareTo(inValue) != 0;
    }

    public static boolean equals(Object inO1, Object inO2) {

        return equals(inO1, inO2, true);
    }

    /**
     * TODO: make external equal function which can be injected into Utils; just like the toString() methods
     * @param inO1
     * @param inO2
     * @param isBothNullEqual
     * @return
     */
    public static boolean equals(Object inO1, Object inO2, boolean isBothNullEqual) {

        if (inO1 == null && inO2 == null) {
            return isBothNullEqual;
        } else if (inO1 == null && inO2 != null) {
            return false;
        } else if (inO2 == null && inO1 != null) {
            return false;
        }

        return inO1.equals(inO2);
    }

    public static <T extends Comparable<T>> int compareTo(T t1, T t2) {
        return compareToNullLast(t1, t2);
    }

    public static <T extends Comparable<T>> int compareToNullLast(T t1, T t2) {
        if (t1 == t2) {
            return 0;
        } else if (t1 == null) {
            return 1;
        } else if (t2 == null) {
            return -1;
        } else {
            return t1.compareTo(t2);
        }
    }
}

Related

  1. isNullOrZero(BigDecimal number)
  2. isNullOrZero(BigDecimal value)
  3. isParseableBigDecimal(String string)
  4. isPositive(BigDecimal number)
  5. isPositive(BigDecimal value)
  6. isProfitable(BigDecimal ask, BigDecimal bid, BigDecimal fee, BigDecimal minProfit)
  7. isSameValue(final BigDecimal val1, final BigDecimal val2)
  8. isStrictementNegatif(BigDecimal pNombre)
  9. isUnscaledValueEqualsOriginal(BigDecimal bigDecimal, BigInteger bigInteger)