File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 25
25
习题完成情况:
26
26
- 章节一: 43/46
27
27
- 章节二: 88/97
28
- - 章节三: 65 /82
28
+ - 章节三: 66 /82
29
29
- 章节四: TODO
30
30
- 章节五: TODO
31
31
* 运行
Original file line number Diff line number Diff line change
1
+ #lang racket
2
+ (require "stream.rkt " )
3
+ (require "infinite-stream.rkt " )
4
+ (require "exercise3-70.rkt " )
5
+
6
+ (define square-weight (lambda (pair) (let ((i (car pair))
7
+ (j (cadr pair)))
8
+ (+ (* i i)
9
+ (* j j)))))
10
+ (define square-pairs (pairs-weighted integers integers square-weight))
11
+
12
+ (define (find-square-sum-of-3ways s)
13
+ (if (or (stream-null? s)
14
+ (stream-null? (stream-cdr s))
15
+ (stream-null? (stream-cdr (stream-cdr s))))
16
+ (error "The given stream contains less than 3 elements " )
17
+ (let* ((first (stream-car s))
18
+ (second (stream-car (stream-cdr s)))
19
+ (third (stream-car (stream-cdr (stream-cdr s))))
20
+ (first-w (square-weight first))
21
+ (second-w (square-weight second))
22
+ (third-w (square-weight third)))
23
+ (if (and (= first-w second-w) (= second-w third-w))
24
+ ;; 找到两个平方数之和的三种表达方式
25
+ (cons-stream
26
+ (list first-w first second third)
27
+ ;; 将找到的有相同权重的三个序对排除
28
+ (find-square-sum-of-3ways
29
+ (stream-cdr (stream-cdr (stream-cdr s)))))
30
+ ;; 没找到,递归寻找
31
+ (find-square-sum-of-3ways (stream-cdr s) )))))
32
+
33
+ (define 3ways-square-sum-stream (find-square-sum-of-3ways square-pairs))
34
+
35
+ (module+ test
36
+ (require rackunit)
37
+ (test-case "Test for square sum of 3ways "
38
+ (check-equal? (stream-take-n 3ways-square-sum-stream 3 ) '((325 (1 18 ) (6 17 ) (10 15 ))
39
+ (425 (5 20 ) (8 19 ) (13 16 ))
40
+ (650 (5 25 ) (11 23 ) (17 19 ))))
41
+ )
42
+ )
You can’t perform that action at this time.
0 commit comments