1 # The Computer Language Shootout Benchmarks
\r
2 # http://shootout.alioth.debian.org
\r
4 # contributed by Jesse Millikan
\r
7 def STDOUT.write_ *args
\r
10 def item_check(tree)
\r
14 tree[1] + item_check(tree[0]) - item_check(tree[2])
\r
18 def bottom_up_tree(item, depth)
\r
20 item_item = 2 * item
\r
22 [bottom_up_tree(item_item - 1, depth), item, bottom_up_tree(item_item, depth)]
\r
28 max_depth = 12 # 16 # ARGV[0].to_i
\r
31 max_depth = min_depth + 2 if min_depth + 2 > max_depth
\r
33 stretch_depth = max_depth + 1
\r
34 stretch_tree = bottom_up_tree(0, stretch_depth)
\r
36 puts "stretch tree of depth #{stretch_depth}\t check: #{item_check(stretch_tree)}"
\r
39 long_lived_tree = bottom_up_tree(0, max_depth)
\r
41 min_depth.step(max_depth + 1, 2) do |depth|
\r
42 iterations = 2**(max_depth - depth + min_depth)
\r
46 for i in 1..iterations
\r
47 temp_tree = bottom_up_tree(i, depth)
\r
48 check += item_check(temp_tree)
\r
50 temp_tree = bottom_up_tree(-i, depth)
\r
51 check += item_check(temp_tree)
\r
54 puts "#{iterations * 2}\t trees of depth #{depth}\t check: #{check}"
\r
57 puts "long lived tree of depth #{max_depth}\t check: #{item_check(long_lived_tree)}"
\r