summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2017-11-11 00:22:18 +0000
committerrsiddharth <s@ricketyspace.net>2017-11-11 00:22:18 +0000
commitf79c9c2fd720c24a2b94b5e6b5950b1e6fb414e1 (patch)
tree4b16da93a5f8ea70898982472b8d966faf055db3 /net
parentcc11528c51c86b9d64a14f9dd038b65444960e5f (diff)
net: Add product-relative-primes.
* net/ricketyspace/sicp/one/thirtythree.scm (gcd product-relative-primes): New functions.
Diffstat (limited to 'net')
-rw-r--r--net/ricketyspace/sicp/one/thirtythree.scm14
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)