检查UUID字符串是否为质数

5

我创建了一个生成128位UUID字符串的方法,现在想要检查它是否为质数。我无法将该字符串转换为整数,因为它太大了。有人能提供一些建议吗?

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

如果你想对一个大数进行一些计算,你可以使用 long num = big.longValue();。 - SpaceCowboy
2
这是一个奇怪的问题。测试 BigInteger 是否为素数非常容易(请参阅http://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#isProbablePrime-int-),但你为什么关心UUID是否为质数?我迫不及待地想知道。 - erickson
我已经改成了long,但似乎不再是128位了,你能建议一下如何修复吗?@durron597并不是想制造重复,但我认为我的问题是不同的。 - Hayes121
1个回答

2

感谢您的回答,它起作用了。 - Hayes121

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接