Right-angled triangle prolog construction - prolog

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.

Related

The way of thinking in multiply 2 natural numbers (problem solving”)

Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants c ≥ 2.
function multiply(y,z) comment Return the product yz.
1. if z = 0 then return(0) else
2. return(multiply(cy, z/c) + y · (z mod c))
I saw this algorithm in “Algorithm Design Manual”.
I know why it works correctly, but I want to know how this algorithm came to be. Is that a good way to think of multiply two natural number with a constant c?
(multiply(cy, z/c) + y · (z mod c))
When c is the base of your representation (like decimal), then this is how multiplication can be done "manually". It's the "shift and add" method.
In c-base cy is a single shift of y to the left (i.e. adding a zero at the right); and z/c is a single shift of z to the right: the right most digit is lost.
That lost digit is actually z mod c, which is multiplied with y separately.
Here is an example with c = 10, where the apostrophe signifies the value of variables in a recursive call.
We perform the multiplication with y for each separate digit of z (retrieved with z mod c). Each next product found in this way is written shifted one more place to the left. Usually the 0 is not padded at the right of this shifted product, but it is silently assumed:
354 y
x 29 z
----
3186 y(z mod c) = 354·9 = 3186
+ 708 y'(z' mod c) = yc(z/c mod c) = 3540·2 = 7080
------
10266
So the algorithm just relies on the mathematical basis for this "shift and add" method in a given c-base.

Ignore parts of an equation with multiple variable in Mathematica

I want to use the linear version of a somewhat big equation which is outputted by my Mathematica code - For simplicity I will here use the example equation:
Test = 3 x + x y + 8 y
Now, I want to use only the first order term, so that for x that will be 3 and for y that will be 8. I have tried to get those using
Coefficient[Test, x]
Coefficient[Test, y]
However these give me for x:
3+y
and for y:
8+x
Is there anyway I can use Mathematica to ignore the terms that depend in both xy simultaneously?

How to Implement a Trig Identity Proving Algorithm

