I have created a method to create a 128 bit UUID string, I now want to check if this is a prime number or not. I cant put the string into an int because it is too big. Can anyone suggest how I would go about checking?
This is the code I have used for creating the UUID
public static String uuid()
{
UUID uuid = UUID.randomUUID();
long hi = uuid.getMostSignificantBits();
long lo = uuid.getLeastSignificantBits();
byte[] bytes = ByteBuffer.allocate(16).putLong(hi).putLong(lo).array();
BigInteger big = new BigInteger(bytes);
String numericUuid = big.toString().replace('-','1'); // just in case
//System.out.println(numericUuid);
return(numericUuid);
}
You could use BigInteger's isProbablePrime:
http://www.tutorialspoint.com/java/math/biginteger_isprobableprime.htm
If you pass a high certainty parameter (e.g. 100) then if this returns true, the probability of it actually being a prime number is extremely close to 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With