Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- const range = function(a, b) {
- const nums = [];
- for (let i = a; i <= b; i++)
- nums.push(i);
- return nums;
- };
- /**
- * 組合せ nCk を計算します。
- *
- * @param {number} n
- * @param {number} k 組合せ元の数
- * @returns {number[][]} combinations of nCk
- */
- const combinations = function(n, k) {
- /**
- *
- * @param {number} i
- * @param {number[]} nums
- */
- const f = function(i, nums) {
- if (nums.length === 0) {
- return [];
- } else {
- const x = nums[0];
- const xs = nums.slice(1);
- const ys = f(i + 1, xs);
- if (ys.length === 0)
- return (x + k - i > n) ? [] : range(x + 1, x + k - i);
- else
- return [x].concat(ys);
- }
- };
- /**
- *
- * @param {number[]} nums
- */
- const g = function(nums) {
- if (nums.length === 0)
- return [];
- else
- return [nums].concat(g(f(0, nums)));
- };
- return g(range(1, k));
- };
Add Comment
Please, Sign In to add comment