How could I implement a program that takes in the two sides of a trig equation (could be generalized to anything but for now I'll leave it at just trig identities) and the program will output the steps to transform one side into another (or transform them both) to show that they are in fact equal. The program will assume that they are equal in the first place. I am quite stumped as to how I might implement an algorithm to do this. My first thought was something to do with graphs, but I couldn't think of anything beyond this. From there, I thought that I should first parse both sides of the equation into trees. For example (cot x * sin) / (sin x + cos x) would look like this:
division
/ \
* +
/ \ / \
cot sin sin cos
After this, I had two similar ideas, both of which have problems. The first idea was to pick the side with the least number of leaves and try to manipulate it into the other side by using equivalencies that would be represented by "tree regexs." Examples of these "tree regexs" would be csc = 1 / sin or cot = cos / sin (in tree form of course), etc. My second idea would be to pick the side with more leaves and try to find some expression that when multiplied by that expression would equal the other side. Using reciprocals this wouldn't be too bad, however, I would then have to prove that the thing I multiplied by equals 1. Again I am back to this "tree regex" thing.
The major flaw with both of these is in what order/how could I apply these substitutions. Will it just have to be a big mess of if statements or is there a more elegant solution? Is there actually a graph-based solution that I'm not seeing. What (if any) might be a good algorithm to prove trig identities.
To be clear I am not talking about the "solve for x" type problem such as tan(x)sin(x) = 5, find all values of x but rather prove that sqrt((1 + sin x) / (1 - sin x)) = sec x + tan x
This is a simple algorithm for deciding trigonometric identities that can be brought into the form polynomial(sin x, cos x) = 0 :
Get rid of tan x, cot x, sec x, ..., sin 2x, ... by the obvious substitutions (tan x -> (sin x)/(cos x), ..., sin 2x -> 2 (sin x) (cos x), ...)
Transform identity to polynomial by squaring (isolated) roots (getting rid of multiple roots in an identity can be tricky, though), multiplying with denominators and bringing all expanded terms to one side
Replace all terms cos^2 x in the polynomial (cos^3 x = (cos^2 x)(cos x), cos^4 x = (cos^2 x)(cos^2 x), ...) by 1 - sin^2 x and expand the polynomial.
Finally a polynomial without cos^2 x is computed. If it is identical to 0 the identity is proven, otherwise the identity does not hold.
Your example sqrt((1 + sin x)/(1 - sin x)) = sec x + tan x:
Using the substitutions sec x -> 1/(cos x) and tan x -> (sin x)/(cos x) we get
sqrt((1 + sin x)/(1 - sin x)) = 1/(cos x) + (sin x)/(cos x).
For brevity let us write s instead of sin x and c instead of cos x, which gives us:
sqrt((1 + s)/(1 - s)) = 1/c + s/c
Squaring the equation and multiplying both sides with (1 - s)c^2 we get
(1 + s)c^2 = (1 + s)^2(1 - s).
Expanding the parenthesis and bringing everthing to one side we get
c^2 - sc^2 + s^3 + s^2 - s - 1 = 0
Substituting c^2 = 1 - s^2 into the polynomial we get
(1 - s^2) - s(1 - s^2) + s^3 + s^2 - s - 1 which expands to 0.
Hence the identity is proven.
Look out for texts on computer algebra (which I haven't), I'm sure you'll find clever ideas there.
My approach would be a graph-based search, as I doubt that a linear application of transformations will reliably lead to a solution.
Express the whole equation as an expression-tree the way you already started, but including an "equals" node above.
For the search-graph view, take one expression-tree as one search-state. The search-target is a decidable expression-tree like 1=1 or 1=0. When searching (expanding a search-state), create the child states by applying equivalence transformations on your expression (regex-like sounds quite plausible to me). Define an evaluation function that counts the overall complexity of an expression (e.g. number of nodes in the expression-tree). Do a directed search minimizing the evaluation function (expanding the lowest-complexity expression first), thus simplifying the expression until you reach a decidable form.
Depending on the expressions, it's quite possible that an unrestricted search never terminates. I don't know how you'd handle that, maybe by limiting the allowed complexity of expressions to some multiple of the original one. That would reduce the risk of running indefinitely, but leave you with undecided cases.

Prolog: Recursive Multiplication of 2 Numbers

I don't understand why this recursive definition of a multiplication is working.
I get the add part, but how is the value of "A" in this context.
The code is the following:
add(0,X,X).
add(s(X),Y,Z):-add(X,s(Y),Z).
mult(0,X,0).
mult(s(X),Y,Z):-mult(X,Y,A), add(Y,A,Z).
To understand the predicates, try to "read" what they are saying.
Reading the add/3 definition first...
add(0,X,X).
Adding 0 to X results in X.
add(s(X),Y,Z):-add(X,s(Y),Z).
Adding s(X) (the successor of X) to Y results in Z if adding X to s(Y) (the successor of Y) results in Z.
If we view successor as adding 1, then this is saying (X + 1) + Y results in Z if X + (Y + 1) results in Z. That's logically obvious, but doesn't seem to go anywhere. However, we'll note that this logic is closely coupled with the base case of add(0,X,X) since the recursive case will reduce the first argument by removing a single succession each iteration until the first argument becomes 0.
Now let's try mult/3...
mult(0,X,0).
Multiplying 0 by X results in 0
This seems to be obvious.
mult(s(X),Y,Z):-mult(X,Y,A), add(Y,A,Z).
Multiplying the successor of X by Y results in Z if multiplying X by Y results in A, and adding Y to A results in Z.
If you think of successor as adding 1, then this is saying that (X+1)*Y is Z if X*Y is A and A+Y is Z. This would make sense since (X+1)*Y is (X*Y)+Y which would be A+Y.
In this context, A is the value of (X-1) * Y. You find this value recursively with the mult rule then add it to Y in the add rule to get your final result. It is writing the multiplication as X * Y = (X - 1) * Y + Y
Really what ends up happening is it calls add X times, and each of those times it adds Y to the final result (starting from zero). This is exploiting multiplication as repeated addition. Here is a trace by hand:
mult(3, 2, Z)
Initial call
mult(2, 2, A_1), add(2, A_1, Z)
Subtract 1 fram X
mult(1, 2, A_2), add(2, A_2, A_1)
Again.
mult(0, 2, A_3), add(2, A_3, A_2)
One last time
mult(0, 2, A_3)
Only one possibility, as zero cannot match s(x). A_3 is set to 0.
mult(0, 2, 0), add(2, 0, A_2)
Step 4, but with A_3 substituted. We now know that A_2 must be 2.
mult(1, 2, 2), add(2, 2, A_1)
Step 3, but with A_2 substituted. We now know A_1 must be 4.
mult(2, 2, 4), add(2, 4, Z)
Step 2, but with A_1 substituted. We now know Z must be 6, the final result.
For steps 2 through 4 you are counting downward as a way of finding the number of times you need to repeat the addition operation. Step 5 is the base case, starting the final result at zero. In steps 6 through 8 you carry out the addition. This gives the result of Z = 6 = 2 + 2 + 2

Prolog, check divisibility in Peano arithmetic

I need to check if first given term (for example s(s(nul)) (or 2)) is dividable by the second term, (for example s(nul) (or 1)).
What I want to do is multiply given term by two and then check if that term is smaller or equal to the other term (if it is equal - problem is solved).
So far I got this:
checkingIfDividable(X,X).
checkingIfDividable(X,Y) :-
X > Y,
multiplication(X,Y).
/* multiplication by two should occur here.
I can't figure it out. This solution does not work!*/
multiplication(Y):-
YY is Y * 2,
checkingIfDividable(X,YY).
I can't seem to figure out how to multiply a term by 2. Any ideas?
If a = n*b, n > 0, it is also a = n*b = (1+m)*b = b + m*b, m >= 0.
So if a is dividable by b, and a = b+x, then x is also dividable by b.
In Peano encoding, n = 1+m is written n = s(m).
Take it from here.

Resources