This site is supported by donations to The OEIS Foundation.

Template:Derangements/doc

From OeisWiki
Jump to: navigation, search

This documentation subpage contains instructions, categories, or other information for Template:Derangements. [<Edit> Template:Derangements]

[⧼Purge⧽ Template:Derangements/doc]
The {{derangements}} mathematical function template returns the subfactorial
!n
, i.e. the number of derangements of 
n, 0   ≤   n   ≤   12,
otherwise returns an error message.

Usage

{{derangements|a nonnegative integer}}

or

{{subfactorial|a nonnegative integer}}

or

{{subfact|a nonnegative integer}}

or

{{!n|a nonnegative integer}}

Valid argument

Returns 
!n
for a nonnegative integer
n, 0   ≤   n   ≤   12,
otherwise returns an error message.

Examples

A000166 Subfactorial numbers, or number of derangements: number of permutations of 
n, n   ≥   0,
elements with no rencontres, i.e. no fixed points.
{1, 0, 1, 2, 9, 44, 265, 1854, 14833, 133496, 1334961, 14684570, 176214841, 2290792932, 32071101049, 481066515734, 7697064251745, 130850092279664, 2355301661033953, ...}

Examples with valid argument 
0   ≤   n   ≤   12
(returns the number of derangements)

Code Result
{{derangements|12}} 176214841
{{subfactorial|12}} 176214841
{{subfact|12}} 176214841
{{!n|12}} 176214841
Code Result
{{!n|0}} 1
{{!n|1}} 0
{{!n|2}} 1
{{!n|3}} 2
{{!n|4}} 9
{{!n|5}} 44
{{!n|6}} 265
{{!n|7}} 1854
{{!n|8}} 14833
{{!n|9}} 133496
{{!n|10}} 1334961
{{!n|11}} 14684570
{{!n|12}} 176214841
The geometric mean of 
an
and 
dn
rounded to nearest integer yields 
n!
, for 
n   ≥   2
, where 
an
and 
dn
are the number of arrangements and the number of derangements of 
n
, respectively.
Code Result   Code Result
{{root| {{arrangements|0}} * {{derangements|0}} }}
2   1 * 1
  {{n!|0}} 1
{{root| {{arrangements|1}} * {{derangements|1}} }}
2   2 * 0
  {{n!|1}} 1
{{root| {{arrangements|2}} * {{derangements|2}} }}
2   5 * 1
  {{n!|2}} 2
{{root| {{arrangements|3}} * {{derangements|3}} }}
2   16 * 2
  {{n!|3}} 6
{{root| {{arrangements|4}} * {{derangements|4}} }}
2   65 * 9
  {{n!|4}} 24
{{root| {{arrangements|5}} * {{derangements|5}} }}
2   326 * 44
  {{n!|5}} 120
{{root| {{arrangements|6}} * {{derangements|6}} }}
2   1957 * 265
  {{n!|6}} 720
{{root| {{arrangements|7}} * {{derangements|7}} }}
2   13700 * 1854
  {{n!|7}} 5040
{{root| {{arrangements|8}} * {{derangements|8}} }}
2   109601 * 14833
  {{n!|8}} 40320
{{root| {{arrangements|9}} * {{derangements|9}} }}
2   986410 * 133496
  {{n!|9}} 362880

Examples with valid, but out of range, argument 
n   ≥   13
(returns a not so user friendly error message)

Code Result
{{!n|13}} 2290792932
{{!n|14}} 32071101049
{{!n|15}} 481066515734
{{!n|16}} 7697064251745
{{!n|80}} 9.680042524614E+34

Examples with invalid argument (returns a user friendly error message)

Code Result
{{!n|-1}} Derangements error: Argument must be a nonnegative integer
{{!n|0.5}} Derangements error: Argument must be a nonnegative integer
{{!n|text}} Derangements error: Argument must be a nonnegative integer
{{!n|6 blobs}} Derangements error: Argument must be a nonnegative integer

Code

{{ifint| ( {{{1|empty}}} )
| {{#ifexpr: ( {{{1}}} ) >= 0
  | {{#ifexpr: ( {{{1}}} ) = 0
    | 1
    | {{expr| ( {{~Pochhammer| 1 | ( {{{1}}} ) | 1 }} / e ) round 0 }}
    }}
  | {{error| Derangements error: Argument must be a nonnegative integer}} 
  }}
| {{error| Derangements error: Argument must be a nonnegative integer}}
}}

Code for {{arrangements}}

{{ifint| ( {{{1|empty}}} )
| {{#ifexpr: ( {{{1}}} ) >= 0
  | {{#ifexpr: ( {{{1}}} ) = 0
    | 1
    | {{expr| floor ( {{~Pochhammer| 1 | ( {{{1}}} ) | 1 }} * e ) }}
    }}
  | {{error| Arrangements error: Argument must be a nonnegative integer}} 
  }}
| {{error| Arrangements error: Argument must be a nonnegative integer}}
}}

See also