RosettaCodeData/Task/Factorial/Tcl/factorial-4.tcl
Ingy döt Net db842d013d A-M baby
2013-04-10 21:29:02 -07:00

17 lines
411 B
Tcl

proc ifact_caching n {
global fact_cache
if { ! [info exists fact_cache]} {
set fact_cache {1 1}
}
if {$n < [llength $fact_cache]} {
return [lindex $fact_cache $n]
}
set i [expr {[llength $fact_cache] - 1}]
set sum [lindex $fact_cache $i]
while {$i < $n} {
incr i
set sum [expr {$sum * $i}]
lappend fact_cache $sum
}
return $sum
}