SICP 1.23-1.26体会

本文探讨了SICP习题1.23至1.25中关于质数检测算法的时间复杂度问题及优化尝试。通过对比不同范围内的质数检测效率,分析了可能的影响因素,如编译器优化、函数调用开销等。

1.23 代码改动很简单, 关键是时间。 电脑上算了一下, 100000000以下全是0, 开始还以为代码写错了。

          最后没办法, 用1e10 1e11来计算, 发现比 1e11 1e12快1.2-1.5之间。比2小。想了半天也给不出很合理的解析。

         开始以为是对3 5 7 取余数 比 4 6 8 要慢, 测试了一下,发现也不是这么一回事。网上有人怀疑是函数调用花了一定时间做if 判断, 老实说这东西性能影响也不应有这么大。

         现在唯一想到的,就是编译器做了一些优化,导致性能不完全取决于调用次数。

         这题目,网上也没找到很合理的分析。

1.24  也很遗憾, 开始将times设为10, 计算时间1e10以下完全是0. 后面没办法,将times设为10000, 结果发现时间也很漂浮不定。

          有时候, 1e12-1e13 比 1e11-1e12计算速度还快。 只能说random导致性能很随机了。

1.25 理论上肯定是对的, 时间上就悲剧了。我这边执行 100000000 1000000000区间,直接死机。

         根本原因,还是我们的CPU是32或64位的, 不是无限位。

1.26 这个题目算法导论有清晰的解析。套用算法导论的公式T(n) = 2T(n/2) 可知道 T(n) = Theta(n)

        坦率地说, SICP不是学算法的好书。 也不是讲数据结构的好书。


个人代码如下:

1.23

(define (search-for-primes-new start end count)
    (define (timed-prime-test n)
(newline)
        (display n)
(start-prime-test n (runtime)))
    (define (start-prime-test n start-time)
(if (prime? n)
   (report-prime (- (runtime) start-time))
   0))
    (define (report-prime elapsed-time)
(display " *** ")
        (display elapsed-time)
1)
    (define (prime? n)
(= n (smallest-divisor-new n)))
    (define (smallest-divisor-new n)
(define (find-divisor n test-divisor)
   (cond ((> (square test-divisor) n) n)
 ((divides? test-divisor n) test-divisor)
 (else (find-divisor n (next test-divisor)))))
      (define (divides? a b)
 (= (remainder b a) 0))
      (define (next test-divisor)
 (if (= test-divisor 2)
     3
     (+ test-divisor 2)))
      (find-divisor n 2))
    (define (search-iter start end count)
(if (or (> start end) (= count 0))
   0
   (if (= (timed-prime-test start) 1)
(search-iter (+ start 1) end (- count 1))
(search-iter (+ start 1) end count))))
    (search-iter start end count))


1.24

(define (search-for-primes-new start end count)
    (define (timed-prime-test n)
(newline)
        (display n)
(start-prime-test n (runtime)))
    (define (start-prime-test n start-time)
(if (fast-prime? n 10000)
   (report-prime (- (runtime) start-time))
   0))
    (define (report-prime elapsed-time)
(display " *** ")
        (display elapsed-time)
1)
    (define (search-iter start end count)
(if (or (> start end) (= count 0))
   0
   (if (= (timed-prime-test start) 1)
(search-iter (+ start 1) end (- count 1))
(search-iter (+ start 1) end count))))
    (search-iter start end count))
(define (expmod base exp m)
(cond ((= exp 0) 1)
     ((even? exp)
      (remainder (square (expmod base (/ exp 2) m))
 m))
     (else
      (remainder (* base (expmod base (- exp 1) m))
 m))))
(define (fast-prime? n times)
(cond ((= times 0) true)
     ((fermat-test n) (fast-prime? n (- times 1)))
     (else false)))
(define (fermat-test n)
(define (try-it a)
   (= (expmod a n n) a))
      (try-it (+ 1 (random (- n 1)))))


1.25

(define (search-for-primes-25 start end count)
    (define (timed-prime-test n)
(newline)
        (display n)
(start-prime-test n (runtime)))
    (define (start-prime-test n start-time)
(if (fast-prime? n 10000)
   (report-prime (- (runtime) start-time))
   0))
    (define (report-prime elapsed-time)
(display " *** ")
        (display elapsed-time)
1)
    (define (search-iter start end count)
(if (or (> start end) (= count 0))
   0
   (if (= (timed-prime-test start) 1)
(search-iter (+ start 1) end (- count 1))
(search-iter (+ start 1) end count))))
    (search-iter start end count))
(define (expmod base exp m)
    (remainder (fast-expt base exp) m))
(define (fast-expt base exp)
(cond ((= exp 0) 1)
     ((even? exp)
      (square (fast-expt base (/ exp 2))))
     (else
      (* base (fast-expt base (- exp 1))))))
(define (fast-prime? n times)
(cond ((= times 0) true)
     ((fermat-test n) (fast-prime? n (- times 1)))
     (else false)))
(define (fermat-test n)
(define (try-it a)
   (= (expmod a n n) a))
      (try-it (+ 1 (random (- n 1)))))

<think>好的,用户问到了“sicp-api”,我需要先弄清楚这是什么。首先,我知道SICP是《计算机程序的构造和解释》这本经典教材的缩写,通常与MIT的课程相关。而“api”指的是应用程序接口,所以可能用户指的是与SICP相关的编程接口或工具。 接下来,我回忆是否有官方或社区维护的SICP相关的API。可能的情况包括:提供习题答案的API、课程资源的接口,或者辅助学习SICP内容的工具库。例如,有的项目可能将SICP中的示例代码或习题用Python实现,并提供一个库方便调用。 然后,我需要检查是否有常见的库或项目名为sicp-api。在Python生态中,可能没有直接名为sicp-api的官方包,但可能存在第三方项目。比如,某些教育机构或个人可能开发了辅助学习SICP的Python工具,提供函数或类来演示书中的概念。 如果确实存在这样的API,我需要说明其用途和功能。例如,可能包含Scheme解释器的Python实现,或者SICP习题的Python解决方案库。如果不存在,可能用户误解了名称,需要引导他们到相关资源,如使用Python实现SICP中的概念的库,如symbolic或lisp解释器项目。 另外,可能需要询问用户具体需求,比如他们是否在寻找特定的习题解答,或者希望用Python练习SICP中的概念。例如,有的项目如“Composing Programs”提供了类似SICP的教材,但使用Python,可能相关。 最后,提供可能的解决方案,如推荐使用现有的Python库来实现SICP中的概念,或者指导如何构建自己的工具。同时,列出相关问题,帮助用户进一步澄清需求,比如询问是否在寻找特定的习题解答,或者Scheme到Python的转换工具等。</think><< `sicp-api` 并不是 Python 生态中广泛认知的标准库或官方 API。根据上下文推测,它可能指以下两种方向: 1. **SICP 教材的编程接口工具** 若指《计算机程序的构造和解释》(SICP)相关的编程工具,可能存在第三方实现的辅助库。例如: ```python # 假设存在一个虚构的 sicp_api 模块用于数值计算演示 from sicp_api import newton_method def square(x): return x * x # 使用牛顿法求平方根(类似 SICP1 章示例) root = newton_method(square, derivative, guess=1.0) ``` 2. **自定义 API 服务** 若指用户自定义的 API 服务(如用 FastAPI 构建): ```python from fastapi import FastAPI app = FastAPI() @app.get("/sicp/chapter/{chapter_id}") def get_chapter_content(chapter_id: int): # 伪代码:返回 SICP 特定章节内容 return {"chapter": chapter_id, "content": "..."} ``` **注意事项**: - 若需操作 SICP 中的 Scheme 代码,可结合 `lispy` 等 Scheme 解释器库 - 若需符号计算,可尝试 `sympy` 库实现 SICP 中的抽象概念
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值