BigInteger prime value

In this chapter you will learn:

  1. How to know if a BigInteger value prime
  2. How to generate a prime number from BigInteger

Is a BigInteger value prime

boolean isProbablePrime(int certainty) returns true if this BigInteger is probably prime, false if it's definitely composite.

import java.math.BigInteger;
/*from   j  a v  a2s  .c  o  m*/
public class Main {
  public static void main(String args[]) {
    BigInteger n = new BigInteger("1000000000000");
    BigInteger one = new BigInteger("1");
    while (!n.isProbablePrime(7))
      n = n.add(one);
    System.out.println(n.toString(10) + " is probably prime.");
    System.out.println("It is " + n.bitLength() + " bits in length.");
  }
}

The output:

Get a prime number from BigInteger

probablePrime(int bitLength, Random rnd) returns a positive BigInteger that is probably prime, with the specified bitLength.

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
//  j av a2 s . co  m
public class MainClass {
  public static void main(String[] unused) {
    Random prng = new SecureRandom();  // self-seeding
    System.out.println(BigInteger.probablePrime(10, prng));
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. When to use BigDecimal
  2. Why BigDecimal is good for monetary values
Home » Java Tutorial » BigDecimal BigInteger

BigDecimal

    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format

BigInteger

    BigInteger class
    BigInteger creation
    BigInteger add, subtract, multiply and divide
    BigInteger power and modPow
    BigInteger conversion
    BigInteger to String
    BigInteger bit and,or,xor,test,flip,negate
    BigInteger bit shift left and right
    BigInteger prime value
    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format