(*  subfactorial_values.nb *)
(* *)
(*  Purpose: *)
(*    Evaluate the subfactorial function, which *)
(*    counts the number of permutations of N *)
(*    objects which leave no object unchanged. *)
(*    This is also called a derangement *)
(* *)
(*  Modified:  21 March 2007 *)
(*  Author:  John Burkardt *)

<< DiscreteMath`CombinatorialFunctions`

nvec = {0,1,2,3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
svec = {};
num = Length [ nvec ];

Do [ 
{  
n = nvec[[i]],
s = Subfactorial[n] ,
svec = Append[svec,s],
 Print[ "s[", n,"] = ",s] 
}, {i,1,num,1} ]

fileout=OpenWrite["C:\\Documents and Settings\\burkardt.BURKARDTPC\\Desktop\\Misc\\mathematica\\subfactorial_values.txt"];
SetOptions[fileout,FormatType->TextForm];
Write[fileout,"#  subfactorial_values.txt"];today=DateList[];Write[fileout,"#  Created on ",today];Write[fileout,"#"];
Write[fileout,"#  n  s"];
Write[fileout,"#"];
Do[{n=nvec[[i]];s=svec[[i]];Write[fileout,n,"  ",s]},{i,1,num,1}];Close[fileout];