Matt Sich
Matt Sich

Reputation: 4105

Get list of two numbers that add up to a number in prolog

for example, getNumbers(M,C,10) would give M=10, C = 0 && M=9, C=1 && M=8, C=2... etc etc

Upvotes: 0

Views: 187

Answers (1)

gusbro
gusbro

Reputation: 22585

Using between as you suggested:

getNumbers(M, C, S):-
  between(0, S, M),
  C is S - M.

and to get the full list you would use findall/3, e.g. this query:

?- findall([M,C], getNumbers(M, C, 5), Numbers).
Numbers = [[0, 5], [1, 4], [2, 3], [3, 2], [4, 1], [5, 0]].

Upvotes: 2

Related Questions