母函数应用的实践
不定方程解的个数
x+y+z+p+q=j,
用母函数能够解得答案是:
C(n-1+j,n-1)
其中n=5,j=10,而对于公式中n应该为6
下面写程序来证明:
(defun pow (num count)
(if (or (> count 1) (eq count 1) )
(* num
(pow num
(- count 1) ) )
1))
(defun slayer ( count)
(if (or (> count 1) (eq count 1) )
(* count
(slayer
(- count 1) ) )
1))
(defun slayerex (num count)
(if (or (> count 1) (eq count 1) )
(* num
(slayerex
(- num 1)
(- count 1) ) )
1))
(defun exprhelp ( count n )
(if (> n 0)
(+ (expr (- count 1)
(- n 1) )
(exprhelp count
(- n 1)))
0))
(defun expr (count n)
(if (eq n 0)
1
(if (eq count 0)
0
(+ (exprhelp count
n)
(expr (- count 1)
n)))))
(defun formula (count n)
(/ (slayerex count
n)
(slayer n)))
(defun test (n)
(if (> n 1)
(progn
(print (expr 5 (1- n)))
(print 'compare)
(print (formula (+ 5 (1- n) -1) (1- n)))
(test (- n 1)))
(print 'over)))
(test 10)
注意其中的选择递归程序写得很优美!