BigInteger prime value
In this chapter you will learn:
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:
Home » Java Tutorial » BigDecimal BigInteger