Posted to tcl by kbk at Tue Aug 21 01:11:44 GMT 2012view pretty

namespace path ::tcl::mathop

interp recursionlimit {} 120000

proc fib {n {i 0} {this 0} {next 1}} {
    if {$i >= $n} {
	return $this
    } else {
	tailcall fib $n [+ $i 1] $next [+ $this $next]
    }
}

puts [time {set result [fib 40000]}]
#puts "fib(40000) == $result"

proc fib {n} {
    variable memo
    if {[info exists memo($n)]} {
	return $memo($n)
    } elseif {$n == 0} {
	return 0
    } elseif {$n == 1} {
	return 1
    } else {
	return [set memo($n) [+ [fib [- $n 2]] [fib [- $n 1]]]]
    }
}

puts [time {set result [fib 40000]}]
#puts "fib(40000) == $result"

exit