Python、Java、JavaScript の3 言語で、階乗、順列、組み合わせの計算 を実施する方法を調べてみました。
Python、java、JavaScript の3 言語なのは、普段私が使用している言語だからという理由です。
組み合わせ計算のライブラリは、組み合わせた結果の要素数を返す
、計算結果のみを返す
ものがある理解で、それぞれ言語のライブラリの具合を見ていきます。
Python
Python については、Web の日本語情報が多数ありましたので、参考記事のリンクを記載します。
itertools、math を使用する例
numpy, scipy.special, scipy.misc を使用する例
組み合わせた結果の要素数を返す
のが itertools で、計算結果のみを返す
のが math、scipy.special、 scipy.misc です。
JavaScript
Vanilla JS での実行
自作関数での、計算方法です。下記が参考になりました。
* JavaScriptで順列、組合せ | You Look Too Cool
* JSで要素の組み合わせを列挙する
Math.js を使う
math.js | an extensive math library for JavaScript and Node.js というライブラリがあり、このライブラリで階乗、順列、組み合わせの計算ができます。
以下、使用法について記載します。
Chrome console で Math.js を読み込む
cdn から 外部 JavaScript を読み込みます。
var ele = document.createElement("script");
ele.type = "text/javascript";
ele.src = "https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.1.1/math.min.js";
document.body.appendChild(ele);
計算結果のみを返す
- 階乗、順列
math.permutations
で階乗、順列 の計算ができます。
階乗の計算は、以下の通り、math.permutations(4);
math.factorial
でも階乗の計算ができます。
順列の計算は、以下の通りです。math.factorial(4);
math.permutations(4,2);
- 組み合わせ
math.combinations
で組み合わせの計算ができます。
母数よりも、取り出す数が多いとエラーになります。// 5個から3個を選ぶ場合の組み合わせ数 math.combinations(5, 3);
math.combinations(3, 5); ----------- math.js:13664 Uncaught TypeError: k must be less than or equal to n at number, number (math.js:13664) at Object.combinations (math.js:26193) at <anonymous>:1:6 -----------
組み合わせた結果の要素数を返す
Set Function
で python の itertools のような組み合わせを取得できます。
-
math.setCartesian(set1, set2)
JSON.stringify(math.setCartesian([1, 2, 3], [3, 4])); // retrun "[[1,3],[1,4],[2,3],[2,4],[3,3],[3,4]]"
-
math.setPowerset(set)
JSON.stringify(math.setPowerset([2,3,54])); // retrun "[[],[2],[3],[54],[2,3],[2,54],[3,54],[2,3,54]]"
python の itertools と同様の処理を行う
以下のライブラリを使うと、python の itertools と同様のことができそうです。
Java
Apache commons-math3 を使う
apache/commons-math: Mirror of Apache Commons Math で順列、階乗、組み合わせの計算が実施できます。
-
pom.xml の依存定義
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-math3</artifactId> <version>3.5</version> </dependency>
-
階乗の計算
CombinatoricsUtils.factorial()
を使用すると、階乗の計算ができます。
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.math3.util.CombinatoricsUtils; public class FactorialExample { public static void main(String[] args) { // 階乗の計算 long result = CombinatoricsUtils.factorial(5); System.out.println("Outputs of CombinatoricsUtils#factorial()..."); System.out.println(ReflectionToStringBuilder.toString(result)); } }
Outputs of CombinatoricsUtils#factorial()...
java.lang.Long@63961c42[value=120]
- 組み合わせの計算
Combinations
、CombinatoricsUtils#combinationsIterator
で組み合わせの計算が可能です。
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.math3.util.Combinations;
import org.apache.commons.math3.util.CombinatoricsUtils;
import java.util.*;
/**
* CombinationsExample
*/
public class CombinationsExample {
public static void main(String[] args) {
Combinations c = new Combinations(5, 3);
List<int[]> al = new ArrayList<>();
for (int[] iterate : c) {
al.add(iterate);
}
Collections.sort(al, Comparator.comparing((int[] arr) -> arr[0]));
System.out.println("Outputs of combinations...");
for (int[] array : al) {
System.out.println(ReflectionToStringBuilder.toString(array));
}
Iterator<int[]> itr = CombinatoricsUtils.combinationsIterator(5, 3);
System.out.println("Outputs of CombinatoricsUtils#combinationsIterator...");
for (Iterator<int[]> it = itr; it.hasNext(); ) {
int[] array = it.next();
System.out.println(ReflectionToStringBuilder.toString(array));
}
}
}
- 順列の計算
commons-math3 で 順列の計算が可能なメソッドは見つけられませんでした。
CombinatoricsUtils
では、他に2項分布 の計算を行うメソッド、第2スターリング数の計算を行うメソッドがあります。
順列は結構一般的に思うのですが、どうなんでしょうか。
参考
以下、参考になりました。
- Math.js: 多機能JavaScript Mathライブラリ
- 【Java】組合せの数を求める - ヽ|∵|ゝ(Fantom) の 開発blog?
- commons-math 解読 (1) : MathUtils に定義されている static メソッド〜整数関数編〜 - 倭算数理研究所
- スターリング数 - Wikipedia
- CombinatoricsUtils (Apache Commons Math 3.3 API)
以上です。
コメント