Stirling numbers of the first kind: Difference between revisions

Added C++ solution
m (→‎{{header|zkl}}: Fix link: Perl 6 --> Raku)
(Added C++ solution)
Line 125:
Maximum Stirling number of the first kind with n = 100:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
</pre>
 
=={{header|C++}}==
<lang cpp>#include <cstdint>
#include <iomanip>
#include <iostream>
#include <map>
 
class unsigned_stirling1
{
public:
int get(int n, int k);
private:
std::map<std::pair<int, int>, int> cache_;
};
 
int unsigned_stirling1::get(int n, int k)
{
if (k == 0)
return n == 0 ? 1 : 0;
if (k > n)
return 0;
auto p = std::make_pair(n, k);
auto i = cache_.find(p);
if (i != cache_.end())
return i->second;
int s = get(n - 1, k - 1) + (n - 1) * get(n - 1, k);
cache_.emplace(p, s);
return s;
}
 
void print_stirling_numbers(unsigned_stirling1& s1, int n)
{
for (int i = 0; i <= n; ++i)
{
for (int j = 0; j <= i; ++j)
std::cout << std::setw(10) << s1.get(i, j);
std::cout << '\n';
}
}
 
int main()
{
unsigned_stirling1 s1;
std::cout << "Unsigned Stirling numbers of the first kind:\n";
print_stirling_numbers(s1, 12);
return 0;
}</lang>
 
{{out}}
<pre>
Unsigned Stirling numbers of the first kind:
1
0 1
0 1 1
0 2 3 1
0 6 11 6 1
0 24 50 35 10 1
0 120 274 225 85 15 1
0 720 1764 1624 735 175 21 1
0 5040 13068 13132 6769 1960 322 28 1
0 40320 109584 118124 67284 22449 4536 546 36 1
0 362880 1026576 1172700 723680 269325 63273 9450 870 45 1
0 3628800 10628640 12753576 8409500 3416930 902055 157773 18150 1320 55 1
0 39916800 120543840 150917976 105258076 45995730 13339535 2637558 357423 32670 1925 66 1
</pre>
 
1,777

edits