Stirling numbers of the second kind: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 39:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">[(Int, Int) = BigInt] computed
 
F sterling2(n, k)
Line 77:
E
print("#.\n(#. digits, k = #.)\n".format(previous, String(previous).len, k - 1))
L.break</langsyntaxhighlight>
 
{{out}}
Line 104:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses the LONG LONG INT mode of Algol 68g which allows large precision integers. As the default precision of LONG LONG INT is too small, the precision is specified via a pragmatic comment.
<langsyntaxhighlight lang="algol68">BEGIN
# show some Stirling numbers of the second kind #
 
Line 153:
print( ( whole( max 100, 0 ), newline ) )
END
END</langsyntaxhighlight>
{{out}}
<pre>
Line 177:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % show some Stirling numbers of the second kind %
integer MAX_STIRLING;
MAX_STIRLING := 12;
Line 202:
end for_n
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 224:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 DEFINT N,K: DEFDBL S: DEFSTR F
20 DIM S2(12,12),F(12)
30 FOR N=0 TO 12: READ F(N): NEXT N
Line 239:
140 NEXT N
150 DATA ##,##,#####,######,#######,########
160 DATA ########,#######,#######,######,#####,###,##</langsyntaxhighlight>
{{out}}
<pre> 1
Line 256:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 317:
stirling_cache_destroy(&sc);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 340:
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iomanip>
#include <iostream>
Line 392:
std::cout << max << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 417:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.bigint;
import std.conv;
import std.functional;
Line 471:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Stirling numbers of the second kind:
Line 494:
=={{header|Factor}}==
{{works with|Factor|0.99 development version 2019-07-10}}
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit formatting io kernel math
math.extras prettyprint sequences ;
RENAME: stirling math.extras => (stirling)
Line 514:
 
"Maximum value from the 100 _ stirling row:" print
100 <iota> [ 100 swap stirling ] map supremum .</langsyntaxhighlight>
{{out}}
<pre>
Line 538:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">dim as integer S2(0 to 12, 0 to 12) 'initially set with zeroes
dim as ubyte n, k
dim as string outstr
Line 569:
next k
print outstr
next n</langsyntaxhighlight>
<pre>Stirling numbers of the second kind
 
Line 597:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 649:
fmt.Println(max)
fmt.Printf("which has %d digits.\n", len(max.String()))
}</langsyntaxhighlight>
 
{{out}}
Line 675:
</pre>
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
import Data.List (groupBy)
import qualified Data.MemoCombinators as Memo
Line 700:
where
table :: [[(Int, Int)]]
table = groupBy (\a b -> fst a == fst b) $ (,) <$> [0..12] <*> [0..12]</langsyntaxhighlight>
{{out}}
<pre>n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
Line 759:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
import java.util.HashMap;
Line 820:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 853:
is available in the second part.
 
<langsyntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# Input: {computed} (the cache)
Line 901:
| .emit,
part2(100)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 926:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
 
const s2cache = Dict()
Line 964:
println("\nThe maximum for stirling2(100, _) is: ", maximum(k-> stirlings2(BigInt(100), BigInt(k)), 1:100))
 
