Search in sources :

Example 1 with KSubsetsList

use of org.matheclipse.core.builtin.Combinatoric.Subsets.KSubsetsList 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;
}
Also used : TreeSet(java.util.TreeSet) IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST) KSubsetsList(org.matheclipse.core.builtin.Combinatoric.Subsets.KSubsetsList)

Example 2 with KSubsetsList

use of org.matheclipse.core.builtin.Combinatoric.Subsets.KSubsetsList in project symja_android_library by axkr.

the class IntegerSym 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 = F.ListAlloc(set.size() + 1);
    resultList.append(F.C1);
    for (IInteger entry : set) {
        resultList.append(entry);
    }
    resultList.append(this);
    return resultList;
}
Also used : TreeSet(java.util.TreeSet) IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST) KSubsetsList(org.matheclipse.core.builtin.Combinatoric.Subsets.KSubsetsList)

Aggregations

TreeSet (java.util.TreeSet)2 KSubsetsList (org.matheclipse.core.builtin.Combinatoric.Subsets.KSubsetsList)2 IAST (org.matheclipse.core.interfaces.IAST)2 IInteger (org.matheclipse.core.interfaces.IInteger)2