Content tagged Project Euler

Still Kicking
posted 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!

This blog covers 2015, Books, Butler, C, Dad, Discrete Math, Displays, Education, Erlang, Essay, Gaming, Gapingvoid, HTDP, Hardware, IP Law, LISP, Lecture, Lessig, Linkpost, Linux, Lists, MPAA, Milosz, Music, Neruda, Open Source, Operating Systems, Personal, Pics, Poetry, Programming, Programming Languages, Project Euler, Quotes, Reddit, SICP, Self-Learning, Uncategorized, Webcomic, XKCD, Xmas, \"Real World\", adulthood, apple, careers, coleslaw, consumption, creation, fqa, games, goals, heroes, injustice, linux, lisp, math, melee, metapost, milosz, personal, poetry, programming, ragequit, recreation, rip, strangeloop, work

View content from 2015-05, 2015-03, 2015-02, 2015-01, 2014-11, 2014-09, 2014-07, 2014-05, 2014-01, 2013-10, 2013-09, 2013-07, 2013-06, 2013-05, 2013-04, 2013-03, 2013-01, 2012-12, 2012-10, 2012-09, 2012-08, 2012-06, 2012-05, 2012-04, 2012-03, 2012-01, 2011-10, 2011-09, 2011-08, 2011-07, 2011-06, 2011-05, 2011-04, 2011-02, 2011-01, 2010-11, 2010-10, 2010-09, 2010-08, 2010-07, 2010-05, 2010-04, 2010-03, 2010-02, 2010-01, 2009-12, 2009-11, 2009-10, 2009-09, 2009-08, 2009-07, 2009-06, 2009-05, 2009-04, 2009-03, 2009-02, 2009-01, 2008-12, 2008-11, 2008-10, 2008-09, 2008-08, 2008-07, 2008-06, 2008-05, 2008-04, 2008-03, 2008-02, 2008-01, 2007-12, 2007-11, 2007-10, 2007-09, 2007-08, 2007-07, 2007-06, 2007-05


Unless otherwise credited all material Creative Commons License by Brit Butler