How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Prolog programming language
Table of Contents
Problem Statement
A fast scheme for evaluating a polynomial such as: when is to arrange the computation as follows: And compute the result from the innermost brackets outwards as in this pseudocode: Task Description Cf. Formal power series
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Prolog programming language
Source code in the prolog programming language
horner([], _X, 0).
horner([H|T], X, V) :-
horner(T, X, V1),
V is V1 * X + H.
?- horner([-19, 7, -4, 6], 3, V).
V = 128.
:- use_module(library(lambda)).
% foldr(Pred, Init, List, R).
%
foldr(_Pred, Val, [], Val).
foldr(Pred, Val, [H | T], Res) :-
foldr(Pred, Val, T, Res1),
call(Pred, Res1, H, Res).
f_horner(L, V, R) :-
foldr(\X^Y^Z^(Z is X * V + Y), 0, L, R).
:- module(_, [horner/3], [fsyntax, hiord]).
:- use_module(library(hiordlib)).
horner(L, X) := ~foldr((''(H,V0,V) :- V is V0*X + H), L, 0).
You may also check:How to resolve the algorithm Combinations and permutations step by step in the C++ programming language
You may also check:How to resolve the algorithm Collections step by step in the Gambas programming language
You may also check:How to resolve the algorithm Arrays step by step in the Oforth programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the J programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the AmigaE programming language