用牛顿二项式开平方

来源:互联网 发布:为什么作死知乎 编辑:程序博客网 时间:2024/04/30 03:29

用牛顿二项式开平方
通项公式为(1+Q)^(m/n)=1+ {m/n} *Q+ {(m/n)*(m/n-1)/2}*Q^2+{(m/n)*(m/n-1)*(m/n-2)/3*2}*Q^3....

比如要计算x^(1/2)
GO
(1+(x-1) )^(1/2)=1+ {1/2} *(x-1)+ {(1/2)*(1/2-1)/2}*(x-1)^2+{(1/2)*(1/2-1)*(1/2-2)/3*2}*(x-1)

^3....


(defun pow (num count)
(if (or (> count 1) (eq  count 1))
      (* num
         (pow num
              (- count 1) ) )
      1))

(defun slist (count)
(if (or (> count 1) (eq  count 1))
      (* count
         (slist  (- count 1) ) )
      1))

(defun slistex (num count)
(if (or (> count 1) (eq  count 1))
      (* num
         (slistex (- num  1) (- count 1) ) )
      1))


(defun  expr  (n exp  rightvalue)
(if  (>  n  10)
       0
      (+  (expr  (+  n  1)
                 exp
                 rightvalue)
          (*  1.0
              (pow rightvalue  n)
              (/  (slistex exp n)
                  (slist  n))))))


(expr  0  2  1)   2^2
(expr  0  3  1)   2^3
(expr  0  4  1)   2^4
(expr  0  2  2)   3^2
(expr  0  3  2)   3^3
(expr  0  4  2)   3^4
(expr  0  (/ 1.0 2)  1)   2^(1/2)

3^(1/2)
Go
{ 4 *(1-1/4) } ^(1/2)
Go
2*{ (1-1/4) } ^(1/2)
Go
(* 2 (expr  0  (/ 1.0 2) (/ -1.0 4) ) )  3^(1/2)
如果直接用下面的方法是错误的,因为2^n是发散的
(expr  0  (/ 1.0 2)  2)   3^(1/2)


5^(1/2)
Go
{ 4 *(1+1/4) } ^(1/2)
Go
2*{ (1+1/4) } ^(1/2)
Go
(* 2 (expr  0  (/ 1.0 2) (/ 1.0 4) ) )  5^(1/2)
如果直接用下面的方法是错误的,因为2^n是发散的
(expr  0  (/ 1.0 2)  2)   5^(1/2)