Calculates the number of derangements of \e n objects.

View versions (1)

Interface

#include <codecogs/maths/combinatorics/sequences/derangements_number.h>

using namespace Maths::Combinatorics::Sequences;

A derangement of n objects is a permutation with no fixed points. If we symbolize the permutation by \sigma, then for a derangment, \sigma(i) is never equal to i.

The number of derangements of n objects is given by the following formula

d(n) = n! \cdot (1 - 1/1! + 1/2! - 1/3! + \ldots (-1)^n/n!)
(1)

Based on the inclusion/exclusion law we are allowed to write

d(n) = \lceil n! / e \rceil
(2)

where \lceil x \rceil is the ceiling function.

Parameters

n
the number of objects

Returns

the number of derangements of n objects

Example:

#include <codecogs/maths/combinatorics/sequences/derangements_number.h>
#include <iostream>
int main()
{
  for (int i = 0; i < 10; i++)
    std::cout << i << " " << Maths::Combinatorics::Sequences::derangements_number(i) << std::endl;
  return 0;
}

Output:

0 1
1 0
2 1
3 2
4 9
5 44
6 265
7 1854
8 14833
9 133496

References

SUBSET, a C++ library of combinatorial routines, http://www.csit.fsu.edu/~burkardt/cpp_src/subset/subset.html

GPL Licence — free for non commercial use. See Licence details.

Interactive Calculator

n
Result