Stirling numbers of the first kind: Difference between revisions

added Tcl
(added Tcl)
Line 942:
func S1((0), _) { 0 }
func S1(n, k) is cached { S1(n-1, k-1) + (n-1)*S1(n-1, k) }</lang>
 
=={{header|Tcl}}==
This computes the unsigned Stirling numbers of the first kind.
Inspired by [[Stirling_numbers_of_the_second_kind#Tcl]], hence similar to [[#Java]].
<lang Tcl>proc US1 {n k} {
if {$k == 0} {
return [expr {$n == 0}]
}
if {$n < $k} {
return 0
}
if {$n == $k} {
return 1
}
 
set nk [list $n $k]
if {[info exists ::US1cache($nk)]} {
return $::US1cache($nk)
}
set n1 [expr {$n - 1}]
set k1 [expr {$k - 1}]
set r [expr {($n1 * [US1 $n1 $k]) + [US1 $n1 $k1]}]
 
set ::US1cache($nk) $r
}
 
proc main {} {
puts "Unsigned Stirling numbers of the first kind:"
set max 12 ;# last table line to print
set L 9 ;# space to use for 1 number
puts -nonewline "n\\k"
for {set n 0} {$n <= $max} {incr n} {
puts -nonewline " [format %${L}d $n]"
}
puts ""
for {set n 0} {$n <= $max} {incr n} {
puts -nonewline [format %-3d $n]
for {set k 0} {$k <= $n} {incr k} {
puts -nonewline " [format %${L}s [US1 $n $k]]"
}
puts ""
}
puts "The maximum value of US1(100, k) = "
set previous 0
for {set k 1} {$k <= 100} {incr k} {
set current [US1 100 $k]
if {$current > $previous} {
set previous $current
} else {
puts $previous
puts "([string length $previous] digits, k = [expr {$k-1}])"
break
}
}
}
main</lang>
 
{{out}}
<pre>Unsigned Stirling numbers of the first kind:
n\k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 1 1
3 0 2 3 1
4 0 6 11 6 1
5 0 24 50 35 10 1
6 0 120 274 225 85 15 1
7 0 720 1764 1624 735 175 21 1
8 0 5040 13068 13132 6769 1960 322 28 1
9 0 40320 109584 118124 67284 22449 4536 546 36 1
10 0 362880 1026576 1172700 723680 269325 63273 9450 870 45 1
11 0 3628800 10628640 12753576 8409500 3416930 902055 157773 18150 1320 55 1
12 0 39916800 120543840 150917976 105258076 45995730 13339535 2637558 357423 32670 1925 66 1
The maximum value of US1(100, k) =
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)</pre>
 
=={{header|zkl}}==
73

edits