【发布时间】:2012-11-04 07:22:10
【问题描述】:
我已经设法使 Eulers Totient Function 的一个版本工作,尽管它适用于较小的数字(与我需要它计算的 1024 位数字相比,这里较小)
我的版本在这里 -
public static BigInteger eulerTotientBigInt(BigInteger calculate) {
BigInteger count = new BigInteger("0");
for(BigInteger i = new BigInteger("1"); i.compareTo(calculate) < 0; i = i.add(BigInteger.ONE)) {
BigInteger check = GCD(calculate,i);
if(check.compareTo(BigInteger.ONE)==0) {//coprime
count = count.add(BigInteger.ONE);
}
}
return count;
}
虽然这适用于较小的数字,但它的工作原理是遍历从 1 到正在计算的数字的所有可能值。对于大的 BigInteger,这是完全不可行的。
我已经读过,可以在每次迭代中除以数字,从而无需逐个遍历它们。我只是不确定我应该除以什么(我看过的一些例子是用 C 语言并使用 long 和平方根 - 据我所知,我无法计算出准确的BigInteger 的平方根。我还想知道,如果对于像这样的模运算,函数是否需要包含一个参数来说明 mod 是什么。我完全不确定,所以任何建议都非常感谢。
任何人都可以在这里指出正确的方向吗?
PS 当我找到modifying Euler Totient Function 时,我删除了这个问题。我对其进行了调整以与 BigIntegers 一起使用 -
public static BigInteger etfBig(BigInteger n) {
BigInteger result = n;
BigInteger i;
for(i = new BigInteger("2"); (i.multiply(i)).compareTo(n) <= 0; i = i.add(BigInteger.ONE)) {
if((n.mod(i)).compareTo(BigInteger.ZERO) == 0)
result = result.divide(i);
while(n.mod(i).compareTo(BigInteger.ZERO)== 0 )
n = n.divide(i);
}
if(n.compareTo(BigInteger.ONE) > 0)
result = result.subtract((result.divide(n)));
return result;
}
它确实给出了准确的结果,当传递一个 1024 位数字时,它会永远运行(我仍然不确定它是否完成,它已经运行了 20 分钟)。
【问题讨论】:
-
有些过于简单化了,您必须能够分解 n 来计算 1024 位数字的总函数。在涉及计算全部函数的加密协议中,您已经有了质因数分解。
标签: java optimization cryptography public-key-encryption factorization