From f79c9c2fd720c24a2b94b5e6b5950b1e6fb414e1 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 11 Nov 2017 00:22:18 +0000 Subject: net: Add product-relative-primes. * net/ricketyspace/sicp/one/thirtythree.scm (gcd product-relative-primes): New functions. --- net/ricketyspace/sicp/one/thirtythree.scm | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'net/ricketyspace') 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) -- cgit v1.2.3