I'm starting learning Prolog and I want a program that given a integer P
gives to integers A
and B
such that P = A² + B²
. If there aren't values of A
and B
that satisfy this equation, false
should be returned
For example: if P = 5
, it should give A = 1
and B = 2
(or A = 2
and B = 1
) because 1² + 2² = 5
.
I was thinking this should work:
giveSum(P, A, B) :- integer(A), integer(B), integer(P), P is A*A + B*B.
with the query:
giveSum(5, A, B).
However, it does not. What should I do? I'm very new to Prolog so I'm still making lot of mistakes.
Thanks in advance!