Skip to content

Commit

Permalink
Finish 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
cj1128 committed Feb 17, 2024
1 parent a7efca3 commit 636e213
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 66 deletions.
328 changes: 285 additions & 43 deletions README.md

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions chapter-1/1.2/1.14.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
digraph G {
graph [ranksep=0.25];
node [color=gray95, style=filled, fontsize=9, shape=box, margin=.08, width=0, height=0 ];
edge [penwidth=.5, arrowsize=0.5];

"[0] (cc 11 5)" [label = "(cc 11 5)"];
"[0] (cc 11 5)" -> "[1] (cc 11 4)"; "[1] (cc 11 4)" [label="(cc 11 4)", ];
"[1] (cc 11 4)" -> "[2] (cc 11 3)"; "[2] (cc 11 3)" [label="(cc 11 3)", ];
"[2] (cc 11 3)" -> "[3] (cc 11 2)"; "[3] (cc 11 2)" [label="(cc 11 2)", ];
"[3] (cc 11 2)" -> "[4] (cc 11 1)"; "[4] (cc 11 1)" [label="(cc 11 1)", ];
"[4] (cc 11 1)" -> "[5] (cc 11 0)"; "[5] (cc 11 0)" [label="(cc 11 0)", color=gray85];
"[4] (cc 11 1)" -> "[5] (cc 10 1)"; "[5] (cc 10 1)" [label="(cc 10 1)", ];
"[5] (cc 10 1)" -> "[6] (cc 10 0)"; "[6] (cc 10 0)" [label="(cc 10 0)", color=gray85];
"[5] (cc 10 1)" -> "[6] (cc 9 1)"; "[6] (cc 9 1)" [label="(cc 9 1)", ];
"[6] (cc 9 1)" -> "[7] (cc 9 0)"; "[7] (cc 9 0)" [label="(cc 9 0)", color=gray85];
"[6] (cc 9 1)" -> "[7] (cc 8 1)"; "[7] (cc 8 1)" [label="(cc 8 1)", ];
"[7] (cc 8 1)" -> "[8] (cc 8 0)"; "[8] (cc 8 0)" [label="(cc 8 0)", color=gray85];
"[7] (cc 8 1)" -> "[8] (cc 7 1)"; "[8] (cc 7 1)" [label="(cc 7 1)", ];
"[8] (cc 7 1)" -> "[9] (cc 7 0)"; "[9] (cc 7 0)" [label="(cc 7 0)", color=gray85];
"[8] (cc 7 1)" -> "[9] (cc 6 1)"; "[9] (cc 6 1)" [label="(cc 6 1)", ];
"[9] (cc 6 1)" -> "[10] (cc 6 0)"; "[10] (cc 6 0)" [label="(cc 6 0)", color=gray85];
"[9] (cc 6 1)" -> "[10] (cc 5 1)"; "[10] (cc 5 1)" [label="(cc 5 1)", ];
"[10] (cc 5 1)" -> "[11] (cc 5 0)"; "[11] (cc 5 0)" [label="(cc 5 0)", color=gray85];
"[10] (cc 5 1)" -> "[11] (cc 4 1)"; "[11] (cc 4 1)" [label="(cc 4 1)", ];
"[11] (cc 4 1)" -> "[12] (cc 4 0)"; "[12] (cc 4 0)" [label="(cc 4 0)", color=gray85];
"[11] (cc 4 1)" -> "[12] (cc 3 1)"; "[12] (cc 3 1)" [label="(cc 3 1)", ];
"[12] (cc 3 1)" -> "[13] (cc 3 0)"; "[13] (cc 3 0)" [label="(cc 3 0)", color=gray85];
"[12] (cc 3 1)" -> "[13] (cc 2 1)"; "[13] (cc 2 1)" [label="(cc 2 1)", ];
"[13] (cc 2 1)" -> "[14] (cc 2 0)"; "[14] (cc 2 0)" [label="(cc 2 0)", color=gray85];
"[13] (cc 2 1)" -> "[14] (cc 1 1)"; "[14] (cc 1 1)" [label="(cc 1 1)", ];
"[14] (cc 1 1)" -> "[15] (cc 1 0)"; "[15] (cc 1 0)" [label="(cc 1 0)", color=gray85];
"[14] (cc 1 1)" -> "[15] (cc 0 1)"; "[15] (cc 0 1)" [label="(cc 0 1)", color=gray85];
"[3] (cc 11 2)" -> "[4] (cc 6 2)"; "[4] (cc 6 2)" [label="(cc 6 2)", ];
"[4] (cc 6 2)" -> "[5] (cc 6 1)"; "[5] (cc 6 1)" [label="(cc 6 1)", ];
"[5] (cc 6 1)" -> "[6] (cc 6 0)"; "[6] (cc 6 0)" [label="(cc 6 0)", color=gray85];
"[5] (cc 6 1)" -> "[6] (cc 5 1)"; "[6] (cc 5 1)" [label="(cc 5 1)", ];
"[6] (cc 5 1)" -> "[7] (cc 5 0)"; "[7] (cc 5 0)" [label="(cc 5 0)", color=gray85];
"[6] (cc 5 1)" -> "[7] (cc 4 1)"; "[7] (cc 4 1)" [label="(cc 4 1)", ];
"[7] (cc 4 1)" -> "[8] (cc 4 0)"; "[8] (cc 4 0)" [label="(cc 4 0)", color=gray85];
"[7] (cc 4 1)" -> "[8] (cc 3 1)"; "[8] (cc 3 1)" [label="(cc 3 1)", ];
"[8] (cc 3 1)" -> "[9] (cc 3 0)"; "[9] (cc 3 0)" [label="(cc 3 0)", color=gray85];
"[8] (cc 3 1)" -> "[9] (cc 2 1)"; "[9] (cc 2 1)" [label="(cc 2 1)", ];
"[9] (cc 2 1)" -> "[10] (cc 2 0)"; "[10] (cc 2 0)" [label="(cc 2 0)", color=gray85];
"[9] (cc 2 1)" -> "[10] (cc 1 1)"; "[10] (cc 1 1)" [label="(cc 1 1)", ];
"[10] (cc 1 1)" -> "[11] (cc 1 0)"; "[11] (cc 1 0)" [label="(cc 1 0)", color=gray85];
"[10] (cc 1 1)" -> "[11] (cc 0 1)"; "[11] (cc 0 1)" [label="(cc 0 1)", color=gray85];
"[4] (cc 6 2)" -> "[5] (cc 1 2)"; "[5] (cc 1 2)" [label="(cc 1 2)", ];
"[5] (cc 1 2)" -> "[6] (cc 1 1)"; "[6] (cc 1 1)" [label="(cc 1 1)", ];
"[6] (cc 1 1)" -> "[7] (cc 1 0)"; "[7] (cc 1 0)" [label="(cc 1 0)", color=gray85];
"[6] (cc 1 1)" -> "[7] (cc 0 1)"; "[7] (cc 0 1)" [label="(cc 0 1)", color=gray85];
"[5] (cc 1 2)" -> "[6] (cc -4 2)"; "[6] (cc -4 2)" [label="(cc -4 2)", color=gray85];
"[2] (cc 11 3)" -> "[3] (cc 1 3)"; "[3] (cc 1 3)" [label="(cc 1 3)", ];
"[3] (cc 1 3)" -> "[4] (cc 1 2)"; "[4] (cc 1 2)" [label="(cc 1 2)", ];
"[4] (cc 1 2)" -> "[5] (cc 1 1)"; "[5] (cc 1 1)" [label="(cc 1 1)", ];
"[5] (cc 1 1)" -> "[6] (cc 1 0)"; "[6] (cc 1 0)" [label="(cc 1 0)", color=gray85];
"[5] (cc 1 1)" -> "[6] (cc 0 1)"; "[6] (cc 0 1)" [label="(cc 0 1)", color=gray85];
"[4] (cc 1 2)" -> "[5] (cc -4 2)"; "[5] (cc -4 2)" [label="(cc -4 2)", color=gray85];
"[3] (cc 1 3)" -> "[4] (cc -9 3)"; "[4] (cc -9 3)" [label="(cc -9 3)", color=gray85];
"[1] (cc 11 4)" -> "[2] (cc -14 4)"; "[2] (cc -14 4)" [label="(cc -14 4)", color=gray85];
"[0] (cc 11 5)" -> "[1] (cc -39 5)"; "[1] (cc -39 5)" [label="(cc -39 5)", color=gray85];
}
33 changes: 22 additions & 11 deletions chapter-1/1.2/1.14.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
# using GraphViz
coins = [50, 25, 10, 5, 1]


