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\Statistical;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   8  use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
   9  use PhpOffice\PhpSpreadsheet\Shared\IntOrFloat;
  10  
  11  class Permutations
  12  {
  13      use ArrayEnabled;
  14  
  15      /**
  16       * PERMUT.
  17       *
  18       * Returns the number of permutations for a given number of objects that can be
  19       *        selected from number objects. A permutation is any set or subset of objects or
  20       *        events where internal order is significant. Permutations are different from
  21       *        combinations, for which the internal order is not significant. Use this function
  22       *        for lottery-style probability calculations.
  23       *
  24       * @param mixed $numObjs Integer number of different objects
  25       *                      Or can be an array of values
  26       * @param mixed $numInSet Integer number of objects in each permutation
  27       *                      Or can be an array of values
  28       *
  29       * @return array|float|int|string Number of permutations, or a string containing an error
  30       *         If an array of numbers is passed as an argument, then the returned result will also be an array
  31       *            with the same dimensions
  32       */
  33      public static function PERMUT($numObjs, $numInSet)
  34      {
  35          if (is_array($numObjs) || is_array($numInSet)) {
  36              return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet);
  37          }
  38  
  39          try {
  40              $numObjs = StatisticalValidations::validateInt($numObjs);
  41              $numInSet = StatisticalValidations::validateInt($numInSet);
  42          } catch (Exception $e) {
  43              return $e->getMessage();
  44          }
  45  
  46          if ($numObjs < $numInSet) {
  47              return ExcelError::NAN();
  48          }
  49          $result = round(MathTrig\Factorial::fact($numObjs) / MathTrig\Factorial::fact($numObjs - $numInSet));
  50  
  51          return IntOrFloat::evaluate($result);
  52      }
  53  
  54      /**
  55       * PERMUTATIONA.
  56       *
  57       * Returns the number of permutations for a given number of objects (with repetitions)
  58       *     that can be selected from the total objects.
  59       *
  60       * @param mixed $numObjs Integer number of different objects
  61       *                      Or can be an array of values
  62       * @param mixed $numInSet Integer number of objects in each permutation
  63       *                      Or can be an array of values
  64       *
  65       * @return array|float|int|string Number of permutations, or a string containing an error
  66       *         If an array of numbers is passed as an argument, then the returned result will also be an array
  67       *            with the same dimensions
  68       */
  69      public static function PERMUTATIONA($numObjs, $numInSet)
  70      {
  71          if (is_array($numObjs) || is_array($numInSet)) {
  72              return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet);
  73          }
  74  
  75          try {
  76              $numObjs = StatisticalValidations::validateInt($numObjs);
  77              $numInSet = StatisticalValidations::validateInt($numInSet);
  78          } catch (Exception $e) {
  79              return $e->getMessage();
  80          }
  81  
  82          if ($numObjs < 0 || $numInSet < 0) {
  83              return ExcelError::NAN();
  84          }
  85  
  86          $result = $numObjs ** $numInSet;
  87  
  88          return IntOrFloat::evaluate($result);
  89      }
  90  }