How to get the LCM of a list in Prolog? Lets say the list is: [1,2,3,4,5] and the LCM will be 60. I have the following code fo the GCD and the LCM that work for 2 numbers but dont know how to apply to lists.
gcd(X, 0, X) :- !.
gcd(X, Y, Z) :-
H is X rem Y,
gcd(Y, H, Z).
lcm(X,Y,LCM):-
gcd(X,Y,GCD),
LCM is X*Y//GCD.
BTW, an interesting result is that you cannot use
gcd_of_listto computelcm_of_listdirectly, for example:See https://math.stackexchange.com/a/319310/430364
Therefore, we must compute
lcm_of_listfrom scratch.