use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class BigIntegerSym method eulerPhi.
public static BigInteger eulerPhi(BigInteger value) throws ArithmeticException {
if (value.equals(BigInteger.ZERO)) {
return BigInteger.ZERO;
}
if (value.equals(BigInteger.ONE)) {
return BigInteger.ONE;
}
IAST ast = AbstractIntegerSym.valueOf(value).factorInteger();
IInteger phi = AbstractIntegerSym.valueOf(1);
for (int i = 1; i < ast.size(); i++) {
IAST element = (IAST) ast.get(i);
IInteger q = (IInteger) element.arg1();
int c = ((IInteger) element.arg2()).toInt();
if (c == 1) {
phi = phi.multiply(q.subtract(AbstractIntegerSym.valueOf(1)));
} else {
phi = phi.multiply(q.subtract(AbstractIntegerSym.valueOf(1)).multiply(q.pow(c - 1)));
}
}
return phi.toBigNumerator();
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class BigIntegerSym method exponent.
/**
* Get the highest exponent of <code>base</code> that divides <code>this</code>
*
* @return the exponent
*/
@Override
public IExpr exponent(IInteger base) {
IInteger b = this;
if (sign() < 0) {
b = b.negate();
} else if (b.isZero()) {
return F.CInfinity;
} else if (b.isOne()) {
return F.C0;
}
if (b.equals(base)) {
return F.C1;
}
BigInteger rest = Primality.countExponent(b.toBigNumerator(), base.toBigNumerator());
return valueOf(rest);
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class BigFractionSym method compareTo.
@Override
public int compareTo(IExpr expr) {
if (expr instanceof IFraction) {
BigInteger valthis = toBigNumerator().multiply(((IFraction) expr).toBigDenominator());
BigInteger valo = ((IFraction) expr).toBigNumerator().multiply(toBigDenominator());
return valthis.compareTo(valo);
}
if (expr instanceof IInteger) {
return fFraction.compareTo(new BigFraction(((IInteger) expr).toBigNumerator(), BigInteger.ONE));
}
if (expr instanceof Num) {
double d = fFraction.doubleValue() - ((Num) expr).getRealPart();
if (d < 0.0) {
return -1;
}
if (d > 0.0) {
return 1;
}
}
return super.compareTo(expr);
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class AbstractIntegerSym method factorize.
/**
* Get all prime factors of this integer
*
* @param result
* add the prime factors to this result list
* @return
*/
public IAST factorize(IAST result) {
IInteger b = this;
if (sign() < 0) {
b = b.negate();
result.append(F.CN1);
} else if (b.isZero()) {
result.append(F.C0);
return result;
} else if (b.isOne()) {
result.append(F.C1);
return result;
}
if (b instanceof IntegerSym) {
Map<Long, Integer> map = PrimeInteger.factors(b.longValue());
for (Map.Entry<Long, Integer> entry : map.entrySet()) {
long key = entry.getKey();
IInteger is = valueOf(key);
for (int i = 0; i < entry.getValue(); i++) {
result.append(is);
}
}
return result;
}
SortedMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
BigInteger rest = Primality.countPrimes32749(b.toBigNumerator(), map);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int key = entry.getKey();
AbstractIntegerSym is = valueOf(key);
for (int i = 0; i < entry.getValue(); i++) {
result.append(is);
}
}
if (rest.equals(BigInteger.ONE)) {
return result;
}
if (rest.isProbablePrime(PRIME_CERTAINTY)) {
result.append(valueOf(rest));
return result;
}
b = valueOf(rest);
SortedMap<BigInteger, Integer> bigMap = new TreeMap<BigInteger, Integer>();
Primality.factorInteger(rest, bigMap);
for (Map.Entry<BigInteger, Integer> entry : bigMap.entrySet()) {
BigInteger key = entry.getKey();
IInteger is = valueOf(key);
for (int i = 0; i < entry.getValue(); i++) {
result.append(is);
}
}
return result;
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class AbstractIntegerSym method pow.
/** {@inheritDoc} */
@Override
public final IInteger pow(final long exponent) throws ArithmeticException {
if (exponent < 0L) {
throw new ArithmeticException("Negative exponent");
}
if (exponent == 0L) {
if (!this.isZero()) {
return F.C1;
}
throw new ArithmeticException("Indeterminate: 0^0");
} else if (exponent == 1L) {
return this;
}
if (isOne()) {
return F.C1;
}
if (isMinusOne()) {
if ((exponent & 1L) == 1L) {
// isOdd(exponent) ?
return F.CN1;
} else {
return F.C1;
}
}
long exp = exponent;
long b2pow = 0;
while ((exp & 1) == 0L) {
b2pow++;
exp >>= 1;
}
IInteger r = this;
IInteger x = r;
while ((exp >>= 1) > 0L) {
x = x.multiply(x);
if ((exp & 1) != 0) {
r = r.multiply(x);
}
}
while (b2pow-- > 0L) {
r = r.multiply(r);
}
return r;
}
Aggregations