我创建了一个生成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);
}
BigInteger
是否为素数非常容易(请参阅http://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#isProbablePrime-int-),但你为什么关心UUID是否为质数?我迫不及待地想知道。 - erickson