举例分析迭代及递归的复杂度(scheme代码实现)

分别用递归和迭代的方式实现下述的公式:


分子部分的规律为:从第二个开始,每两个数递增一次。因此,可以用如下公式表示:
其中div(n,2)表示n整除2得到的结果。
用程序计算分子中第n个数的值,如下所示:

(define (numerator-num n)
  (* (+ (quotient n 2)
        1)
     2))

分母部分的规律为:从第一个开始,每两个数递增一次。因此,可以用如下公式表示:


用程序计算分母中第n个值,如下所示:

(define (denominator-num n)
  (+ (* (quotient (+ n 1) 2)
     2)
     1))

对于类似

既可以用递归,也可以用迭代的形式得到。
(1)递归表示

(define (product term a next b)
  (if (> a b)
      1
      (* (term a)
         (product term (next a) next b))))

代码中product是过程名,term是公式中的函数fa是起始计算值(也就是公式中的a1),b的终止运算值(也就是公式中的an)。next指的是a(k)变化到a(k+1)的规律,比如对于公式中的分子分母而言,a(k)=ka(k+1)=k+1,因此这里的next就是进行加一操作。递归的思想是通过反复调用过程product,得到最终的结果:


用程序表示过程,可以表示为:

(2)迭代表示

(define (product term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (* (term a) result))))
  (iter a 1))

迭代与递归最根本的区别在于:递归会不停地展开,直到完全展开,然后开始代入计算;迭代会不停的对数据进行替代,而不会展开数据。比如该段代码的公式表示为:![][7]
result值会在迭代过程中不断更新,直到最后将result输出。

其实有两种实现方式,一种是分别迭代或递归分子和分母,最后将分子和分母相除;另一种是讲对应项的分子和分母看作整体,再进行迭代或递归。

因此,可以得到2中不同的递归和迭代方式。
(1)递归方式1:分子分母分别递归,最后再相除。

#lang planet neil/sicp
(#%require (only racket/base current-inexact-milliseconds))


(define (fraction n)
  (define start-time (current-inexact-milliseconds))
  (/ (numerator n)
     (denominator n))
  (newline)
  (define end-time (current-inexact-milliseconds))
  (display (- end-time start-time)))

(define (numerator n)
  (define (fractions-next x) (+ x 1))
  (product numerator-num 1 fractions-next n))

(define (denominator n)
  (define (fractions-next x) (+ x 1))
  (product denominator-num 1 fractions-next n))

(define (numerator-num n)
  (* (+ (quotient n 2)
        1)
     2))

(define (denominator-num n)
  (+ (* (quotient (+ n 1) 2)
     2)
     1))

(define (product term a next b)
  (if (> a b)
      1
      (* (term a)
         (product term (next a) next b))))

(2)递归方式2:分子分母看作一个整体,再进行递归。

#lang planet neil/sicp
(#%require (only racket/math pi))
(#%require (only racket/base current-inexact-milliseconds))


(define (fraction n)
  (define start-time (current-inexact-milliseconds))
  (define (fractions-next x) (+ x 1))
  (product fraction-num 1 fractions-next n)
  (define end-time (current-inexact-milliseconds))
  (display (- end-time start-time)))


(define (fraction-num n)
  (/ (numerator_num n)
     (denominator_num n)))


(define (numerator_num n)
  (* (+ (quotient n 2)
        1)
     2))

(define (denominator_num n)
  (+ (* (quotient (+ n 1) 2)
     2)
     1))


(define (product term a next b)
  (if (> a b)
      1
      (* (term a)
         (product term (next a) next b))))

(3)迭代方式1:分子分母分别迭代,最后再相除。

#lang planet neil/sicp
(#%require (only racket/base current-inexact-milliseconds))

(define (fraction n)
  (define start-time (current-inexact-milliseconds))
  (/ (numerator n) (denominator n))
  (newline)
  (define end-time (current-inexact-milliseconds))
  (display (- end-time start-time)))

(define (numerator n)
  (define start-time (current-inexact-milliseconds))
  (define (fractions-next x) (+ x 1))
  (product numerator_num 1 fractions-next n))

(define (denominator n)
  (define (fractions-next x) (+ x 1))
  (product denominator_num 1 fractions-next n))

(define (numerator_num n)
  (* (+ (quotient n 2)
        1)
     2))

(define (denominator_num n)
  (+ (* (quotient (+ n 1) 2)
     2)
     1))

(define (product term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (* (term a) result))))
  (iter a 1))

(4)迭代方式2:分子分母看作一个整体,再进行迭代。

#lang planet neil/sicp
(#%require (only racket/base current-inexact-milliseconds))

(define (fraction n)
  (define start-time (current-inexact-milliseconds))
  (define (fractions-next x) (+ x 1))
  (product fraction-1num 1 fractions-next n)
  (newline)
  (define end-time (current-inexact-milliseconds))
  (display (- end-time start-time)))

(define (fraction-1num n)
  (/ (numerator_num n)
     (denominator_num n)))

(define (numerator_num n)
  (* (+ (quotient n 2)
        1)
     2))

(define (denominator_num n)
  (+ (* (quotient (+ n 1) 2)
     2)
     1))

(define (product term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (* (term a) result))))
  (iter a 1))

分别使用上述迭代和递归方法,计算n=1000,重复计算50次,记录下每次运算时间,如下图所示。

[7]: http://latex.codecogs.com/svg.latex?(itera_{1}result)=(itera_{2}(f(a_{1}){\times}result))=(itera_{3}(f(a_{2}){\times}result))

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容