X-Git-Url: https://git.rkrishnan.org/?p=sicp.git;a=blobdiff_plain;f=src%2Fsicp%2Fex4_37.rkt;fp=src%2Fsicp%2Fex4_37.rkt;h=a4528a0070e6777ecc900bb8dc05a419bea00ab8;hp=0000000000000000000000000000000000000000;hb=16ffd75b36a5dbfbda3da703c2732f8f49208c11;hpb=ef9bb63c4d3fa21f9964c3a5e612bfc519406744 diff --git a/src/sicp/ex4_37.rkt b/src/sicp/ex4_37.rkt new file mode 100644 index 0000000..a4528a0 --- /dev/null +++ b/src/sicp/ex4_37.rkt @@ -0,0 +1,25 @@ +#lang racket + +(require "amb-eli.rkt") + +(define (a-pythagorean-triple-between low high) + (let ((i (an-integer-between low high)) + (hsq (* high high))) + (let ((j (an-integer-between i high))) + (let ((ksq (+ (* i i) (* j j)))) + (assert (>= hsq ksq)) + (let ((k (sqrt ksq))) + (assert (integer? k)) + (list i j k)))))) + +#| + +Yes, Ben is correct. The above program prunes the search space by restricting the +possible values of k. + +k^2 <= high^2 + +and sqrt (i^2 + j^2) is an integer. This eliminates a large number of (i, j, k) triples +and hence the search space is a lot less than the naive implementation in the text. + +|#