-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 3.71 ramanujan numbers.rkt
575 lines (514 loc) · 13.3 KB
/
Exercise 3.71 ramanujan numbers.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#lang racket
; Exercise 3.71. Numbers that can be expressed as the sum of two cubes in more than one
; way are sometimes called Ramanujan numbers, in honor of the mathematician Srinivasa
; Ramanujan. Ordered streams of pairs provide an elegant solution to the problem of
; computing these numbers. To find a number that can be written as the sum of two cubes
; in two different ways, we need only generate the stream of pairs of integers (i,j)
; weighted according to the sum i^3 + j^3 (see exercise 3.70), then search the stream
; for two consecutive pairs with the same weight. Write a procedure to generate the
; Ramanujan numbers. The first such number is 1,729. What are the next five?
; S O L U T I O N
(define (pair-weight-ramanujan pair)
(+ (cube (car pair)) (cube (cadr pair)))
)
(define (extract-consecutive-pairs-with-same-weight stream-of-pairs weight-proc)
(let ((p1 (stream-first stream-of-pairs)) (p2 (stream-first (stream-rest stream-of-pairs))))
(if (= (weight-proc p1) (weight-proc p2))
(stream-cons
(list (weight-proc p1) p1 p2)
(extract-consecutive-pairs-with-same-weight
(stream-rest stream-of-pairs)
weight-proc
)
)
(extract-consecutive-pairs-with-same-weight
(stream-rest stream-of-pairs)
weight-proc
)
)
)
)
; This procedure displays 'count' number of elements from the supplied stream
; of pairs along with the weight of each pair
(define (display-pairs-with-weight stream-of-pairs count weight-proc)
(if (= 0 count)
(begin
(newline)
'done
)
(begin
(newline)
(display (stream-first stream-of-pairs))
(display " ")
(display (weight-proc (stream-first stream-of-pairs)))
(display-pairs-with-weight (stream-rest stream-of-pairs) (- count 1) weight-proc)
)
)
)
(define (weighted-pairs s t weight)
(stream-cons
(list (stream-first s) (stream-first t))
(merge-weighted
(stream-map (lambda (x) (list (stream-first s) x)) (stream-rest t))
(weighted-pairs (stream-rest s) (stream-rest t) weight)
weight
)
)
)
(define (merge-weighted s1 s2 weight)
(cond
((stream-empty? s1) s2)
((stream-empty? s2) s1)
(else
(let ((s1car (stream-first s1)) (s2car (stream-first s2)))
(cond
((< (weight s1car) (weight s2car))
(stream-cons s1car (merge-weighted (stream-rest s1) s2 weight))
)
((> (weight s1car) (weight s2car))
(stream-cons s2car (merge-weighted s1 (stream-rest s2) weight))
)
(else
(stream-cons
s1car
(stream-cons
s2car
(merge-weighted (stream-rest s1) (stream-rest s2) weight)
)
)
)
)
)
)
)
)
; The following procedure expects two ascending infinite streams,
; one of which needs to be a subset of the other. Let S be the stream with the superset
; and let T be the stream with the subset. Then this procedure will produce a new stream
; that contains all those elements that are present in S but not in T
(define (s-minus-t s t)
; Assumes that S and T are ordered ascending and T is a subset of S
(cond
((stream-empty? t) s)
((stream-empty? s) empty-stream)
(else
(let ((scar (stream-first s)) (tcar (stream-first t)))
(cond
((< scar tcar)
(stream-cons
scar
(s-minus-t (stream-rest s) t)
)
)
((> scar tcar)
(s-minus-t s (stream-rest t))
)
(else
(s-minus-t (stream-rest s) (stream-rest t))
)
)
)
)
)
)
; The following proc copied from SICP Exercise 3.56
(define (merge s1 s2)
(cond
((stream-empty? s1) s2)
((stream-empty? s2) s1)
(else
(let ((s1car (stream-first s1)) (s2car (stream-first s2)))
(cond
((< s1car s2car)
(stream-cons s1car (merge (stream-rest s1) s2))
)
((> s1car s2car)
(stream-cons s2car (merge s1 (stream-rest s2)))
)
(else
(stream-cons s1car
(merge (stream-rest s1) (stream-rest s2))
)
)
)
)
)
)
)
; Implementation of procedure triples
; The infinite streams S, T and U are represented as follows:
; S0 T0 U0
; S1 T1 U1
; S2 T2 U2
; S3 T3 U3
; S4 T4 U4
; . . .
; . . .
; . . .
; All triples (Si, Tj, Uk) such that i < j < k can be produced by combining the following:
; 1. (S0, T1, U2)
; 2. S0 combined with all the pairs produced using the streams (T1, T2, T3, ...) and
; (U1, U2, U3, ...) such that for every pair (Tj, Uk), j < k. We need to exclude the
; first element from this combined stream because the first element will be
; (S0, T1, U2) which is already accounted for.
; 3. triples called recursively on the streams (S1, S2, ...) (T1, T2, ...) and (U1, U2, ...)
(define (triples s t u)
(stream-cons
(list
(stream-first s)
(stream-first (stream-rest t))
(stream-first (stream-rest (stream-rest u)))
)
(interleave
(stream-map
(lambda (x) (cons (stream-first s) x))
(stream-rest (less-than-pairs (stream-rest t) (stream-rest u)))
)
(triples (stream-rest s) (stream-rest t) (stream-rest u))
)
)
)
(define (is-pythagorean-triple? triple)
; This procedure assumes that the elements in the triple are in increasing order
(= (+ (square (car triple)) (square (car (cdr triple)))) (square (car (cdr (cdr triple)))))
)
; This procedures produces all pairs (Si, Tj) where i < j
(define (less-than-pairs s t)
(stream-cons
(list (stream-first s) (stream-first (stream-rest t)))
(interleave
(stream-map (lambda (x) (list (stream-first s) x)) (stream-rest (stream-rest t)))
(less-than-pairs (stream-rest s) (stream-rest t))
)
)
)
(define (all-pairs s t)
(stream-cons
(list (stream-first s) (stream-first t))
(interleave
(stream-map (lambda (x) (list (stream-first s) x)) (stream-rest t))
(interleave
(stream-map (lambda (x) (list x (stream-first t))) (stream-rest s))
(all-pairs (stream-rest s) (stream-rest t))
)
)
)
)
(define (pairs s t)
(stream-cons
(list (stream-first s) (stream-first t))
(interleave
(stream-map (lambda (x) (list (stream-first s) x)) (stream-rest t))
(pairs (stream-rest s) (stream-rest t))
)
)
)
(define (interleave s1 s2)
(if (stream-empty? s1)
s2
(stream-cons
(stream-first s1)
(interleave s2 (stream-rest s1))
)
)
)
(define (partial-sums S)
(stream-cons (stream-first S) (add-streams (partial-sums S) (stream-rest S)))
)
(define (ln2-summands n)
(stream-cons
(/ 1.0 n)
(stream-map - (ln2-summands (+ n 1)))
)
)
(define ln2-stream
(partial-sums (ln2-summands 1))
)
(define (sqrt x tolerance)
(stream-limit (sqrt-stream x) tolerance)
)
(define (stream-limit s t)
(let ((s0 (stream-ref s 0)) (s1 (stream-ref s 1)))
(if (< (abs (- s1 s0)) t)
s1
(stream-limit (stream-rest s) t)
)
)
)
(define (sqrt-stream x)
(define guesses
(stream-cons
1.0
(stream-map
(lambda (guess) (sqrt-improve guess x))
guesses
)
)
)
guesses
)
(define (sqrt-improve guess x)
(average guess (/ x guess))
)
(define (average x y) (/ (+ x y) 2))
(define (square x) (* x x))
(define (cube x) (* x x x))
(define (euler-transform s)
(let ((s0 (stream-ref s 0)) ; Sn-1
(s1 (stream-ref s 1)) ; Sn
(s2 (stream-ref s 2))) ; Sn+1
(stream-cons
(-
s2
(/ (square (- s2 s1)) (+ s0 (* -2 s1) s2))
)
(euler-transform (stream-rest s))
)
)
)
(define (make-tableau transform s)
(stream-cons
s
(make-tableau transform (transform s))
)
)
(define (accelerated-sequence transform s)
(stream-map stream-first (make-tableau transform s))
)
(define (scale-stream stream factor)
(stream-map (lambda (x) (* x factor)) stream)
)
(define (stream-map proc . argstreams)
; (displayln "Entered stream-map")
(if (stream-empty? (car argstreams))
empty-stream
(stream-cons
(apply proc (map stream-first argstreams))
(apply stream-map (cons proc (map stream-rest argstreams)))
)
)
)
(define (stream-filter pred stream)
(cond
((stream-empty? stream) empty-stream)
((pred (stream-first stream))
(stream-cons
(stream-first stream)
(stream-filter pred (stream-rest stream))
)
)
(else
(stream-filter pred (stream-rest stream))
)
)
)
(define (stream-ref s n)
; (display "Entered stream-ref with n = ")
; (display n)
; (newline)
(if (= n 0)
(stream-first s)
(stream-ref (stream-rest s) (- n 1))
)
)
; This procedure produces a stream with a maximum number of count elements from
; the supplied stream
(define (truncate-stream s count)
(if (<= count 0)
empty-stream
(stream-cons
(stream-first s)
(truncate-stream (stream-rest s) (- count 1))
)
)
)
; This procedure displays a finite number of elements from the supplied stream
; as specified by 'count'
(define (display-stream-elements count s)
(if (= 0 count)
(begin
(newline)
'done
)
(begin
(newline)
(display (stream-first s))
(display-stream-elements (- count 1) (stream-rest s))
)
)
)
(define (display-stream s)
(stream-for-each display-line s)
)
(define (display-line x)
(newline)
(display x)
)
(define (indent-and-display pair)
(display-spaces (car pair))
(display pair)
(newline)
)
(define (display-spaces n)
(if (<= n 0)
(void)
(begin
(display " ")
(display-spaces (- n 1))
)
)
)
(define (div-series dividend-series divisor-series)
(cond
((= 0 (stream-first divisor-series))
(error "Denominator should not have a zero constant: " (stream-first divisor-series))
)
(else
(mul-series
dividend-series
(invert-unit-series (scale-stream divisor-series (/ 1 (stream-first divisor-series))))
)
)
)
)
(define (invert-unit-series s)
(stream-cons
1
(mul-series
(scale-stream (stream-rest s) -1)
(invert-unit-series s)
)
)
)
(define (mul-series s1 s2)
(stream-cons
; (a0 * b0) (The following is the constant term of the series resulting from
; the multiplication)
(* (stream-first s1) (stream-first s2))
; The following is the rest of the series starting with the x^1 term
(add-streams
; {a0 * (b1x + b2x^2 + b3x^3 + ...)} +
; {b0 * (a1x + a2x^2 + a3x^3 + ...)} +
(add-streams
(scale-stream (stream-rest s2) (stream-first s1))
(scale-stream (stream-rest s1) (stream-first s2))
)
; {(a1x + a2x^2 + a3x^3 + ...) * (b1x + b2x^2 + b3x^3 + ...)}
(stream-cons
; 0 needs to be prepended to this stream so that the first term of the stream
; is the x^1 term. Only then the outer add-streams will add like terms in the
; two series supplied to it
0
(mul-series (stream-rest s1) (stream-rest s2))
)
)
)
)
(define exp-series
(stream-cons 1 (integrate-series exp-series))
)
; the integral of negative sine is cosine
(define cosine-series
(stream-cons 1 (integrate-series (scale-stream sine-series -1)))
)
; the integral of cosine is sine
(define sine-series
(stream-cons 0 (integrate-series cosine-series))
)
(define tan-series
(div-series sine-series cosine-series)
)
(define (integrate-series s)
(div-streams s integers)
)
(define (add-streams s1 s2)
(stream-map + s1 s2)
)
(define (div-streams s1 s2)
(stream-map / s1 s2)
)
(define ones (stream-cons 1 ones))
(define integers (stream-cons 1 (add-streams ones integers)))
; Test Driver
(define (run-test return-type proc . args)
(define (print-item-list items first-time?)
(cond
((not (pair? items)) (void))
(else
(if (not first-time?)
(display ", ")
(void)
)
(print (car items))
(print-item-list (cdr items) false)
)
)
)
(display "Applying ")
(display proc)
(if (not (null? args))
(begin
(display " on: ")
(print-item-list args true)
)
(void)
)
(newline)
(let ((result (apply proc args)))
(if (not (eq? return-type 'none))
(display "Result: ")
(void)
)
(cond
((procedure? result) ((result 'print)))
; ((eq? return-type 'deque) (print-deque result))
((eq? return-type 'none) (void))
(else
(print result)
(newline)
)
)
)
(newline)
)
(define (execution-time proc . args)
(define start-time (current-milliseconds))
; (display start-time)
; (display " ")
(apply proc args)
(define end-time (current-milliseconds))
; (display end-time)
(display "Execution time of ")
(display proc)
(display ": ")
(- end-time start-time)
)
; Tests
; Test Results
Welcome to DrRacket, version 6.11 [3m].
Language: racket, with debugging; memory limit: 4096 MB.
> (define P (weighted-pairs integers integers pair-weight-ramanujan))
> (define ramanujan-pairs (extract-consecutive-pairs-with-same-weight P pair-weight-ramanujan))
> (stream-ref ramanujan-pairs 0)
'(1729 (1 12) (9 10))
> (stream-ref ramanujan-pairs 1)
'(4104 (2 16) (9 15))
> (stream-ref ramanujan-pairs 2)
'(13832 (2 24) (18 20))
> (stream-ref ramanujan-pairs 3)
'(20683 (10 27) (19 24))
> (stream-ref ramanujan-pairs 4)
'(32832 (4 32) (18 30))
> (display-stream-elements 10 ramanujan-pairs)
(1729 (1 12) (9 10))
(4104 (2 16) (9 15))
(13832 (2 24) (18 20))
(20683 (10 27) (19 24))
(32832 (4 32) (18 30))
(39312 (2 34) (15 33))
(40033 (9 34) (16 33))
(46683 (3 36) (27 30))
(64232 (17 39) (26 36))
(65728 (12 40) (31 33))
'done
>