Validating the UUID String

I created a method for creating a 128-bit UUID string, now I want to check if this is a prime or not. I cannot put a string in int because it is too big. Can anyone suggest how I will check?

This is the code I used to create 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); } 
+5
source share
1 answer

You can use BigInteger isProbablePrime:

http://www.tutorialspoint.com/java/math/biginteger_isprobableprime.htm

If you pass a parameter with a high degree of certainty (for example, 100), then if it returns true, the probability that it is actually a prime number is very close to 1.

+2
source

Source: https://habr.com/ru/post/1213744/


All Articles