java欧拉函数,计算非常大数字JAVA的欧拉函数

I've managed to get a version of Eulers Totient Function working, albeit one that works for smaller numbers (smaller here being smaller compared to the 1024 bit numbers I need it to calculate)

My version is here -

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;

}

While this works for smaller numbers, it works by iterating through every possible from 1 to the number being calculated. With large BigIntegers, this is totally unfeasible.

I've read that it's possible to divide the number on each iteration, removing the need to go through them one by one. I'm just not sure what I'm supposed to divide by what (some of the examples I've looked at are in C and use longs and a square root - as far as I know I can't calculate an accurate an accurate square root of a BigInteger. I'm also wondering that if for modular arithmetic such as this, does the function need to include an argument stating what the mod is. I'm totally unsure on that so any advice much appreciated.

Can anyone point me in the right direction here?

PS I deleted this question when I found modifying Euler Totient Function. I adapted it to work with 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;

}

And it does give an accurate result, bit when passed a 1024 bit number it runs forever (I'm still not sure if it even finished, it's been running for 20 minutes).

解决方案

The algorithm you are trying to write is equivalent to factoring the argument n, which means you should expect it to run forever, practically speaking until either your computer dies or you die. See this post in mathoverflow for more information: How hard is it to compute the Euler totient function?.

If, on the other hand, you want the value of the totient for some large number for which you have the factorization, pass the argument as sequence of (prime, exponent) pairs.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值