-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.33.rkt
43 lines (33 loc) · 1.03 KB
/
1.33.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#lang sicp
(define (square x) (* x x))
(define (smallest-div n)
(define (divides? a b)
(= 0 (remainder b a)))
(define (find-div n test)
(cond ((> (square test) n) n) ((divides? test n) test)
(else (find-div n (+ test 1)))))
(find-div n 2))
; ensure prime of 1 is false
(define (prime? n)
(if (= n 1) false (= n (smallest-div n))))
(define (filtered-accumulate combiner null-value term a next b filter)
(define (iter a result)
(if (> a b)
result
(if (filter a)
(iter (next a) (combiner result (term a)))
(iter (next a) result))))
(iter a null-value))
(define (sum-of-squares-of-primes a b)
(filtered-accumulate + 0 square a inc b prime?))
(define (gcd m n)
(cond ((< m n) (gcd n m))
((= n 0) m)
(else (gcd n (remainder m n)))))
(define (relative-prime? m n)
(= (gcd m n) 1))
(define (identity x) x)
(define (product-lesser-relative-prime n)
(define (filter x)
(relative-prime? x n))
(filtered-accumulate * 1 identity 1 inc n filter))