# return (node string, label string)
def get_str(level, amount, kind):
return (f"[{level}] (cc {amount} {kind})", f"(cc {amount} {kind})")


def is_leaf(amount, kind):
return amount <= 0 or kind == 0


def cc(level, amount, kind):
(ns, ls) = get_str(level, amount, kind)
(node_name, label) = get_str(level, amount, kind)

if level == 0:
print(f' "{ns}" [label = "{ls}"];')
print(f' "{node_name}" [label = "{label}"];')

if amount < 0:
return 0
Expand All @@ -25,28 +28,36 @@ def cc(level, amount, kind):
left_amount = amount
left_kind = kind - 1
left_is_leaf = is_leaf(left_amount, left_kind)
(left_ns, left_ls) = get_str(level+1, left_amount, left_kind)
print(f' "{ns}" -> "{left_ns}"; "{left_ns}" [label="{left_ls}", {"color=gray85" if left_is_leaf else ""}];')
left = cc(level+1, left_amount, left_kind)
(left_node, left_label) = get_str(level + 1, left_amount, left_kind)
print(
f' "{node_name}" -> "{left_node}"; "{left_node}" [label="{left_label}", {"color=gray85" if left_is_leaf else ""}];'
)
left = cc(level + 1, left_amount, left_kind)

right_amount = amount - coins[5-kind]
right_amount = amount - coins[5 - kind]
right_kind = kind
right_is_leaf = is_leaf(right_amount, right_kind)
(right_ns, right_ls) = get_str(level+1, right_amount, right_kind)
print(f' "{ns}" -> "{right_ns}"; "{right_ns}" [label="{right_ls}", {"color=gray85" if right_is_leaf else ""}];')
right = cc(level+1, right_amount, right_kind)
(right_node, right_label) = get_str(level + 1, right_amount, right_kind)
print(
f' "{node_name}" -> "{right_node}"; "{right_node}" [label="{right_label}", {"color=gray85" if right_is_leaf else ""}];'
)
right = cc(level + 1, right_amount, right_kind)

