[Tutor] A small math puzzle [recreational Python]

Kirby Urner urnerk@qwest.net
Sat, 05 Jan 2002 12:55:56 -0800


Re pure derangements with all elements distinct, a simpler
algorithm, which ignores the whole "fraction object" thread,
is just:

  >>> def drs(n):
  	 if n==1: return 0
	 return n * drs(n-1) + (-1)**n

  >>> drs(1)
  0
  >>> drs(2)
  1
  >>> drs(3)
  2
  >>> drs(4)
  9
  >>> drs(5)
  44
  >>> drs(6)
  265
  >>> drs(7)
  1854

But that's not the answer to the puzzle, as it doesn't
allow repeats of the same letters.

Kirby

Reference:
http://www.theory.csc.uvic.ca/~cos/inf/perm/Derangements.html