How to resolve the algorithm Faulhaber's triangle step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Faulhaber's triangle step by step in the Prolog programming language
Table of Contents
Problem Statement
Named after Johann Faulhaber, the rows of Faulhaber's triangle are the coefficients of polynomials that represent sums of integer powers, which are extracted from Faulhaber's formula:
where
B
n
{\displaystyle B_{n}}
is the nth-Bernoulli number.
The first 5 rows of Faulhaber's triangle, are:
Using the third row of the triangle, we have:
∑
k
1
n
k
2
=
1 6
n +
1 2
n
2
1 3
n
3
{\displaystyle \sum _{k=1}^{n}k^{2}={1 \over 6}n+{1 \over 2}n^{2}+{1 \over 3}n^{3}}
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Faulhaber's triangle step by step in the Prolog programming language
Source code in the prolog programming language
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), !.
You may also check:How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the Maple programming language
You may also check:How to resolve the algorithm Department numbers step by step in the C# programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Pict programming language
You may also check:How to resolve the algorithm Binary search step by step in the Delphi programming language
You may also check:How to resolve the algorithm FTP step by step in the Wren programming language