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 2:02


// Bjarne Stroustrup, 2010
// License: Public domain
#include <iostream>
using namespace std;

int main() {
    unsigned long long i = 1, last = 0, total = 0;

    while( i < 4000000 ) {
        if( i % 2 == 0 ) total += i;

        unsigned long long tmplast = i;
        i += last;
        last = tmplast;
    }

    cout << total << endl;
    return 0;
}

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