I need to write a program, which calculates product of product in range:
I written the following code:
mult(N,N,R,R).
mult(N,Nt,R,Rt):-N1=Nt+1,R1=Rt*(1/(log(Nt))),mult(N,N1,R,R1).
This should implement basic product from Nt
to N
of 1/ln(j)
. As far as I understand it's got to be stopped when Nt and N are equal. However, I can't get it working due to:
?- mult(10,2,R,1), write(R).
ERROR: Out of global stack
The following error. Is there any other way to implement loop not using default libraries of SWI-Prolog?
Out of global stack means you entered a too-long chain of recursion, possibly an infinite one.
The problem stems from using
=
instead ofis
in your assignments.You might want to insert a cut in your first clause to avoid going on after getting an answer.
If you have a graphical debugger (like the one in SWI) try setting 'trace' and 'debug' on and running. You'll soon realize that performing
N1 = Nt+1
givingNt
as 2 yields the term2+1
. Since2+1+1+1+(...)
will never unify with with 10, that's the problem right there.