2013-11-29 16:19:13 -08:00
|
|
|
// Ported from the Python version.
|
|
|
|
|
|
|
|
|
|
class Tree {
|
2015-07-21 07:24:53 -07:00
|
|
|
construct new(item, depth) {
|
2013-11-29 16:19:13 -08:00
|
|
|
_item = item
|
|
|
|
|
if (depth > 0) {
|
|
|
|
|
var item2 = item + item
|
|
|
|
|
depth = depth - 1
|
2015-07-10 09:18:22 -07:00
|
|
|
_left = Tree.new(item2 - 1, depth)
|
|
|
|
|
_right = Tree.new(item2, depth)
|
2013-11-29 16:19:13 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
check {
|
|
|
|
|
if (_left == null) {
|
2013-12-07 18:43:39 -08:00
|
|
|
return _item
|
2013-11-29 16:19:13 -08:00
|
|
|
}
|
2013-12-07 18:43:39 -08:00
|
|
|
|
|
|
|
|
return _item + _left.check - _right.check
|
2013-11-29 16:19:13 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var minDepth = 4
|
2013-12-12 16:59:57 -08:00
|
|
|
var maxDepth = 12
|
2013-11-29 16:19:13 -08:00
|
|
|
var stretchDepth = maxDepth + 1
|
|
|
|
|
|
2015-09-15 07:46:09 -07:00
|
|
|
var start = System.clock
|
2013-11-29 16:19:13 -08:00
|
|
|
|
2015-11-11 07:55:48 -08:00
|
|
|
System.print("stretch tree of depth %(stretchDepth) check: " +
|
|
|
|
|
"%(Tree.new(0, stretchDepth).check)")
|
2013-11-29 16:19:13 -08:00
|
|
|
|
2015-07-10 09:18:22 -07:00
|
|
|
var longLivedTree = Tree.new(0, maxDepth)
|
2013-11-29 16:19:13 -08:00
|
|
|
|
|
|
|
|
// iterations = 2 ** maxDepth
|
|
|
|
|
var iterations = 1
|
2013-12-25 15:01:45 -08:00
|
|
|
for (d in 0...maxDepth) {
|
2013-11-29 16:19:13 -08:00
|
|
|
iterations = iterations * 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var depth = minDepth
|
|
|
|
|
while (depth < stretchDepth) {
|
|
|
|
|
var check = 0
|
2014-01-20 13:46:37 -08:00
|
|
|
for (i in 1..iterations) {
|
2015-07-10 09:18:22 -07:00
|
|
|
check = check + Tree.new(i, depth).check + Tree.new(-i, depth).check
|
2013-11-29 16:19:13 -08:00
|
|
|
}
|
|
|
|
|
|
2015-11-11 07:55:48 -08:00
|
|
|
System.print("%(iterations * 2) trees of depth %(depth) check: %(check)")
|
2013-11-29 16:19:13 -08:00
|
|
|
iterations = iterations / 4
|
|
|
|
|
depth = depth + 2
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 07:55:48 -08:00
|
|
|
System.print(
|
|
|
|
|
"long lived tree of depth %(maxDepth) check: %(longLivedTree.check)")
|
|
|
|
|
System.print("elapsed: %(System.clock - start)")
|