return left + right


def run():
print("""digraph G {
print(
"""digraph G {
graph [ranksep=0.25];
node [color=gray95, style=filled, fontsize=9, shape=box, margin=.08, width=0, height=0 ];
edge [penwidth=.5, arrowsize=0.5];
""")
"""
)

cc(0, 11, 5)

print("}")


run()
5 changes: 1 addition & 4 deletions chapter-1/1.2/1.16.scm
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
(define (square n) (* n n))
(define (even? n) (= (remainder n 2) 0))

(define (expt b n)
(define (iter a b n)
(cond
Expand All @@ -9,4 +6,4 @@
(else (iter (* a b) (square b) (/ (- n 1) 2)))))
(iter 1 b n))

(display (expt 2 100))
(display (expt 2 20))
4 changes: 1 addition & 3 deletions chapter-1/1.2/1.18.scm
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
(define (double x) (+ x x))
(define (halve x) (/ x 2))
(define (even? x) (= (remainder x 2) 0))


(define (mul a b)
(define (iter s a b)
Expand All @@ -14,4 +12,4 @@
(display (mul 2 100))
(newline)

(display (mul 4 6))
(display (mul 3 7))
15 changes: 15 additions & 0 deletions chapter-1/1.2/1.19.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(define (fib n)
(define (fib-iter a b p q count)
(cond ((= count 0) b)
((even? count)
(fib-iter
a b (+ (square p) (square q)) (+ (square q) (* 2 p q)) (/ count 2)))
(else (fib-iter
(+ (* b q) (* a q) (* a p))
(+ (* b p) (* a q))
p
q
(- count 1)))))
(fib-iter 1 0 0 1 n))

(display (fib 100))
35 changes: 34 additions & 1 deletion chapter-1/1.2/1.22.scm
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
(define (prime? n)
(define (smallest-divisor n)
(find-divisor n 2))

(define (find-divisor n test)
(cond
((> (* test test) n) n)
((divide? test n) test)
(else (find-divisor n (+ 1 test)))))

(define (divide? test n)
(= (remainder n test) 0))

(if (= (smallest-divisor n) n) true false))

(define (search-for-primes n count)
Expand All @@ -19,7 +22,7 @@
(display "***")
(display (- (real-time-clock) begin-time))
(newline)
(search-for-primes (+ n 1) (- count 1)))
(search-for-primes (+ n 2) (- count 1)))
(search-for-primes (+ n 1) count)))))

