Write a script to display top 10 Bell Numbers. Please refer to wikipedia page for more informations.
proc bellNumber {n} {
array set bell {}
set bell(0,0) 1
for {set i 1} {$i <= $n} {incr i} {
set decri [expr $i -1]
set bell($i,0) $bell($decri,$decri)
for {set j 1} {$j <= $i} {incr j} {
set decrj [expr $j -1]
set bell($i,$j) [expr $bell($decri,$decrj) + $bell($i,$decrj)]
}
}
return $bell($n,0)
}
for {set i 1} {$i <= 10} {incr i} {
puts "n=$i, Bell Number=[bellNumber $i]"
}
沒有留言:
張貼留言