Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[Project Euler] #2

Name: !BBCdr/ZfzM 2011-07-13 1:04

Criticism of my code and discussion about the problem are welcome.

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
    1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

__________________________________________________

(defun euler2 (max)
  (labels ((next-fib (term1 term2 acc)
             (if (> (+ term1 term2) max)
               (return-from next-fib acc))
             (if (evenp (+ term1 term2))
               (next-fib (+ term1 term2) term1 (+ acc term1 term2))
               (next-fib (+ term1 term2) term1 acc))))
   (next-fib 1 1 0)))


I'm sure /prague/ can find at least one way my solution is crappy.  Have at it!

Name: Anonymous 2011-07-13 10:23

#include <stdio.h>

int main() {
  int a = 0, b = 1, tmp, sum = 0;
  while (a < 4000000L) {
    sum += a;
    tmp = a + b * 2;
    b = a + b + tmp;
    a = tmp;
  }
  printf("%d\n", sum);
  return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List