Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog arithmetic syntax

Tags:

prolog

clpfd

How to define a as a integer/float number ?

I want to find the results of a+b+c+d=10 where a,b,c,d is integer and >=0.

like image 773
user198729 Avatar asked Dec 24 '09 05:12

user198729


2 Answers

Here is a simple, modern, pure Prolog, non-CLP-library solution:

range(X):-
        member(X,[0,1,2,3,4,5,6,7,8,9,10]).

ten(A,B,C,D):-
        range(A),
        range(B),
        range(C),
        range(D),
        10 =:= A + B + C + D.
like image 72
ThomasH Avatar answered Sep 26 '22 21:09

ThomasH


with SWI-Prolog you can use CLP(FD) library

1 ?- use_module(library(clpfd)).
%  library(error) compiled into error 0.00 sec, 9,764 bytes
% library(clpfd) compiled into clpfd 0.05 sec, 227,496 bytes
true.

2 ?- Vars=[A,B,C,D],Vars ins 0..10,sum(Vars,#=,10),label(Vars).
Vars = [0, 0, 0, 10],
A = 0,
B = 0,
C = 0,
D = 10 ;
Vars = [0, 0, 1, 9],
A = 0,
B = 0,
C = 1,
D = 9 ;
Vars = [0, 0, 2, 8],
A = 0,
B = 0,
C = 2,
D = 8 ;
Vars = [0, 0, 3, 7],
A = 0,
B = 0,
C = 3,
D = 7 ;
...
like image 30
Volodymyr Gubarkov Avatar answered Sep 22 '22 21:09

Volodymyr Gubarkov