Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if UUID String is Prime [duplicate]

Tags:

java

uuid

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);
    }
like image 299
Hayes121 Avatar asked Oct 31 '22 09:10

Hayes121


1 Answers

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.

like image 188
Josh Avatar answered Nov 13 '22 04:11

Josh