Faulhaber's triangle: Difference between revisions

no edit summary
No edit summary
Line 1,973:
 
56056972216555580111030077961944183400198333273050000
</pre>
 
=={{header|Prolog}}==
<lang Prolog>
ft_rows(Lz) :-
lazy_list(ft_row, [], Lz).
 
ft_row([], R1, R1) :- R1 = [1].
ft_row(R0, R2, R2) :-
length(R0, P),
Jmax is 1 + P, numlist(2, Jmax, Qs),
maplist(term(P), Qs, R0, R1),
sum_list(R1, S), Bk is 1 - S, % Bk is Bernoulli number
R2 = [Bk | R1].
 
term(P, Q, R, S) :- S is R * (P rdiv Q).
 
show(N) :-
ft_rows(Rs),
length(Rows, N), prefix(Rows, Rs),
forall(
member(R, Rows),
(format(string(S), "~w", [R]),
re_replace(" rdiv "/g, "/", S, T),
re_replace(","/g, ", ", T, U),
write(U), nl)).
 
sum(N, K, S) :- % sum I=1,N (I ** K)
ft_rows(Rows), drop(K, Rows, [Coefs|_]),
reverse([0|Coefs], Poly),
foldl(horner(N), Poly, 0, S).
 
horner(N, A, S0, S1) :-
S1 is N*S0 + A.
 
drop(N, Lz1, Lz2) :-
append(Pfx, Lz2, Lz1), length(Pfx, N), !.
</lang>
{{Out}}
<pre>
?- show(10).
[1]
[1/2, 1/2]
[1/6, 1/2, 1/3]
[0, 1/4, 1/2, 1/4]
[-1/30, 0, 1/3, 1/2, 1/5]
[0, -1/12, 0, 5/12, 1/2, 1/6]
[1/42, 0, -1/6, 0, 1/2, 1/2, 1/7]
[0, 1/12, 0, -7/24, 0, 7/12, 1/2, 1/8]
[-1/30, 0, 2/9, 0, -7/15, 0, 2/3, 1/2, 1/9]
[0, -3/20, 0, 1/2, 0, -7/10, 0, 3/4, 1/2, 1/10]
true.
 
?- sum(1000, 17, S).
S = 56056972216555580111030077961944183400198333273050000.
</pre>
 
357

edits