Вопрос

I'm to ask a question, which answers are solving this task:

Which right-angled triangles can be constructed by choosing three sides out of six segments of length being integers from 1 to 6

So, I'm thinking this is essential:

between(1,6,X),
between(1,6,Y),
between(1,6,Z),

Then we have to make sure it fits Pythagoras statement, so I'm trying this, adding to the above sentence:

(X^2 = Y^2 + Z^2 ; 
Y^2 = X^2 + Z^2 ; 
Z^2 = X^2 + Y^2)

Also I have been trying to replace X^2 with X*X, but it returns false every time. Why is that?

From my understanding, I need it to work like this: Choose three sides from range 1-6, and make sure they fit Pythagoras statement. (Is triangle disparity also required here? I mean X>Y+Z,Y>X+Z,Z>X+Y ?

Это было полезно?

Решение

Check the prolog manual regarding the different comparators, etc. They mean and do various things. =:=/2 is specifically evaluates arithmetic expressions on either side and checks for equality of results. =/2 is not an equality operator; it performs prolog unification. It's important to know the difference. In your example, limiting all results to maximum of 6, then permutations of 3,4,5 are the only positive integer solutions to the right triangle.

?- between(1,6,X), between(1,6,Y), between(1,6,Z), Z^2 =:= X^2 + Y^2.
X = 3,
Y = 4,
Z = 5 ;
X = 4,
Y = 3,
Z = 5 ;
false.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top