Attachment 'circledot_factorials.py'

Download

   1 import sys
   2 
   3 n = int(sys.argv[1])
   4 k = int(sys.argv[2])
   5 
   6 facts = [1]
   7 
   8 def fact_straight(n):
   9   r = 1
  10   for i in range(2,n):
  11     r *= i
  12   return r
  13 
  14 def fact(n):
  15   global facts
  16   return facts[n]
  17 
  18 # precompute facts
  19 f = 1
  20 for i in range(1,n):
  21   f *= i
  22   facts.append(f)
  23 
  24 l = 0
  25 
  26 #fact=fact_straight
  27 
  28 for i in xrange(k):
  29   for j in xrange(n-k):
  30     l+=(fact(i+j)/fact(i)/fact(j))*(fact(k-1)/fact(i)/fact(k-1-i))*(fact(n-k-1)/fact(j)/fact(n-k-1-j))
  31 
  32     # slower! 
  33     #l+=fact(i+j)*fact(k-1)*fact(n-k-1)/fact(i)/fact(j)/fact(i)/fact(k-i-1)/fact(j)/fact(n-k-j-1)
  34 
  35     
  36 
  37 
  38 print l

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.