Perm sign

perm_sign is a function that computes the sign of a permutation. The output of the function is either 1, indicating the permutation is even, or -1, indicating the permutation is odd.

Syntax

 * SGN = perm_sign(PERM)

Argument descriptions

 * PERM: A vector containing a permutation of the integers 1, 2, ..., n.

Small examples
The identity permutation is even:

The permutation that transposes 3 and 4 is odd:

A large example
This function has no trouble with large permutations. The following code determines the sign of a random permutation of 1:1000000 in under 1/2 of a second on a standard desktop computer: