Still Kicking

Tagged as C, LISP, Programming, Project Euler, Self-Learning

Written on 2008-01-30 19:26:42

Okay. So, I didn't get the week 2 recap posted last Friday and I'm not getting it posted today either. Before you folks go judging me and deciding I turned into a lazy bum I thought I should make some note of progress.

As I've mentioned, SICP isn't going as fast as I hoped but I won't skip a thing. If my schedule goes out the window so be it but this book is getting finished. Of course, hopefully I can conform somewhat to the schedule as well. There will be an update this weekend even if I'm not through section 1.2.

In the meantime, I thought that I'd post up something I've been working on during my lunch hour. Namely, Project Euler code. Project Euler is a website that has about 180 Programming Problems of escalating difficult. I've only devoted one lunch hour to it so far but it's been fun and I'd love to get through a quarter to half the problems this year.

The challenge for me I think will come from the math side as well as the programming and some of these I just won't be able to solve for a while. Better to challenge myself from both ends, right? The code's hidden behind a cut for those who don't want their eyes scarred by this programming nonsense. Also, I'll be improving these as I discover better programming formalisms. I'm also solving each problem in both C and Scheme. I want to solve each problem from two paradigms (or more) if possible.

Problem 1 in C:

//Project Euler Problem 1:
//Sum the numbers below 1000 divisible by 3 or 5.

#include

int main (void)
{
int count;
int sum = 0;
for (count = 1; count < 1000; count++){
if ((count % 3 == 0) || (count % 5 == 0))
sum += count;}
printf ("The sum of all multiples of 3 or 5 below 1000 is %d.n", sum);
return (0);
}

Problem 1 in Scheme:

;;Project Euler Problem 1:
;;Sum the numbers below 1,000 divisible by 3 or 5.

(define (euler1 top)
(define (iter count sum)
(define (divides? n)
(or (= (modulo n 3) 0) (= (modulo n 5) 0)))
(cond ((= count top) sum)
((divides? count) (iter (+ count 1) (+ sum count)))
(else (iter (+ count 1) sum))))
(iter 1 0))
;Value: euler1

Problem 2 in C:

//Project Euler Problem 2:
//Sum the even-valued terms in the Fibonacci sequence below 1,000,000.

#include

int main (void)
{
int a = 1;
int b = 2;
int temp, sum = 0;
while (a <= 1000000){
if (a % 2 == 0){
temp = b;
b += a;
sum += a;
a = temp;}
else{
temp = b;
b += a;
a = temp;}}
printf ("The sum of the even valued Fibonacci terms below 1,000,000 is %d.n", sum);
return (0);
}

Problem 2 in Scheme:

;;Project Euler Problem 2:
;;Sum the even-valued terms in the Fibonacci sequence below 1,000,000.

(define (euler2 top)
(define (iter current sum count)
(define (fib n)
(cond ((< n 3) n)
(else (+ (fib (- n 1)) (fib (- n 2))))))
(cond ((> current top) sum)
((even? current) (iter (fib (+ count 1))
(+ sum current) (+ count 1)))
(else (iter (fib (+ count 1)) sum (+ count 1)))))
(iter 0 0 0))
;Value: euler2

That's all for now. Hope I get section 1.2 done by this weekend!
comments powered by Disqus

Unless otherwise credited all material Creative Commons License by Brit Butler