This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 基于 https://cisco.github.io/ChezScheme/csug9.5/csug9_5.pdf 的 12.10. Times and Dates | |
(define (time-prime-test n) | |
(newline) | |
(display n) | |
(start-prime-test n (current-time)) | |
) | |
(define (start-prime-test n start-time) | |
(if (prime? n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (miller-rabin-test n) | |
(define (try-it a) | |
; 因为 1 小于n, 所以 a/n 的余数就是 1. | |
(= (expmode a (- n 1) n) 1) | |
) | |
(try-it (+ 1 (random (- n 1)))) | |
) | |
(define (expmod base exp m) | |
(define (squaremod-with-check x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (sum term a next b) | |
(if (> a b) | |
0 | |
(+ (term a) | |
(sum term (next a) next b) | |
) | |
) | |
) | |
; 结果逼近 Π/8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (sum term a next b) | |
(if (> a b) | |
0 | |
(+ (term a) | |
(sum term (next a) next b) | |
) | |
) | |
) | |
; 求立方. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (product term a next b) | |
(if (> a b) | |
1 | |
(* (term a) | |
(product term (next a) next b) | |
) | |
) | |
) | |
(define (product term a next b) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (accumulate combiner null-value term a next b) | |
(if (> a b) | |
null-value | |
(combiner (term a) | |
(accumulate combiner null-value term (next a) next b) | |
) | |
) | |
) | |
(define (accumulate combiner null-value term a next b) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (filtered-accumulate combiner null-value term a next b filter?) | |
(if (> a b) | |
null-value | |
(combiner (if (filter? a) (term a) null-value) | |
(filtered-accumulate combiner null-value term (next a) next b filter?) | |
) | |
) | |
) | |
(define (filtered-sum term a next b filter?) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 我的答案: | |
; 递归写法. | |
(define (cont-frac-full n d k calculator) | |
(define (iterator i) | |
(if (< i k) | |
(/ (n i) (calculator (d i) (iterator (+ i 1)))) | |
(/ (n i) (d i)) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (newton-transform g) | |
(lambda (x) | |
(- x (/ (g x) ((deriv g) x))))) | |
(define (deriv g) | |
(lambda (x) | |
(/ (- (g (+ x dx)) (g x)) | |
dx) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (png-size w h) (* w h 4.0 1/1024)) |