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.
//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);
}
;;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
//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);
}
;;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