Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   7  
   8  class Combinations
   9  {
  10      use ArrayEnabled;
  11  
  12      /**
  13       * COMBIN.
  14       *
  15       * Returns the number of combinations for a given number of items. Use COMBIN to
  16       *        determine the total possible number of groups for a given number of items.
  17       *
  18       * Excel Function:
  19       *        COMBIN(numObjs,numInSet)
  20       *
  21       * @param mixed $numObjs Number of different objects, or can be an array of numbers
  22       * @param mixed $numInSet Number of objects in each combination, or can be an array of numbers
  23       *
  24       * @return array|float|int|string Number of combinations, or a string containing an error
  25       *         If an array of numbers is passed as the argument, then the returned result will also be an array
  26       *            with the same dimensions
  27       */
  28      public static function withoutRepetition($numObjs, $numInSet)
  29      {
  30          if (is_array($numObjs) || is_array($numInSet)) {
  31              return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet);
  32          }
  33  
  34          try {
  35              $numObjs = Helpers::validateNumericNullSubstitution($numObjs, null);
  36              $numInSet = Helpers::validateNumericNullSubstitution($numInSet, null);
  37              Helpers::validateNotNegative($numInSet);
  38              Helpers::validateNotNegative($numObjs - $numInSet);
  39          } catch (Exception $e) {
  40              return $e->getMessage();
  41          }
  42  
  43          return round(Factorial::fact($numObjs) / Factorial::fact($numObjs - $numInSet)) / Factorial::fact($numInSet);
  44      }
  45  
  46      /**
  47       * COMBINA.
  48       *
  49       * Returns the number of combinations for a given number of items. Use COMBIN to
  50       *        determine the total possible number of groups for a given number of items.
  51       *
  52       * Excel Function:
  53       *        COMBINA(numObjs,numInSet)
  54       *
  55       * @param mixed $numObjs Number of different objects, or can be an array of numbers
  56       * @param mixed $numInSet Number of objects in each combination, or can be an array of numbers
  57       *
  58       * @return array|float|int|string Number of combinations, or a string containing an error
  59       *         If an array of numbers is passed as the argument, then the returned result will also be an array
  60       *            with the same dimensions
  61       */
  62      public static function withRepetition($numObjs, $numInSet)
  63      {
  64          if (is_array($numObjs) || is_array($numInSet)) {
  65              return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet);
  66          }
  67  
  68          try {
  69              $numObjs = Helpers::validateNumericNullSubstitution($numObjs, null);
  70              $numInSet = Helpers::validateNumericNullSubstitution($numInSet, null);
  71              Helpers::validateNotNegative($numInSet);
  72              Helpers::validateNotNegative($numObjs);
  73              $numObjs = (int) $numObjs;
  74              $numInSet = (int) $numInSet;
  75              // Microsoft documentation says following is true, but Excel
  76              //  does not enforce this restriction.
  77              //Helpers::validateNotNegative($numObjs - $numInSet);
  78              if ($numObjs === 0) {
  79                  Helpers::validateNotNegative(-$numInSet);
  80  
  81                  return 1;
  82              }
  83          } catch (Exception $e) {
  84              return $e->getMessage();
  85          }
  86  
  87          return round(
  88              Factorial::fact($numObjs + $numInSet - 1) / Factorial::fact($numObjs - 1)
  89          ) / Factorial::fact($numInSet);
  90      }
  91  }