File tree Expand file tree Collapse file tree 3 files changed +44
-1
lines changed Expand file tree Collapse file tree 3 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 25
25
习题完成情况:
26
26
- 章节一: 43/46
27
27
- 章节二: 88/97
28
- - 章节三: 64 /82
28
+ - 章节三: 65 /82
29
29
- 章节四: TODO
30
30
- 章节五: TODO
31
31
* 运行
Original file line number Diff line number Diff line change 26
26
(stream-cdr t))
27
27
(pairs-weighted (stream-cdr s) (stream-cdr t) weight)
28
28
weight))))
29
+ (provide pairs-weighted merge-weighted)
29
30
30
31
(module+ test
31
32
(require rackunit)
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 cube-weight (lambda (pair) (let ((i (car pair))
7
+ (j (cadr pair)))
8
+ (+ (* i i i)
9
+ (* j j j)))))
10
+ (define cube-sum-stream (pairs-weighted integers integers cube-weight))
11
+
12
+ (define (find-ramanujan-numbers s)
13
+ (if (or (stream-null? s)
14
+ (stream-null? (stream-cdr s)))
15
+ (error "The given stream is empty " )
16
+ (let* ((first (stream-car s))
17
+ (second (stream-car (stream-cdr s)))
18
+ (first-w (cube-weight first))
19
+ (second-w (cube-weight second)))
20
+ (if (= first-w second-w)
21
+ ;; 找到权重相同的前后相邻两个序对
22
+ (cons-stream
23
+ (list first-w first second)
24
+ ;; 将有后面有相同权重的序对排除
25
+ (find-ramanujan-numbers (stream-filter (lambda (x) (not (= (cube-weight x) first-w)))
26
+ (stream-cdr s)))
27
+ )
28
+ ;; 没找到,递归寻找
29
+ (find-ramanujan-numbers (stream-cdr s) )))))
30
+
31
+ (define ramanujan-stream (find-ramanujan-numbers cube-sum-stream))
32
+
33
+ (module+ test
34
+ (require rackunit)
35
+
36
+ (test-case "Test for ramanujan-numbers "
37
+ (check-equal? (stream-take-n ramanujan-stream 5 ) '((1729 (1 12 ) (9 10 ))
38
+ (4104 (2 16 ) (9 15 ))
39
+ (13832 (2 24 ) (18 20 ))
40
+ (20683 (10 27 ) (19 24 ))
41
+ (32832 (4 32 ) (18 30 )))))
42
+ )
You can’t perform that action at this time.
0 commit comments