java - 检查 UUID 字符串是否为素数

标签 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);
    }

最佳答案

您可以使用 BigInteger 的 isProbablePrime:

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

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

关于java - 检查 UUID 字符串是否为素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28612493/

相关文章:

java - 使用文件路径保存 HashMap

java - 在EDT中使用SwingWorker生成的tablemodel

java - paraminterceptor如何自行从String转换为integer?

c++ - C++ 中与平台无关的 GUID 生成?

java - 无法使用 mapper.readValue 将有效的 UUID 字符串转换为 UUID

java - MSys2 下的 Scala - 无法初始化终端

java - react native : Override Error on Package without Duplicate

python - 在 django save() 方法中,如果 id 是 UUIDField,您应该如何识别新对象?

xml - 为什么 Atom (RSS) id 是全局唯一的,而不是本地唯一的?

java - 在 MYSQL 表中存储 UUID 的各种选项及其权衡是什么?