# Module: Combinatorics::Permute

Defined in:
lib/combinatorics/permute/cardinality.rb,
lib/combinatorics/permute/mixin.rb

Author:

Since:

• 0.4.0

Modules: Mixin

## Class Method Summary collapse

• Mathematically determine the number of elements in a r-permutations set.

• Compute cardinality of all r-permutations for a set with cardinality c.

## Class Method Details

### .cardinality(n, r = nil) ⇒ Fixnum

Note:

This function is well-known within fields of academic inquiry such as discrete mathematics and set theory. It is represented in "chalkboard" notation by the letter "P."

Mathematically determine the number of elements in a r-permutations set.

Examples:

Calculate total 4-permutations for a set whose cardinality is 6

``````cardinality(6, 4)
# => 360``````

Parameters:

• n (Fixnum)

The number of elements in the input set.

• r (Fixnum) (defaults to: nil)

Cardinality of permuted subsets.

Returns:

• (Fixnum)

The product of the first `r` factors of `n`.

Raises:

• (RangeError)

`n` must be non-negative.

• (RangeError)

`r` must be non-negative.

• (RangeError)

`r` must be less than or equal to `n`.

Since:

• 0.4.0

 ``` 43 44 45 46 47 48 49 50 51 52 53 54 55``` ```# File 'lib/combinatorics/permute/cardinality.rb', line 43 def self.cardinality(n,r=nil) raise(RangeError,"n must be non-negative") if n < 0 case r when 0 then 0 when nil then Math.factorial(n) else raise(RangeError,"r must be non-negative") if r < 0 raise(RangeError,"r must be less than or equal to n") if r > n Math.factorial(n) / Math.factorial(n - r) end end```

### .cardinality_all(n, c = (1..n)) ⇒ Array

Note:

sum of elements will equal `factorial(c)`

Compute cardinality of all r-permutations for a set with cardinality c

Examples:

cardinality_all(4)

``# => [4, 3, 10, 1]``

Parameters:

• c (Fixnum) (defaults to: (1..n))

Input set cardinality.

Returns:

• (Array)

Elements are cardinalities for each subset `1 .. c`.

Raises:

• (RangeError)

`c` must be non-negative.

Since:

• 0.4.0

 ``` 86 87 88 89 90 91 92``` ```# File 'lib/combinatorics/permute/cardinality.rb', line 86 def self.cardinality_all(n,c=(1..n)) if n < 0 raise(RangeError,"n must be non-negative") end c.map { |r| cardinality(n,r) } end```

### .N(n, r = nil) ⇒ Object

Note:

In the study of set theory, permutations are often referenced by the name of an associated algorithm called "n-choose-r."

Since:

• 0.4.0

 ``` 63``` ```# File 'lib/combinatorics/permute/cardinality.rb', line 63 def self.N(n,r=nil); cardinality(n,r); end```

### .N_all(c) ⇒ Object

Since:

• 0.4.0

 ``` 94``` ```# File 'lib/combinatorics/permute/cardinality.rb', line 94 def self.N_all(c); cardinality_all(c); end```

### .NR(n, r = nil) ⇒ Object

Since:

• 0.4.0

 ``` 64``` ```# File 'lib/combinatorics/permute/cardinality.rb', line 64 def self.NR(n,r=nil); cardinality(n,r); end```

### .NR_all(c) ⇒ Object

Since:

• 0.4.0

 ``` 95``` ```# File 'lib/combinatorics/permute/cardinality.rb', line 95 def self.NR_all(c); cardinality_all(c); end```

### .R(n, r = nil) ⇒ Object

Since:

• 0.4.0

 ``` 65``` ```# File 'lib/combinatorics/permute/cardinality.rb', line 65 def self.R(n,r=nil); cardinality(n,r); end```

### .R_all(c) ⇒ Object

Since:

• 0.4.0

 ``` 96``` ```# File 'lib/combinatorics/permute/cardinality.rb', line 96 def self.R_all(c); cardinality_all(c); end```