Commit 0e6be926 authored by Mason Smith's avatar Mason Smith Committed by Nikodemus Siivola

improved -- well, fixed -- SUBFACTORIAL

  Plus tests.
parent 75f9136a
......@@ -227,11 +227,12 @@ greater then K."
(defun subfactorial (n)
"Subfactorial of the non-negative integer N."
(check-type n (integer 0))
(case n
(0 1)
(1 0)
(otherwise
(floor (/ (+ 1 (factorial n)) (exp 1))))))
(if (zerop n)
1
(do ((x 1 (1+ x))
(a 0 (* x (+ a b)))
(b 1 a))
((= n x) a))))
(defun count-permutations (n &optional (k n))
"Number of K element permutations for a sequence of N objects.
......
......@@ -991,6 +991,31 @@
(list p xv))
(2 #(10 2 10)))
(deftest subfactorial.1
(mapcar #'subfactorial (iota 22))
(1
0
1
2
9
44
265
1854
14833
133496
1334961
14684570
176214841
2290792932
32071101049
481066515734
7697064251745
130850092279664
2355301661033953
44750731559645106
895014631192902121
18795307255050944540))
;;;; Arrays
#+nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment