more e2 versions

This commit is contained in:
Dan Ballard 2011-04-01 16:37:14 -07:00
parent be77f4b886
commit 907652a192
4 changed files with 28 additions and 0 deletions

14
e2/e2.cs Normal file
View File

@ -0,0 +1,14 @@
using System;
public class E2 {
public static void Main() {
int sum, last, current, tmp;
last = 0;
sum = 0;
for (current = 1; current < 4000000; tmp = current, current += last, last = tmp) {
if (current % 2 == 0)
sum += current;
}
System.Console.WriteLine(sum);
}
}

8
e2/e2.erl Normal file
View File

@ -0,0 +1,8 @@
-module(e2).
-export([e2/0, fib_count/4]).
fib_count(Last, Current, Max, Sum) when Current >= Max -> Sum;
fib_count(Last, Current, Max, Sum) when Current rem 2 == 0 -> fib_count(Current, Last + Current, Max, Sum + Current);
fib_count(Last, Current, Max, Sum) -> fib_count(Current, Last + Current, Max, Sum).
e2() -> fib_count(0, 1, 4000000, 0).

1
e2/e2.m Normal file
View File

@ -0,0 +1 @@
#include <objc/Object.h>

5
e2/e2.pl Normal file
View File

@ -0,0 +1,5 @@
fib_count(Last, Current, Max, Sum, Sum) :- Current >= Max.
fib_count(Last, Current, Max, Acc, Sum) :- 0 is Current rem 2, Next is Last + Current, NewAcc is Acc + Current, fib_count(Current, Next, Max, NewAcc, Sum).
fib_count(Last, Current, Max, Acc, Sum) :- Next is Last + Current, fib_count(Current, Next, Max, Acc, Sum).
e2(Result) :- fib_count(0, 1, 4000000, 0, Result).