diff options
-rw-r--r-- | net/ricketyspace/sicp/one/thirtythree.scm | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/net/ricketyspace/sicp/one/thirtythree.scm b/net/ricketyspace/sicp/one/thirtythree.scm index 6f54c35..903e9cd 100644 --- a/net/ricketyspace/sicp/one/thirtythree.scm +++ b/net/ricketyspace/sicp/one/thirtythree.scm @@ -18,7 +18,12 @@ (define-module (net ricketyspace sicp one thirtythree) #:use-module (net ricketyspace sicp one twentyeight) - #:export (filtered-accumulate sum-odd sum-prime-sqrs)) + #:export (filtered-accumulate sum-odd sum-prime-sqrs product-relative-primes)) + +(define (gcd a b) + (if (= b 0) + a + (gcd b (remainder a b)))) (define (filtered-accumulate combiner null-value term a next b filter) (cond ((> a b) null-value) @@ -48,6 +53,13 @@ A and B must be an integer greater than zero." (next (lambda (x) (1+ x)))) (filtered-accumulate + null-value term a next b prime?))) +(define (product-relative-primes n) + (let ((null-value 1) + (term (lambda (x) x)) + (next (lambda (x) (1+ x))) + (filter (lambda (x) (= (gcd x n) 1)))) + (filtered-accumulate * null-value term 1 next n filter))) + ;;; Guile REPL ;;; scheme@(guile-user)> ,use (net ricketyspace sicp one thirtythree) |