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