Guest User

Untitled

a guest
Jun 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. "use strict";
  2.  
  3. const range = function(a, b) {
  4. const nums = [];
  5. for (let i = a; i <= b; i++)
  6. nums.push(i);
  7. return nums;
  8. };
  9.  
  10. /**
  11. * 組合せ nCk を計算します。
  12. *
  13. * @param {number} n
  14. * @param {number} k 組合せ元の数
  15. * @returns {number[][]} combinations of nCk
  16. */
  17. const combinations = function(n, k) {
  18. /**
  19. *
  20. * @param {number} i
  21. * @param {number[]} nums
  22. */
  23. const f = function(i, nums) {
  24. if (nums.length === 0) {
  25. return [];
  26. } else {
  27. const x = nums[0];
  28. const xs = nums.slice(1);
  29. const ys = f(i + 1, xs);
  30. if (ys.length === 0)
  31. return (x + k - i > n) ? [] : range(x + 1, x + k - i);
  32. else
  33. return [x].concat(ys);
  34. }
  35. };
  36. /**
  37. *
  38. * @param {number[]} nums
  39. */
  40. const g = function(nums) {
  41. if (nums.length === 0)
  42. return [];
  43. else
  44. return [nums].concat(g(f(0, nums)));
  45. };
  46. return g(range(1, k));
  47. };
Add Comment
Please, Sign In to add comment