1. 程式人生 > 實用技巧 >用BigInteger求尤拉函式

用BigInteger求尤拉函式

本來想搬磚,無奈沒磚可搬,DIY 吧,反正也花不了多長事件

BigInteger求尤拉函式:

public static BigInteger bigIntegerEuler(BigInteger n) {
        BigInteger ret = new BigInteger(n.toString());

        BigInteger i = new BigInteger("2");
        BigInteger one = new BigInteger("1");
        BigInteger zero = new BigInteger("0");
        
for (; i.multiply(i).compareTo(n) <= 0; i.add(one)) { if (n.mod(i).compareTo(zero) == 0) { ret = ret.subtract(n.divide(i)); do { n = n.divide(i); }while (n.mod(i).compareTo(zero) == 0); } } if
(n.compareTo(one) > 0) { ret = ret.subtract(ret.divide(n)); } return ret; }

參照程式碼:

ll euler(ll n)  // ll 為 long long
{  
    ll ret=n;  
    for(ll i=2;i*i<=n;i++){  
        if(n%i==0){  
            ret-=ret/i;  
            while(n%i==0){  
                n
/=i; } } } if(n>1)ret-=ret/n; return ret; }

生活依然苦澀.........................