Stirling numbers of the first kind: Difference between revisions

Added Prolog Solution
(C++ solution uses GMP for large integer support)
(Added Prolog Solution)
Line 612:
 
The maximum S1(100,k): 1971090874705526110...6000000000000000000 (158 digits)
</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<lang prolog>:- dynamic stirling1_cache/3.
 
stirling1(N, N, 1):-!.
stirling1(_, 0, 0):-!.
stirling1(N, K, 0):-
K > N,
!.
stirling1(N, K, L):-
stirling1_cache(N, K, L),
!.
stirling1(N, K, L):-
N1 is N - 1,
K1 is K - 1,
stirling1(N1, K, L1),
stirling1(N1, K1, L2),
!,
L is L2 + (N - 1) * L1,
assertz(stirling1_cache(N, K, L)).
 
print_stirling_numbers(N):-
between(1, N, K),
stirling1(N, K, L),
writef('%10r', [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_stirling1(N, M):-
findall(L, (between(1, N, K), stirling1(N, K, L)), List),
max_list(List, M).
 
main:-
writeln('Unsigned Stirling numbers of the first kind up to S1(12,12):'),
print_stirling_numbers_up_to(12),
writeln('Maximum value of S1(n,k) where n = 100:'),
max_stirling1(100, M),
writeln(M).</lang>
 
{{out}}
<pre>
Unsigned Stirling numbers of the first kind up to S1(12,12):
1
1 1
2 3 1
6 11 6 1
24 50 35 10 1
120 274 225 85 15 1
720 1764 1624 735 175 21 1
5040 13068 13132 6769 1960 322 28 1
40320 109584 118124 67284 22449 4536 546 36 1
362880 1026576 1172700 723680 269325 63273 9450 870 45 1
3628800 10628640 12753576 8409500 3416930 902055 157773 18150 1320 55 1
39916800 120543840 150917976 105258076 45995730 13339535 2637558 357423 32670 1925 66 1
Maximum value of S1(n,k) where n = 100:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
</pre>
 
1,777

edits