Saturday, January 31, 2009

Code for My Solution the the First Euler Problem

This was basically the first scheme program I have ever written and the first program i have written in about 4 years, so yes, I know the code is very sloppy...

(define (testdiv n f)
(cond ((= (remainder n f) 0) 1)
(else 0)))
(define (buildlist n m)
(cond ((> n 0) (buildlist (- n 1) (cons n m)))
((= n 0) m)))
(define (test2div n f1 f2)
(cond ((= (testdiv n f1) 1) n)
((= (testdiv n f2) 1) n)
(else 0)))
(define (euler1 n f1 f2)
(cond ((not (empty? (cdr n))) (+ (test2div (car n) f1 f2) (euler1 (cdr n) f1 f2)))
(else (test2div (car n) f1 f2))))

It worked but due to the fact that its been 2 weeks since I wrote it and the code is ugly as hell I cant for the life of me remember how I call it. sure, I could probably figure out how, but if I had any desire to go back to this problem I would almost certainly scrap this and rewrite the code entirely..

No comments: