Monday, August 07, 2006

Collatz Collector Program and M91 Program


I haven't posted in awhile so here is some from my code archive that I have written.
CollatzProblem and the
McCarthy91-Function.

(define (collatz n)
(and (display n))
(cond ((and (odd? n) (> n 1))
(collatz (+ 1 (* 3 n))))
((even? n) (collatz (/ n 2)))))

(define (optimized-collatz n)
(begin (and (display n))
(let (n (modulo 4 x))
(define (f x)
(cond
((= n 0) => (/ n 4))
((= n 1) => (* (/ 3 4) (+ 1 (g (- n 1)))))
((= n 2) => (/ n 2))
((= n 3) => (* (/ 1 2) (+ 1 (* 3 n)))))))
(define (g x)
(cond
((not (= n 0)) => n)
((= n 1) => (/ 1 4 (g (- n 1))))))))


(define (collatz-range n)
(if (= n 0)
(display "Finished")
(and (display #\newline)
(collatz n)
(collatz-range (- n 1)))))
(define (m91 n)
(begin (and (display n))
(cond ((and (<= n 100) (> n 0))
(m91 (m91 (+ n 11))))
((and (> n 0) (- n 10))))))
(define (mc91 n)
(display n)
(cond ((< n 1) (display "Function only defined for positive values of n"))
((<= n 100) (mc91 (mc91 (+ n 11))))
(else (- n 10))))

No comments: