I have a question regarding the variable creating inside the program. Lets say i have a function(?- not sure about the wording here), which is /2, is there a way of changing it to /1?
:- use_module(library(clpfd)).
solve(N, L):-
L = [A,B,C,D],
L ins 1..sup,
N #= A * B * C * D,
all_distinct(L),
A #< B, B #< C, C #< D,
labeling([],L),nl,
Z #= A+B+C+D,
write(A+B+C+D=Z).
Is there a possibility of actually making it solve(N), where L is created on the run. Tried using both,
L is [A,B,C,D],
or
L = [],
L is [A,B,C,D]
but no luck right now.
Program is working like that:->
?- solve(30,Elements).
1+2+3+5=11
Elements = [1, 2, 3, 5].
?- solve(60, Elements).
1+2+3+10=16
Elements = [1, 2, 3, 10] ;
1+2+5+6=14
Elements = [1, 2, 5, 6] ;
1+3+4+5=13
Elements = [1, 3, 4, 5] ;
false.
Yes but then you would get for example:
?- solve(30).
1+2+3+5=11
true.
?- solve(60).
1+2+3+10=16
true ;
1+2+5+6=14
true ;
1+3+4+5=13
true ;
false.
If this is what you are looking for just write solve(N)
instead solve(N, L)
:
:- use_module(library(clpfd)).
solve(N):-
L = [A,B,C,D],
L ins 1..sup,
N #= A * B * C * D,
all_distinct(L),
A #< B, B #< C, C #< D,
labeling([],L),nl,
Z #= A+B+C+D,
write(A+B+C+D=Z).
Keep in mind that L is [A,B,C,D]
is not valid because is/1
is used for arithmetic expressions not unification. You need to use =/2
which unifies L with a four element list.
If you want to print the list you could write:
:- use_module(library(clpfd)).
solve(N):-
L = [A,B,C,D],
L ins 1..sup,
N #= A * B * C * D,
all_distinct(L),
A #< B, B #< C, C #< D,
labeling([],L),nl,
Z #= A+B+C+D,
writeln(A+B+C+D=Z),
write("Elements="),
write(L).
Example:
?- solve(30).
1+2+3+5=11
Elements=[1,2,3,5]
true.