From 2d574d4514bc5ba81798a09046953482ac67ff28 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Sat, 2 Apr 2011 23:53:00 -0700 Subject: [PATCH] Finished of e2, two ruby methods --- e2/e2.2.rb | 13 +++++++++++++ e2/e2.lisp | 7 +++++++ e2/e2.m | 11 +++++++++++ e2/e2.py | 11 +++++++++++ e2/e2.rb | 16 ++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100755 e2/e2.2.rb create mode 100644 e2/e2.lisp create mode 100755 e2/e2.py create mode 100755 e2/e2.rb diff --git a/e2/e2.2.rb b/e2/e2.2.rb new file mode 100755 index 0000000..60fa3d5 --- /dev/null +++ b/e2/e2.2.rb @@ -0,0 +1,13 @@ +#!/usr/bin/ruby + +last = sum = 0 +current = 1 +while current < 4000000 + if current % 2 == 0 + sum += current + end + tmp = current + current += last + last = tmp +end +puts sum diff --git a/e2/e2.lisp b/e2/e2.lisp new file mode 100644 index 0000000..bea2eb0 --- /dev/null +++ b/e2/e2.lisp @@ -0,0 +1,7 @@ +(defun e2 () + (let ((sum 0)) + (do ((last 0 current) + (current 1 (+ current last))) + ( (>= current 4000000) sum) + (if (= (rem current 2) 0) + (incf sum current))))) \ No newline at end of file diff --git a/e2/e2.m b/e2/e2.m index 5b43f10..a10af63 100644 --- a/e2/e2.m +++ b/e2/e2.m @@ -1 +1,12 @@ #include + +int main(int argc, char ** argv) { + int i, last, current, sum, tmp; + sum = 0; + last = 0; + for(current = 1; current < 4000000; tmp = current, current = last + current, last = tmp) { + if ( current % 2 == 0) + sum += current; + } + printf("%d\n", sum); +} diff --git a/e2/e2.py b/e2/e2.py new file mode 100755 index 0000000..f9b7cfa --- /dev/null +++ b/e2/e2.py @@ -0,0 +1,11 @@ +#!/usr/bin/python + +last = sum = 0 +current = 1 +while (current < 4000000): + if current % 2 == 0: + sum += current + tmp = current + current += last + last = tmp +print sum diff --git a/e2/e2.rb b/e2/e2.rb new file mode 100755 index 0000000..104846a --- /dev/null +++ b/e2/e2.rb @@ -0,0 +1,16 @@ +#!/usr/bin/ruby + +def sum(last, current, sum) + if current >= 4000000 + return sum + end + if current % 2 == 0 + sum += current + end + tmp = current + current += last + last = tmp + sum(last, current, sum) +end + +puts sum(0, 1, 0)