Skip to main content.
-->
May 31st, 2007

Solution to SICP Exercise 1.30

Structure and Interpretation of Computer Programs

Solution to Exercise 1.30:

(define (sum term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (+ result (term a)))))
  (iter a 0))

Posted by Ken Dyck as Programming at 6:47 PM MST

No Comments »

Solution to SICP Exercise 1.29

Structure and Interpretation of Computer Programs

Solution to Exercise 1.29:

(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a)
         (sum term (next a) next b))))
 
(define (inc n) (+ n 1))
 
(define (simpsons-integral f a b n)
  (define (do-it h)
    (define (y k)
      (f (+ a (* k h))))
    (define (simpson-term k)
      (* (y k)
         (cond ((or (= k 0) (= k n)) 1)
               ((odd? k) 4)
               (else 2))))
    (* (/ h 3) (sum simpson-term 0 inc n)))
  (do-it (/ (- b a) n)))

Posted by Ken Dyck as Programming at 6:36 PM MST

1 Comment »

May 30th, 2007

The Luck Factor

Luck Factor

Ever wanted to improve your luck? Richard Wiseman promises to show you how in The Luck Factor. Read my review here.

Posted by Ken Dyck as Books at 7:57 PM MST

1 Comment »

Solution to SICP Exercise 1.28

Structure and Interpretation of Computer Programs

Solution to Exercise 1.28:

(define (square x)
  (* x x))
 
(define (expmod-with-trivial-sqrt-check base exp m)
  (cond ((= exp 0) 1)
        ((even? exp)
         (let* ((intermediate (expmod-with-trivial-sqrt-check base (/ exp 2) m))
                (squared-mod (remainder (square intermediate) m)))
           (if (and (not (or (= intermediate 1) (= intermediate (- m 1))))
                    (= squared-mod 1))
               0
               squared-mod)))
        (else
         (remainder (* base (expmod-with-trivial-sqrt-check base (- exp 1) m))
                    m))))
 
(define (miller-rabin-test n)
  (define (try-it a )
    (= (expmod-with-trivial-sqrt-check a (- n 1) n) 1))
  (try-it (+ 1 (random (- n 1)))))

Posted by Ken Dyck as Programming at 7:17 PM MST

5 Comments »