Lah numbers: Difference between revisions
Content deleted Content added
added Tcl |
|||
Line 1,195: | Line 1,195: | ||
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1 |
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1 |
||
Maximum value from the L(100, *) row: 44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre> |
Maximum value from the L(100, *) row: 44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre> |
||
=={{header|Tcl}}== |
|||
<lang Tcl>proc prod {from to} { |
|||
set r 1 |
|||
if {$from <= $to} { |
|||
set r $from |
|||
while {[incr from] <= $to} { |
|||
set r [expr {$r * $from}] |
|||
} |
|||
} |
|||
return $r |
|||
} |
|||
proc US3 {n k} { |
|||
if {$n < 0 || $k < 0} { |
|||
error "US3(): negative arg ($n,$k)" |
|||
} |
|||
## L(0,0) = 1 |
|||
## L(n,0) = 0 if 0 < n |
|||
## L(0,k) = 0 if 0 < k |
|||
## L(n,k) = 0 if n < k |
|||
## L(n,n) = 1 |
|||
if {$n == $k} { |
|||
return 1 |
|||
} |
|||
if {$n == 0 || $k == 0} { |
|||
return 0 |
|||
} |
|||
if {$n < $k} { |
|||
return 0 |
|||
} |
|||
set nk [list $n $k] |
|||
if {[info exists ::US3cache($nk)]} { |
|||
return $::US3cache($nk) |
|||
} |
|||
if {$k == 1} { |
|||
## L(n,1) = n! |
|||
set r [prod 2 $n] |
|||
} else { |
|||
## k > 1 |
|||
## L(n,k) = L(n,k-1) * (n - (k-1)) / ((k-1)*k) |
|||
set k1 [expr {$k - 1}] |
|||
set r [expr {([US3 $n $k1] * ($n - $k1)) / ($k * $k1)}] |
|||
} |
|||
set ::US3cache($nk) $r |
|||
} |
|||
proc main {} { |
|||
puts "Unsigned Lah numbers L(n,k):" |
|||
set max 12 ;# last n,k to print |
|||
set L 10 ;# space to use for 1 number |
|||
set minn 1 ;# first row to print |
|||
set mink 1 ;# first column to print |
|||
puts -nonewline "n\\k" |
|||
for {set n $minn} {$n <= $max} {incr n} { |
|||
puts -nonewline " [format %${L}d $n]" |
|||
} |
|||
puts "" |
|||
for {set n $minn} {$n <= $max} {incr n} { |
|||
puts -nonewline [format %3d $n] |
|||
for {set k $mink} {$k <= $n} {incr k} { |
|||
puts -nonewline " [format %${L}s [US3 $n $k]]" |
|||
} |
|||
puts "" |
|||
} |
|||
set n 100 |
|||
puts "The maximum value of L($n, k) = " |
|||
set maxv 0 |
|||
set maxk -1 |
|||
for {set k 0} {$k <= $n} {incr k} { |
|||
set v [US3 $n $k] |
|||
if {$v > $maxv} { |
|||
set maxv $v |
|||
set maxk $k |
|||
} |
|||
} |
|||
puts $maxv |
|||
puts "([string length $maxv] digits, k=$maxk)" |
|||
} |
|||
main </lang> |
|||
{{out}} |
|||
<pre style="font-size:83%">Unsigned Lah numbers L(n,k): |
|||
n\k 1 2 3 4 5 6 7 8 9 10 11 12 |
|||
1 1 |
|||
2 2 1 |
|||
3 6 6 1 |
|||
4 24 36 12 1 |
|||
5 120 240 120 20 1 |
|||
6 720 1800 1200 300 30 1 |
|||
7 5040 15120 12600 4200 630 42 1 |
|||
8 40320 141120 141120 58800 11760 1176 56 1 |
|||
9 362880 1451520 1693440 846720 211680 28224 2016 72 1 |
|||
10 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1 |
|||
11 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1 |
|||
12 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1 |
|||
The maximum value of L(100, k) = |
|||
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000 |
|||
(164 digits, k=10) |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |