卷积(用母函数理解,一般情况)
设置(1+x)^k*(1+x)^(n-k)=(1+x)^n
考察当k一定的时候,对于特定系数s,有:
C(k,0)*C(n-k,s-0)+C(k,1)*C(n-k,s-1)+...+C(k,s)*C(n-k,0)=C(n,s)
这里考虑s>k时候的情况,由直觉可以知道,当k<label时候,计算过程应该结果,前面日志中主要是考虑在s<label时应该结束。所以代码实际上没什么改变,只是其中在test中将条件改为(if (< temps globaln)来放开限制。
下面写程序来证明:
n=20
(setq globaln 20)
(setq k 10)
(setq s 8)
(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 com (num count)
(/ (slayerex num
count)
(slayer count)))
(defun expr (k label temps)
(if (and (or (> k label)
(eq k label))
(or (> temps label)
(eq temps label)))
(+ (expr k
(1+ label)
temps)
(* (com k
label)
(com (- globaln
k)
(- temps
label))))
0))
(defun formula (temps)
(com globaln
temps))
(defun test (temps)
(if (or (< temps globaln)
(eq temps globaln))
(progn
(print (expr k 0 temps))
(print 'compare)
(print (formula temps))
(test (+ temps 1)))
(print 'over)))
[41]> (test 0)
1
COMPARE
1
20
COMPARE
20
190
COMPARE
190
1140
COMPARE
1140
4845
COMPARE
4845
15504
COMPARE
15504
38760
COMPARE
38760
77520
COMPARE
77520
125970
COMPARE
125970
167960
COMPARE
167960
184756
COMPARE
184756
167960
COMPARE
167960
125970
COMPARE
125970
77520
COMPARE
77520
38760
COMPARE
38760
15504
COMPARE
15504
4845
COMPARE
4845
1140
COMPARE
1140
190
COMPARE
190
20
COMPARE
20
1
COMPARE
1
OVER
OVER