Project

General

Profile

Backport #1063 ยป test.rb

konung (Nick Gorbikoff), 01/28/2009 02:47 AM

 
a = Time.now
k = []

class Array
def inject(n)
each { |value| n = yield(n, value) }
n
end
def sum
inject(0) { |n, value| n + value }
end
def product
inject(1) { |n, value| n * value }
end
end

def fibUpTo(max)
i1, i2 = 1, 1 # parallel assignment
while i1 <= max
yield i1
i1, i2 = i2, i1+i2
end
end

fibUpTo(100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) { |f| k.push(f) }
puts "k is: "
puts k
s = k.sum
p = k.product
puts "s is: "
puts s

puts "p is: "
puts p

b = Time.now

c = b-a
puts c
puts "it took: " + c.to_s + " seconds to complete"
    (1-1/1)