/** * Returns the nth-root of this integer. * * @return <code>k<code> such as <code>k^n <= this < (k + 1)^n</code> * @throws ArithmeticException * if this integer is negative and n is even. */ public IInteger nthRoot(int n) throws ArithmeticException { if (sign() == 0) { return IntegerSym.valueOf(0); } else if (sign() < 0) { if (n % 2 == 0) { // even exponent n throw new ArithmeticException(); } else { // odd exponent n return (IntegerSym) ((IntegerSym) negate()).nthRoot(n).negate(); } } else { IntegerSym result; IntegerSym temp = this; do { result = temp; temp = divideAndRemainder(temp.pow(n - 1))[0].add(temp.multiply(IntegerSym.valueOf(n - 1))).divideAndRemainder( IntegerSym.valueOf(n))[0]; } while (temp.compareTo(result) < 0); return result; } }
public IInteger eulerPhi() throws ArithmeticException { IAST ast = factorInteger(); IInteger phi = IntegerSym.valueOf(1); for (int i = 1; i < ast.size(); i++) { IAST element = (IAST) ast.get(i); IntegerSym q = (IntegerSym) element.get(1); int c = ((IInteger) element.get(2)).toInt(); if (c == 1) { phi = phi.multiply(q.subtract(IntegerSym.valueOf(1))); } else { phi = phi.multiply(q.subtract(IntegerSym.valueOf(1)).multiply(q.pow(c - 1))); } } return phi; }