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