more e2 versions
This commit is contained in:
parent
be77f4b886
commit
907652a192
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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).
|
|
@ -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).
|
Loading…
Reference in New Issue