正在检查UUID字符串是否为Prime



我已经创建了一个创建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);
    }

您可以使用BigInteger的isProbablePrime:

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

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

最新更新