Project

General

Profile

Backport #1063 ยป test.rb

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

 
1
a = Time.now
2
k = []
3

    
4
class Array
5
  def inject(n)
6
     each { |value| n = yield(n, value) }
7
     n
8
  end
9
  def sum
10
    inject(0) { |n, value| n + value }
11
  end
12
  def product
13
    inject(1) { |n, value| n * value }
14
  end
15
end
16

    
17
def fibUpTo(max)
18
  i1, i2 = 1, 1        # parallel assignment
19
  while i1 <= max
20
    yield i1
21
    i1, i2 = i2, i1+i2
22
  end
23
end
24

    
25
fibUpTo(100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) { |f| k.push(f) }
26
puts "k is: "
27
puts k
28
s = k.sum
29
p = k.product
30
puts "s is: "
31
puts s
32

    
33
puts "p is: "
34
puts p
35

    
36
b = Time.now
37

    
38
c = b-a
39
puts c
40
puts "it took: " + c.to_s + " seconds to complete"