X-Git-Url: https://git.rkrishnan.org/?p=sicp.git;a=blobdiff_plain;f=src%2Fsicp%2Fex4_36.rkt;fp=src%2Fsicp%2Fex4_36.rkt;h=5c9ec2d5fecb5c62d5248fe1f7fbe640cb195e27;hp=0000000000000000000000000000000000000000;hb=16ffd75b36a5dbfbda3da703c2732f8f49208c11;hpb=ef9bb63c4d3fa21f9964c3a5e612bfc519406744 diff --git a/src/sicp/ex4_36.rkt b/src/sicp/ex4_36.rkt new file mode 100644 index 0000000..5c9ec2d --- /dev/null +++ b/src/sicp/ex4_36.rkt @@ -0,0 +1,28 @@ +#lang racket + +(require "amb-eli.rkt") + +(define (an-integer-starting-from n) + (amb n (an-integer-starting-from (+ n 1)))) + +(define (an-integer-between low high) + (assert (< low high)) + (amb low (an-integer-between (+ low 1) high))) + +#| + +If we replace an-integer-between with an-integer-starting-from, then +the k grows too high to invalid ranges and never stops. The way to accomplish +correct values is to restrict i, j and k to valid ranges. + +|# + +;; using euclid's formula +;; http://en.wikipedia.org/wiki/Pythagorean_triple#Generating_a_triple +(define (pythagorean-triples) + (let [(n (an-integer-starting-from 1))] + (let [(m (an-integer-starting-from n))] + (assert (> m n)) + (list (- (sqr m) (sqr n)) + (* 2 m n) + (+ (sqr m) (sqr n)))))) \ No newline at end of file