Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Hands on the CombinatoricsUtils class in Apache Commons Math

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

%%classpath add mvn
org.apache.commons commons-math3 3.6.1
Loading...
Loading...
import org.apache.commons.math3.util.CombinatoricsUtils._
import org.apache.commons.math3.util.CombinatoricsUtils._

binomialCoefficient

binomialCoefficient(5, 3)
10
binomialCoefficientDouble(5, 3)
10.0
binomialCoefficientLog(5, 3)
2.302585092994046
checkBinomial(5, 3)

combinationsIterator

Notice that this methods returns an iterator of the type java.util.Iterator[Array[Int]], which is not a Scala Iterator! For combinations/permutations iterator, it is suggested that you use Seq.combinations and/or Seq.permutations.

val it = combinationsIterator(5, 3)
org.apache.commons.math3.util.Combinations$LexicographicIterator@1ab48026

Factorial

factorial(10)
3628800
factorial(100)
org.apache.commons.math3.exception.MathArithmeticException: arithmetic exception
  at org.apache.commons.math3.util.CombinatoricsUtils.factorial(CombinatoricsUtils.java:281)
  ... 46 elided
factorialDouble(100)
9.332621544395286E157
factorialDouble(20)
2.43290200817664E18
factorialLog(100)
363.7393755555636

Stirling

stirlingS2(10, 3)
9330