Add exercise 4.37

This commit is contained in:
Oliver Payne 2023-11-01 22:41:55 +00:00
parent 0e99db8285
commit f769d990ec
1 changed files with 16 additions and 0 deletions

View File

@ -54,3 +54,19 @@
(define (a-triple-with-sum-from n)
(amb (a-triple-with-sum-between n (+ n 1))
(a-triple-with-sum-from (+ n 1))))
;; Exercise 4.37
;; Alternative version from the book. I think this will be more
;; efficient because it only has to explore the values of k that are
;; sums of the squares of i and j, rather than all integers from j up
;; to high.
(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))))
(require (>= hsq ksq))
(let ((k (sqrt ksq)))
(require (integer? k))
(list i j k))))))