</langsyntaxhighlight>{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9 10 11 12
Line 987:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.math.BigInteger
 
fun main() {
Line 1,039:
COMPUTED[key] = result
return result
}</langsyntaxhighlight>
{{out}}
<pre>Stirling numbers of the second kind:
Line 1,061:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">TableForm[Array[StirlingS2, {n = 12, k = 12} + 1, {0, 0}], TableHeadings -> {"n=" <> ToString[#] & /@ Range[0, n], "k=" <> ToString[#] & /@ Range[0, k]}]
Max[Abs[StirlingS2[100, #]] & /@ Range[0, 100]]</langsyntaxhighlight>
{{out}}
<pre> k=0 k=1 k=2 k=3 k=4 k=5 k=6 k=7 k=8 k=9 k=10 k=11 k=12
Line 1,082:
=={{header|Nim}}==
As for Stirling numbers of first kind, a simple program using recursive definition is enough to solve the task when not using big numbers.
<langsyntaxhighlight Nimlang="nim">import sequtils, strutils
 
proc s2(n, k: Natural): Natural =
Line 1,095:
for k in 0..n:
stdout.write ($s2(n, k)).align(8)
stdout.write '\n'</langsyntaxhighlight>
 
{{out}}
Line 1,116:
Now, to solve the second part of the task, we have to use big numbers. As for Stirling numbers of first kind, we also use a cache to avoid to repeat multiple times the same computations.
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import tables
import bignum
 
Line 1,135:
 
echo "Maximum Stirling number of the second kind with n = 100:"
echo max</langsyntaxhighlight>
 
{{out}}
Line 1,142:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use bigint;
Line 1,173:
 
say "\nMaximum value from the S2(100, *) row:";
say max map { Stirling2(101,$_) } 0..100;</langsyntaxhighlight>
{{out}}
<pre>Unsigned Stirling2 numbers of the second kind: S2(n, k):
Line 1,197:
{{libheader|Phix/mpfr}}
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,236:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nThe maximum S2(100,k): %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m100</span><span style="color: #0000FF;">)))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,261:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">:- dynamic stirling2_cache/3.
 
stirling2(N, N, 1):-!.
Line 1,302:
writeln('Maximum value of S2(n,k) where n = 100:'),
max_stirling2(100, M),
writeln(M).</langsyntaxhighlight>
 
{{out}}
Line 1,325:
=={{header|Python}}==
{{trans|Java}}
<syntaxhighlight lang="python">
<lang Python>
computed = {}
 
Line 1,365:
print("{0}\n({1} digits, k = {2})\n".format(previous, len(str(previous)), k - 1))
break
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,390:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ dip number$
over size -
space swap of
Line 1,421:
0 100 times
[ 100 i^ 1+ s2 max ]
echo cr</langsyntaxhighlight>
 
{{out}}
Line 1,446:
{{works with|Rakudo|2019.07.1}}
 
<syntaxhighlight lang="raku" perl6line>sub Stirling2 (Int \n, Int \k) {
((1,), { (0, |@^last) »+« (|(@^last »*« @^last.keys), 0) } … *)[n;k]
}
Line 1,463:
 
say "\nMaximum value from the S2(100, *) row:";
say (^100).map( { Stirling2 100, $_ } ).max;</langsyntaxhighlight>
{{out}}
<pre>Stirling numbers of the second kind: S2(n, k):
Line 1,486:
=={{header|REXX}}==
Some extra code was added to minimize the displaying of the column widths.
<syntaxhighlight lang="text">/*REXX program to compute and display Stirling numbers of the second kind. */
parse arg lim . /*obtain optional argument from the CL.*/
if lim=='' | lim=="," then lim= 12 /*Not specified? Then use the default.*/
Line 1,523:
end /*c*/
say center(r, wgi) strip( substr($,2), 'T') /*display a single row of the grid. */
end /*r*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,549:
=={{header|Ruby}}==
 
<langsyntaxhighlight lang="ruby">@memo = {}
 
def sterling2(n, k)
Line 1,573:
puts "\nMaximum value from the sterling2(100, k)";
puts (1..100).map{|a| sterling2(100,a)}.max
</syntaxhighlight>
</lang>
{{out}}<pre>Sterling2 numbers:
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
Line 1,595:
</pre>
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func S2(n, k) { # Stirling numbers of the second kind
stirling2(n, k)
}
Line 1,615:
say "\nMaximum value from the S2(#{n}, *) row:"
say { S2(n, _) }.map(^n).max
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,638:
 
Alternatively, the '''S2(n,k)''' function can be defined as:
<langsyntaxhighlight lang="ruby">func S2((0), (0)) { 1 }
func S2(_, (0)) { 0 }
func S2((0), _) { 0 }
func S2(n, k) is cached { S2(n-1, k)*k + S2(n-1, k-1) }</langsyntaxhighlight>
 
=={{header|Tcl}}==
 
{{trans|Java}}
<langsyntaxhighlight Tcllang="tcl">proc S2 {n k} {
set nk [list $n $k]
if {[info exists ::S2cache($nk)]} {
Line 1,695:
}
}
main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,720:
=={{header|Visual Basic .NET}}==
{{trans|Java}}
<langsyntaxhighlight lang="vbnet">Imports System.Numerics
 
Module Module1
Line 1,789:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Stirling numbers of the second kind:
Line 1,814:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/big" for BigInt
import "/fmt" for Fmt
 
Line 1,852:
break
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,877:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn stirling2(n,k){
var seen=Dictionary(); // cache for recursion
if(n==k) return(1); // (0.0)==1
Line 1,885:
if(Void==(s2 := seen.find(z2))){ s2 = seen[z2] = stirling2(n-1,k-1) }
k*s1 + s2; // k is first to cast to BigInt (if using BigInts)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">// calculate entire table (cached), find max, find num digits in max
N,mx := 12, [1..N].apply(fcn(n){ [1..n].apply(stirling2.fp(n)) }).flatten() : (0).max(_);
fmt:="%%%dd".fmt("%d".fmt(mx.numDigits + 1)).fmt; // "%9d".fmt
Line 1,893:
foreach row in ([0..N]){
println("%3d".fmt(row), [0..row].pump(String, stirling2.fp(row), fmt));
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
Line 1,913:
</pre>
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
N=100;
S2100:=[BI(2)..N].apply(stirling2.fp(BI(N))).reduce(fcn(m,n){ m.max(n) });
println("Maximum value from the S2(%d,*) row (%d digits):".fmt(N,S2100.numDigits));
println(S2100);</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
10,333

edits