精确分析复杂的递推关系(多项式函数)

来源:互联网 发布:冯诺依曼 知乎 编辑:程序博客网 时间:2024/04/28 15:17

精确分析复杂的递推关系(多项式函数)

设置a(n)=5*a(n-1)+3^n+7^n,按照以前的相同的处理方式有:

a(n-1)=5*a(n-2)+3^(n-1)+7^(n-1)

Go

上面两个式子(1)-7*(2)有:

a(n)-7*a(n-1)=5*{ a(n-1)- 7* a(n-2) } + 3^n - 7* 3^(n-1)

化简有:

a(n)-7*a(n-1)=5*{ a(n-1)- 7* a(n-2) } +  - 4* 3^(n-1)

从这里可以看出其中将只存在一个多项式函数,按照这种思维可以获得a(n)的通项公式:

a(n)=A*7^n+B*5^n+C*3^n

现在假设初始值a(1)=1,推理有a(2)=63,a(3)=685

所以可以解得

A=7/2

B=-19/5

C=-3/2

 

 

下面写程序来证明:

(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  expr (n)

(if (eq  n 1)

       1   

         (+  (*  5

                 (expr (-  n  

                           1)))

             (pow 3

                  n)

             (pow 7 

                  n))))

 

(setq  A  7/2)

(setq  B  -19/5)

(setq  C  -3/2)

 

(defun  formula (n)

(+  (*   A          

         (pow  7  n))

    (*   B

         (pow  5  n))

    (*   C

         (pow  3  n))))

 

 

(defun  test (n)

(if (> n 0)

  (progn 

       (print (expr  n))

       (print  'compare)

       (print (formula n))       

       (test (- n 1)))

  (print 'over)))

 

[31]> (test  10)

 

951465423

COMPARE

951465423

133786225

COMPARE

133786225

18682587

COMPARE

18682587

2582245

COMPARE

2582245

351303

COMPARE

351303

46585

COMPARE

46585

5907

COMPARE

5907

685

COMPARE

685

63

COMPARE

63

1

COMPARE

1

OVER

OVER

很明显这里A占据主要位置,并且B,C都为负数,7的指数的增长速度也快于其它两者。

 

原创粉丝点击