combinationsWithRep.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createCombinationsWithRep = void 0;
  6. var _factory = require("../../utils/factory.js");
  7. var _number = require("../../utils/number.js");
  8. var _product = require("../../utils/product.js");
  9. var name = 'combinationsWithRep';
  10. var dependencies = ['typed'];
  11. var createCombinationsWithRep = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  12. var typed = _ref.typed;
  13. /**
  14. * Compute the number of ways of picking `k` unordered outcomes from `n`
  15. * possibilities, allowing individual outcomes to be repeated more than once.
  16. *
  17. * CombinationsWithRep only takes integer arguments.
  18. * The following condition must be enforced: k <= n + k -1.
  19. *
  20. * Syntax:
  21. *
  22. * math.combinationsWithRep(n, k)
  23. *
  24. * Examples:
  25. *
  26. * math.combinationsWithRep(7, 5) // returns 462
  27. *
  28. * See also:
  29. *
  30. * combinations, permutations, factorial
  31. *
  32. * @param {number | BigNumber} n Total number of objects in the set
  33. * @param {number | BigNumber} k Number of objects in the subset
  34. * @return {number | BigNumber} Number of possible combinations with replacement.
  35. */
  36. return typed(name, {
  37. 'number, number': function numberNumber(n, k) {
  38. if (!(0, _number.isInteger)(n) || n < 0) {
  39. throw new TypeError('Positive integer value expected in function combinationsWithRep');
  40. }
  41. if (!(0, _number.isInteger)(k) || k < 0) {
  42. throw new TypeError('Positive integer value expected in function combinationsWithRep');
  43. }
  44. if (n < 1) {
  45. throw new TypeError('k must be less than or equal to n + k - 1');
  46. }
  47. if (k < n - 1) {
  48. var _prodrange = (0, _product.product)(n, n + k - 1);
  49. return _prodrange / (0, _product.product)(1, k);
  50. }
  51. var prodrange = (0, _product.product)(k + 1, n + k - 1);
  52. return prodrange / (0, _product.product)(1, n - 1);
  53. },
  54. 'BigNumber, BigNumber': function BigNumberBigNumber(n, k) {
  55. var BigNumber = n.constructor;
  56. var result, i;
  57. var one = new BigNumber(1);
  58. var nMinusOne = n.minus(one);
  59. if (!isPositiveInteger(n) || !isPositiveInteger(k)) {
  60. throw new TypeError('Positive integer value expected in function combinationsWithRep');
  61. }
  62. if (n.lt(one)) {
  63. throw new TypeError('k must be less than or equal to n + k - 1 in function combinationsWithRep');
  64. }
  65. result = one;
  66. if (k.lt(nMinusOne)) {
  67. for (i = one; i.lte(nMinusOne); i = i.plus(one)) {
  68. result = result.times(k.plus(i)).dividedBy(i);
  69. }
  70. } else {
  71. for (i = one; i.lte(k); i = i.plus(one)) {
  72. result = result.times(nMinusOne.plus(i)).dividedBy(i);
  73. }
  74. }
  75. return result;
  76. }
  77. });
  78. });
  79. /**
  80. * Test whether BigNumber n is a positive integer
  81. * @param {BigNumber} n
  82. * @returns {boolean} isPositiveInteger
  83. */
  84. exports.createCombinationsWithRep = createCombinationsWithRep;
  85. function isPositiveInteger(n) {
  86. return n.isInteger() && n.gte(0);
  87. }