检查UUID字符串是否为Prime

时间:2015-02-19 17:08:40

标签: java uuid

我创建了一个创建128位UUID字符串的方法,我现在想检查这是否是素数。我不能将字符串放入int中,因为它太大了。任何人都可以建议我如何检查?

这是我用于创建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);
    }

1 个答案:

答案 0 :(得分:2)

您可以使用BigInteger的isProbablePrime:

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

如果你传递一个高确定性参数(例如100),那么如果它返回true,那么它实际上是素数的概率非常接近于1.

相关问题