Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/Catalan-numbers/Tcl/catalan-numbers-1.tcl
Normal file
19
Task/Catalan-numbers/Tcl/catalan-numbers-1.tcl
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package require Tcl 8.5
|
||||
|
||||
# Memoization wrapper
|
||||
proc memoize {function value generator} {
|
||||
variable memoize
|
||||
set key $function,$value
|
||||
if {![info exists memoize($key)]} {
|
||||
set memoize($key) [uplevel 1 $generator]
|
||||
}
|
||||
return $memoize($key)
|
||||
}
|
||||
|
||||
# The simplest recursive definition
|
||||
proc tcl::mathfunc::catalan n {
|
||||
if {[incr n 0] < 0} {error "must not be negative"}
|
||||
memoize catalan $n {expr {
|
||||
$n == 0 ? 1 : 2 * (2*$n - 1) * catalan($n - 1) / ($n + 1)
|
||||
}}
|
||||
}
|
||||
3
Task/Catalan-numbers/Tcl/catalan-numbers-2.tcl
Normal file
3
Task/Catalan-numbers/Tcl/catalan-numbers-2.tcl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
for {set i 0} {$i < 15} {incr i} {
|
||||
puts "C_$i = [expr {catalan($i)}]"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue