Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 75 lines (51 sloc) 1.329 kb
bb2fd30 @klandergren first commit
authored
1 ;;; Sunny Problem 001
2
3 (define (sum x y)
4 (if (> x y)
5 0
6 (+ x (sum (+ x 1) y))))
7
8
9 (define (divides? a b)
10 (= (remainder b a) 0))
11
12 (define (find-divisor n test-divisor)
13 (cond ((> (square test-divisor) n) n)
14 ((divides? test-divisor n) test-divisor)
15 (else (find-divisor n (+ test-divisor 1)))))
16
17 (define (smallest-divisor n)
18 (find-divisor n 2))
19
20 (define (prime? n)
21 (= n (smallest-divisor n)))
22
23
24
25 (define (cube x)
26 (* x x x))
27
28 (define (square x)
29 (* x x))
30
31 (define (inc x)
32 (+ x 1))
33
34 (define (even? x)
35 (= (remainder x 2) 0))
36
37 (define (accumulate term lo next hi)
38 (if (> lo hi)
39 0
40 (+ (term lo)
41 (accumulate term (next lo) next hi))))
42
43 (define (new-accumulate condition term lo next hi)
44 (if (condition lo)
45 (if (> lo hi)
46 0
47 (+ (term lo)
48 (new-accumulate condition term (next lo) next hi)))
49 (new-accumulate condition term (next lo) next hi)))
50
51 ;sum square only evens
52 (define (ssoe lo hi)
53 (new-accumulate even? square lo inc hi))
54
55 ;sum primes
56 (define (sp lo hi)
57 (new-accumulate prime? square lo inc hi))
58
59 (prime? 13)
60
61 (sp 1 5)
62
63 (ssoe 1 7)
64
65
66 ;for sum-of-squares
67 (define (sum-of-squares lo hi)
68 (accumulate square lo inc hi))
69
70 (sum-of-squares 1 3)
71
72 (define (sum-of-cubes lo hi)
73 (accumulate cube lo inc hi))
74
75 (sum-of-cubes 1 3)
Something went wrong with that request. Please try again.