2024-03-13 22:01:27 +08:00
|
|
|
<TeXmacs|2.1.2>
|
|
|
|
|
2024-05-15 10:48:51 +08:00
|
|
|
<style|<tuple|generic|no-page-numbers|british|reduced-margins|s7>>
|
2024-03-13 22:01:27 +08:00
|
|
|
|
|
|
|
<\body>
|
|
|
|
<section*|Orders of Growth>
|
|
|
|
|
2024-05-15 10:48:51 +08:00
|
|
|
<\session|s7|default>
|
|
|
|
<\unfolded-io>
|
|
|
|
\<gtr\>\
|
|
|
|
<|unfolded-io>
|
2024-03-13 22:01:27 +08:00
|
|
|
(define (expt-1 b n)
|
|
|
|
|
|
|
|
\ \ (if (= n 0)
|
|
|
|
|
|
|
|
\ \ \ \ \ \ 1
|
|
|
|
|
|
|
|
\ \ \ \ \ \ (* b (expt-1 b (- n 1)))))
|
|
|
|
<|unfolded-io>
|
|
|
|
expt-1
|
|
|
|
</unfolded-io>
|
|
|
|
|
2024-05-15 10:48:51 +08:00
|
|
|
<\unfolded-io>
|
|
|
|
\<gtr\>\
|
|
|
|
<|unfolded-io>
|
2024-03-13 22:01:27 +08:00
|
|
|
(expt-1 1 0)
|
|
|
|
<|unfolded-io>
|
|
|
|
1
|
|
|
|
</unfolded-io>
|
|
|
|
|
2024-05-15 10:48:51 +08:00
|
|
|
<\unfolded-io>
|
|
|
|
\<gtr\>\
|
|
|
|
<|unfolded-io>
|
2024-03-13 22:01:27 +08:00
|
|
|
(expt-1 2 3)
|
|
|
|
<|unfolded-io>
|
|
|
|
8
|
|
|
|
</unfolded-io>
|
|
|
|
|
2024-05-15 10:48:51 +08:00
|
|
|
<\unfolded-io>
|
|
|
|
\<gtr\>\
|
|
|
|
<|unfolded-io>
|
2024-03-13 22:01:27 +08:00
|
|
|
(define (expt-2 b n)
|
|
|
|
|
|
|
|
\ \ (define (expt-iter b counter product)
|
|
|
|
|
|
|
|
\ \ \ \ (if (= counter 0)
|
|
|
|
|
|
|
|
\ \ \ \ \ \ \ \ product
|
|
|
|
|
|
|
|
\ \ \ \ \ \ \ \ (expt-iter b
|
|
|
|
|
|
|
|
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (- counter 1)
|
|
|
|
|
|
|
|
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (* b product))))
|
|
|
|
|
|
|
|
\ \
|
|
|
|
|
|
|
|
\ \ (expt-iter b n 1))
|
|
|
|
<|unfolded-io>
|
|
|
|
expt-2
|
|
|
|
</unfolded-io>
|
|
|
|
|
2024-05-15 10:48:51 +08:00
|
|
|
<\unfolded-io>
|
|
|
|
\<gtr\>\
|
|
|
|
<|unfolded-io>
|
2024-03-13 22:01:27 +08:00
|
|
|
(expt-2 3 3)
|
|
|
|
<|unfolded-io>
|
|
|
|
27
|
|
|
|
</unfolded-io>
|
|
|
|
|
2024-05-15 10:48:51 +08:00
|
|
|
<\input>
|
|
|
|
\<gtr\>\
|
|
|
|
<|input>
|
2024-03-13 22:01:27 +08:00
|
|
|
\;
|
|
|
|
</input>
|
|
|
|
</session>
|
|
|
|
|
|
|
|
<section*|Exponentiation>
|
|
|
|
|
2024-05-15 10:48:51 +08:00
|
|
|
<\session|s7|default>
|
|
|
|
<\unfolded-io>
|
|
|
|
\<gtr\>\
|
|
|
|
<|unfolded-io>
|
2024-03-13 22:01:27 +08:00
|
|
|
(define (fast-expt b n)
|
|
|
|
|
|
|
|
\ \ (define (even? n)
|
|
|
|
|
|
|
|
\ \ \ \ (= (remainder n 2) 0))
|
|
|
|
|
|
|
|
\ \ (define (square x) (* x x))
|
|
|
|
|
|
|
|
\ \
|
|
|
|
|
|
|
|
\ \ (cond ((= n 0) 1)
|
|
|
|
|
|
|
|
\ \ \ \ \ \ \ \ ((even? n) (square (fast-expt b (/ n 2))))
|
|
|
|
|
|
|
|
\ \ \ \ \ \ \ \ (else (* b (fast-expt b (- n 1))))))
|
|
|
|
<|unfolded-io>
|
|
|
|
fast-expt
|
|
|
|
</unfolded-io>
|
|
|
|
|
2024-05-15 10:48:51 +08:00
|
|
|
<\unfolded-io>
|
|
|
|
\<gtr\>\
|
|
|
|
<|unfolded-io>
|
2024-03-13 22:01:27 +08:00
|
|
|
(fast-expt 3 3)
|
|
|
|
<|unfolded-io>
|
|
|
|
27
|
|
|
|
</unfolded-io>
|
2024-05-15 10:48:51 +08:00
|
|
|
|
|
|
|
<\input>
|
|
|
|
\<gtr\>\
|
|
|
|
<|input>
|
|
|
|
\;
|
|
|
|
</input>
|
2024-03-13 22:01:27 +08:00
|
|
|
</session>
|
|
|
|
|
|
|
|
<section*|Greatest Common Divisors>
|
|
|
|
|
2024-05-15 10:48:51 +08:00
|
|
|
<\session|s7|default>
|
|
|
|
<\unfolded-io>
|
|
|
|
\<gtr\>\
|
|
|
|
<|unfolded-io>
|
2024-03-13 22:01:27 +08:00
|
|
|
(define (gcd a b)
|
|
|
|
|
|
|
|
\ \ (if (= b 0)
|
|
|
|
|
|
|
|
\ \ \ \ \ \ a
|
|
|
|
|
|
|
|
\ \ \ \ \ \ (gcd b (remainder a b))))
|
|
|
|
<|unfolded-io>
|
|
|
|
gcd
|
|
|
|
</unfolded-io>
|
|
|
|
|
2024-05-15 10:48:51 +08:00
|
|
|
<\unfolded-io>
|
|
|
|
\<gtr\>\
|
|
|
|
<|unfolded-io>
|
2024-03-13 22:01:27 +08:00
|
|
|
(gcd 10 20)
|
|
|
|
<|unfolded-io>
|
|
|
|
10
|
|
|
|
</unfolded-io>
|
2024-05-15 10:48:51 +08:00
|
|
|
|
|
|
|
<\input>
|
|
|
|
\<gtr\>\
|
|
|
|
<|input>
|
|
|
|
\;
|
|
|
|
</input>
|
2024-03-13 22:01:27 +08:00
|
|
|
</session>
|
|
|
|
</body>
|
|
|
|
|
|
|
|
<\initial>
|
|
|
|
<\collection>
|
|
|
|
<associate|page-screen-margin|true>
|
|
|
|
</collection>
|
|
|
|
</initial>
|
|
|
|
|
|
|
|
<\references>
|
|
|
|
<\collection>
|
|
|
|
<associate|auto-1|<tuple|?|?>>
|
|
|
|
<associate|auto-2|<tuple|?|?>>
|
|
|
|
<associate|auto-3|<tuple|?|?>>
|
|
|
|
</collection>
|
2024-05-15 10:48:51 +08:00
|
|
|
</references>
|
|
|
|
|
|
|
|
<\auxiliary>
|
|
|
|
<\collection>
|
|
|
|
<\associate|toc>
|
|
|
|
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|Orders
|
|
|
|
of Growth> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
|
|
|
|
<no-break><pageref|auto-1><vspace|0.5fn>
|
|
|
|
|
|
|
|
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|Exponentiation>
|
|
|
|
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
|
|
|
|
<no-break><pageref|auto-2><vspace|0.5fn>
|
|
|
|
|
|
|
|
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|Greatest
|
|
|
|
Common Divisors> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
|
|
|
|
<no-break><pageref|auto-3><vspace|0.5fn>
|
|
|
|
</associate>
|
|
|
|
</collection>
|
|
|
|
</auxiliary>
|