Finished of e2, two ruby methods

This commit is contained in:
Dan Ballard 2011-04-02 23:53:00 -07:00
parent 907652a192
commit 2d574d4514
5 changed files with 58 additions and 0 deletions

13
e2/e2.2.rb Executable file
View File

@ -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

7
e2/e2.lisp Normal file
View File

@ -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)))))

11
e2/e2.m
View File

@ -1 +1,12 @@
#include <objc/Object.h>
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);
}

11
e2/e2.py Executable file
View File

@ -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

16
e2/e2.rb Executable file
View File

@ -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)