(search-for-primes 1e6 3)
Expand All @@ -33,3 +36,33 @@

(search-for-primes 1e9 3)
(newline)

### 1.23

{empty}::
+
--

link:chapter-1/1.2/1.23.scm[] is almost identical to 1.22.scm.

----
1000003.***1
1000033.***1
1000037.***1

10000019.***3
10000079.***3
10000103.***3

100000007.***10
100000037.***8
100000039.***8

1000000007.***24
1000000009.***25
1000000021.***24
----

We can see that the process runs basically twice as fast as exercise 1.22, so the expectation is confirmed.

--
4 changes: 4 additions & 0 deletions chapter-1/1.2/1.23.scm
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
(define (prime? n)
(define (next n) (if (= n 2) 3 (+ n 2)))

(define (smallest-divisor n)
(find-divisor n 2))

(define (find-divisor n test)
(cond
((> (* test test) n) n)
((divide? test n) test)
(else (find-divisor n (next test)))))

(define (divide? test n)
(= (remainder n test) 0))

(if (= (smallest-divisor n) n) true false))

(define (search-for-primes n count)
Expand Down
9 changes: 5 additions & 4 deletions chapter-1/1.2/1.28.scm
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
(define (even? n)
(= (remainder n 2) 0))

(define (checked-square x m)
(if (and
(= (remainder (* x x) m) 1)
(= (remainder (square x) m) 1)
(not (or (= x 1) (= x (- m 1)))))
0
(square x)))
Expand All @@ -29,6 +26,10 @@
((miller-rabin-test n) (fast-prime? n (- times 1)))
(else false)))

; real prime
(display (fast-prime? 7919 10))
(newline)

; Carmichael numbers: 561, 1105, 1729, 2465, 2821, 6601
(display (fast-prime? 561 10))
(newline)
Expand Down
6 changes: 6 additions & 0 deletions chapter-1/1.2/fast-expt.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(define (fast-expt b n)
(cond ((= n 0) 1)
((even? n) (square (fast-expt b (/ n 2))))
(else (* b (fast-expt b (- n 1))))))

(display (fast-expt 2 20))
4 changes: 4 additions & 0 deletions chapter-1/1.2/fast-prime.scm
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@
((fermat-test n) (fast-prime? n (- times 1)))
(else false)))

; wrong result, 6601 is not a prime, but it can fool the Fermat test
(display (fast-prime? 6601 10))
(newline)

(display (fast-prime? 9677 10))
2 changes: 2 additions & 0 deletions chapter-1/1.2/prime.scm
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
(define (divides? a b) (= (remainder b a) 0))

(define (prime? n) (= n (smallest-divisor n)))

(display (prime? 7919))
1 change: 1 addition & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
indent-width = 2

0 comments on commit 636e213

Please sign in to comment.