sicp

sicp sandbox.
git clone git://git.ricketyspace.net/sicp.git
Log | Files | Refs

commit f79c9c2fd720c24a2b94b5e6b5950b1e6fb414e1
parent cc11528c51c86b9d64a14f9dd038b65444960e5f
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat, 11 Nov 2017 00:22:18 +0000

net: Add product-relative-primes.

* net/ricketyspace/sicp/one/thirtythree.scm
(gcd product-relative-primes): New functions.

Diffstat:
net/ricketyspace/sicp/one/thirtythree.scm | 14+++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)

diff --git 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)