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

Pages: 1-

[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 1:49

It's waaaaaaaaay too long.

[+] (0, 1, *+* ... * > 4_000_000).grep: * %% 2;

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;
}

Name: Anonymous 2011-07-13 2:15

sum $ takeWhile (<4000000) $ filter even fibs
  where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

Name: Anonymous 2011-07-13 2:36

Sum[If[Fibonacci[n]<=4000000,If[Mod[Fibonacci[n],2]==0,Fibonacci[n],0],0],{n,1,80}]

Name: Anonymous 2011-07-13 2:57

fib(4000000)

Name: Anonymous 2011-07-13 4:20

F;

Name: Anonymous 2011-07-13 9:45

>>1
(define (euler2 max)
  (let loop ((term1 1) (term2 1) (acc 0))
    (let ((next (+ term1 term2)))
      (if (> next max)
          acc
          (loop next term1 (if (even? next) (+ acc next) acc))))))

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;
}

Name: Anonymous 2011-07-13 10:41

>>9 shit language
>>8 worse than shit language
>>7-kun thinks he's absolutely hilarious
>>5 not sure what it is, but it sure smells like shit
>>4 constipated shit language
>>3 huge steaming pile of shit language
>>2 shit language
>>1 worse than shit language

Name: Anonymous 2011-07-13 11:30

>>10
where's yours?

Name: Anonymous 2011-07-13 11:34

>>11
my what?

Name: Anonymous 2011-07-13 11:51

>>12
your retarded

Name: Anonymous 2011-07-13 11:56

>>13
Right here.
*points to >>13*

Name: Anonymous 2011-07-13 12:25

>>14 pwnt
>>13

Name: Anonymous 2011-07-13 17:20

>>14
>>15
samefNullPointerException

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