From 6c9f32f0029b79442866d2a13425f7b8b3163e35 Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Tue, 26 Mar 2024 15:57:56 +0800 Subject: [PATCH] =?UTF-8?q?SICP=2006:=20=E4=BB=A3=E7=A0=81=E6=B8=85?= =?UTF-8?q?=E5=8D=95=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SICP/code06.tm | 159 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) diff --git a/SICP/code06.tm b/SICP/code06.tm index 7bc6910..2ffee00 100644 --- a/SICP/code06.tm +++ b/SICP/code06.tm @@ -267,6 +267,161 @@ #t + <\unfolded-io|Scheme] > + (smallest-divisor 7) + <|unfolded-io> + 7 + + + <\unfolded-io|Scheme] > + (find-divisor 7 2) + <|unfolded-io> + 7 + + + <\input|Scheme] > + \; + + + <\unfolded-io|Scheme] > + (define (square x) (* x x)) + <|unfolded-io> + square + + + <\unfolded-io|Scheme] > + (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)))) + <|unfolded-io> + expmod + + + <\unfolded-io|Scheme] > + (define (fermat-test n) + + \ \ (define (try-it a) + + \ \ \ \ (= (expmod a n n) a)) + + \ \ (try-it (+ 1 (random (- n 1))))) + <|unfolded-io> + fermat-test + + + <\unfolded-io|Scheme] > + (define start 0) + <|unfolded-io> + 0 + + + <\input|Scheme] > + (debug-message "std" "\\nHello\\n") + + + <\unfolded-io|Scheme] > + (define (show-time msg ts) + + \ \ (debug-message "std" + + \ \ \ \ (string-append msg (number-\string (- (texmacs-time) ts)) + "\\n"))) + <|unfolded-io> + show-time + + + + + <\unfolded-io|Scheme] > + (define (fast-prime? n times ts) + + \ \ (cond ((= times 0) + + \ \ \ \ \ \ \ \ \ (begin + + \ \ \ \ \ \ \ \ \ \ \ (show-time "true time: " ts) + + \ \ \ \ \ \ \ \ \ \ \ #t)) + + \ \ \ \ \ \ \ \ ((fermat-test n) (fast-prime? n (- times 1) ts)) + + \ \ \ \ \ \ \ \ (else (begin + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (show-time "false time: " ts) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ #f)))) + <|unfolded-io> + fast-prime? + + + + + <\unfolded-io|Scheme] > + (define (wrap-prime? n times) + + \ \ (fast-prime? n times (texmacs-time))) + <|unfolded-io> + wrap-prime? + + + + + <\unfolded-io|Scheme] > + (wrap-prime? 99999787 100000) + <|unfolded-io> + #t + + + + + <\unfolded-io|Scheme] > + (current-time) + <|unfolded-io> + 1711437953 + + + <\unfolded-io|Scheme] > + (define start (current-time)) + <|unfolded-io> + 1711438362 + + + <\unfolded-io|Scheme] > + (define end (current-time)) + <|unfolded-io> + 1711438370 + + + <\unfolded-io|Scheme] > + (- end start) + <|unfolded-io> + 8 + + + <\unfolded-io|Scheme] > + (texmacs-time) + <|unfolded-io> + 10853179 + + + <\unfolded-io|Scheme] > + (texmacs-time) + <|unfolded-io> + 10859400 + + <\input|Scheme] > \; @@ -296,6 +451,10 @@ |math-font-series||\<#7ED8\>\<#56FE\>> |.>>>>|> + + |math-font-series||\<#5BFB\>\<#627E\>\<#8D28\>\<#6570\>> + |.>>>>|> + \ No newline at end of file