use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class BigIntegerSym method divisors.
/**
* Return the divisors of this integer number.
*
* <pre>
* divisors(24) ==> {1,2,3,4,6,8,12,24}
* </pre>
*/
@Override
public IAST divisors() {
if (isOne() || isMinusOne()) {
return F.List(F.C1);
}
Set<IInteger> set = new TreeSet<IInteger>();
final IAST primeFactorsList = factorize(F.List());
int len = primeFactorsList.size() - 1;
// build the k-subsets from the primeFactorsList
for (int k = 1; k < len; k++) {
final KSubsetsList iter = Subsets.createKSubsets(primeFactorsList, k, F.List(), 1);
for (IAST subset : iter) {
if (subset == null) {
break;
}
// create the product of all integers in the k-subset
IInteger factor = F.C1;
for (int j = 1; j < subset.size(); j++) {
factor = factor.multiply((IInteger) subset.get(j));
}
// add this divisor to the set collection
set.add(factor);
}
}
// build the final divisors list from the tree set
final IAST resultList = List(F.C1);
for (IInteger entry : set) {
resultList.append(entry);
}
resultList.append(this);
return resultList;
}
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;
}
Aggregations