Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

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