Stirling numbers of the second kind: Difference between revisions

Added Prolog Solution
m (Fixed output)
(Added Prolog Solution)
Line 582:
 
Maximum value from the S2(100, *) row: 7769730053598745155...3545178960761551674 (115 digits)
</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<lang prolog>:- dynamic stirling2_cache/3.
 
stirling2(N, N, 1):-!.
stirling2(_, 0, 0):-!.
stirling2(N, K, 0):-
K > N,
!.
stirling2(N, K, L):-
stirling2_cache(N, K, L),
!.
stirling2(N, K, L):-
N1 is N - 1,
K1 is K - 1,
stirling2(N1, K, L1),
stirling2(N1, K1, L2),
!,
L is K * L1 + L2,
assertz(stirling2_cache(N, K, L)).
 
print_stirling_numbers(N):-
between(1, N, K),
stirling2(N, K, L),
writef('%8r', [L]),
fail.
print_stirling_numbers(_):-
nl.
 
print_stirling_numbers_up_to(M):-
between(1, M, N),
print_stirling_numbers(N),
fail.
print_stirling_numbers_up_to(_).
 
max_stirling2(N, M):-
findall(L, (between(1, N, K), stirling2(N, K, L)), List),
max_list(List, M).
 
main:-
writeln('Stirling numbers of the second kind up to S2(12,12):'),
print_stirling_numbers_up_to(12),
writeln('Maximum value of S2(n,k) where n = 100:'),
max_stirling2(100, M),
writeln(M).</lang>
 
{{out}}
<pre>
Stirling numbers of the second kind up to S2(12,12):
1
1 1
1 3 1
1 7 6 1
1 15 25 10 1
1 31 90 65 15 1
1 63 301 350 140 21 1
1 127 966 1701 1050 266 28 1
1 255 3025 7770 6951 2646 462 36 1
1 511 9330 34105 42525 22827 5880 750 45 1
1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
Maximum value of S2(n,k) where n = 100:
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
</pre>
 
1,777

edits