Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Project Euler Problem number 2: <a href="http://projecteuler.net/problem=2" rel="nofollow">http://projecteuler.net/problem=2</a></p> <p>Haskell (functional/logic):</p> <pre><code>p2 = sum [x | x &lt;- fibs, (x `mod` 2) == 0] where fibs = unfoldr acc (0,1) where acc (prev, cur) | (prev+cur) &gt; 4000000 = Nothing | otherwise = Just (prev+cur, (cur, prev+cur)) </code></pre> <p>Python (OO):</p> <pre><code>class FibSum(object): def __init__(self, end): self.end = end self.next_two = (1,0) self.sum = 0 def __iter__(self): return self def next(self): current, previous = self.next_two self.next_two = (previous+current, current) new = current+previous if current &gt;= self.end: raise StopIteration elif (new % 2) == 0: self.sum += new else: pass fibcount = FibSum(4000000) [i for i in fibcount] print fibcount.sum </code></pre> <p>C (procedural/imperative):</p> <pre><code>#include &lt;stdio.h&gt; int main(void) { long int sum, newnum, previous = 0; long int current = 1; while(current &lt;= 4000000) { newnum = previous+current; if ((newnum % 2) == 0) { sum = sum + newnum; } previous = current; current = newnum; } printf("%d\n", sum); } </code></pre> <p>And here is a very inefficient version written in MIT Scheme</p> <pre><code>(define (unfold func seed) (let* ((result (func seed))) (cond ((null? result) ()) (else (cons (car result) (unfold func (second result))))))) (define (test x) (cond ((&gt; (sum x) 4000000) ()) (else (list (sum x) (list (second x) (sum x)))))) (define (sum xs) (cond ((null? (cdr xs)) (first xs)) (else (+ (car xs) (sum (cdr xs)))))) (sum (filter (lambda (x) (eq? (modulo x 2) 0)) (unfold test (list 0 1)))) </code></pre> <p>Prolog: <a href="http://snipd.net/prolog-project-euler-problem-2" rel="nofollow">Take from this here, posted by 13tazer31</a></p> <pre><code>fibonacci(_,Current,End,0) :- Current &gt; End. fibonacci(Previous, Current, End, Total) :- divisible(Current, 2), Next is Current + Previous, fibonacci(Current, Next, End, Sum), Total is Sum + Current, !. fibonacci(Previous, Current, End, Total) :- Next is Current + Previous, fibonacci(Current, Next, End, Total). divisible(Number, 0) :- write(‘Error: division by 0′). divisible(Number, Divisor) :- Number mod Divisor =:= 0. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload