Hello I am trying to find the GCD of a list of numbers in Prolog, but I cannot manage to do it.
Could you please help?
I am not even barely close so I wont share any of my work till now.
PP: In the process of trying to solve this I've encountered another problem that I can't solve, if you can help with this one too I will be grateful. It's finding all the common divisors of two numbers.
Thanks!
Thanks to Lurker!
I wasn't using the proper algorithm. I thought of finding GCD of two numbers and then GCD of the result and the next one, but I am not sure why I got confused that this won't work.
Anyway, here is the code:
gcd(0,X,X):- X > 0, !.
gcd(X,Y,Z):- X>=Y, X1 is X -Y, gcd(X1,Y,Z).
gcd(X,Y,Z):- X<Y, X1 is Y-X, gcd(X1,X,Z).
gcdL([H,H1|T],Z):-gcd(H,H1,X),gcdL([X|T],Z).
gcdL([H1,H2],Z):-gcd(H1,H2,Z).
And for the curious here is the retarded approach I was trying to achieve. And I was almost there as the first answer the script gives is correct, but it continues to backtrack. Anyway it's ugly, long, hard and inefficient:
minel([X],X).
minel([H,H1|T],X):-H>H1,minel([H1|T],X).
minel([H,H1|T],X):-H=<H1,minel([H|T],X).
gcdL(L,X):-gcdL(L,X,1).
gcdL(L,X,C):-minel(L,M),C<M,delAll(L,C),Temp is C,C1 is C + 1,gcdL(L,R,C1),X is max(Temp,R).
gcdL(L,X,C):-minel(L,M),C1 is C + 1,C1=<M,gcdL(L,X,C1).
gcdL(L,X,C):-minel(L,M),C=:=M,X is 1.
delAll([T],X):- 0 is mod(T,X).
delAll([H|T],X):- 0 is mod(H,X),delAll(T,X)
Note taken: First find the most proper algorithm, then try to script the problem.
I believe this link will help you
https://math.stackexchange.com/questions/8611/number-of-common-divisors-between-two-given-numbers
insist of count you can store the numbers to a table for example.
Related
So I am currently learning prolog and I can't get my head around how this language works.
"It tries all the possible solutions until it finds one, if it doesn't it returns false" is what I've read that this language does. You just Describe the solution and it finds it for you
With that in mind, I am trying to solve the 8 queens problem ( how to place 8 queens on a chess board without anyone threatening the others).
I have this predicate, 'safe' that gets a list of pairs, the positions of all the queens and succeeds when they are not threatening each other.
When I enter in the terminal
?- safe([(1,2),(3,5)]).
true ?
| ?- safe([(1,3),(1,7)]).
no
| ?- safe([(2,2),(3,3)]).
no
| ?- safe([(2,2),(3,4),(8,7)]).
true ?
it recognizes the correct from the wrong answers, so it knows if something is a possible solution
BUT
when I enter
| ?- safe(L).
L = [] ? ;
L = [_] ? ;
it gives me the default answers, even though it recognizes a solution for 2 queens when I enter them.
here is my code
threatens((_,Row),(_,Row)).
threatens((Column,_),(Column,_)).
threatens((Column1,Row1),(Column2,Row2)) :-
Diff1 is Column1 - Row1,
Diff2 is Column2 - Row2,
abs(Diff1) =:= abs(Diff2).
safe([]).
safe([_]).
safe([A,B|T]) :-
\+ threatens(A,B),
safe([A|T]),
safe(T).
One solution I found to the problem is to create predicates 'position' and modify the 'safe' one
possition((0,0)).
possition((1,0)).
...
...
possition((6,7)).
possition((7,7)).
safe([A,B|T]) :-
possition(A),
possition(B),
\+ threatens(A,B),
safe([A|T]),
safe(T).
safe(L,X):-
length(L,X),
safe(L).
but this is just stupid, as you have to type everything explicitly and really really slow,
even for 6 queens.
My real problem here, is not with the code itself but with prolog, I am trying to think in prolog, But all I read is
Describe how the solution would look like and let it work out what is would be
Well that's what I have been doing but it does not seem to work,
Could somebody point me to some resources that don't teach you the semantics but how to think in prolog
Thank you
but this is just stupid, as you have to type everything explicitly and really really slow, even for 6 queens.
Regarding listing the positions, the two coordinates are independent, so you could write something like:
position((X, Y)) :-
coordinate(X),
coordinate(Y).
coordinate(1).
coordinate(2).
...
coordinate(8).
This is already much less typing. It's even simpler if your Prolog has a between/3 predicate:
coordinate(X) :-
between(1, 8, X).
Regarding the predicate being very slow, this is because you are asking it to do too much duplicate work:
safe([A,B|T]) :-
...
safe([A|T]),
safe(T).
Once you know that [A|T] is safe, T must be safe as well. You can remove the last goal and will get an exponential speedup.
Describe how the solution would look like and let it work out what is
would be
demands that the AI be very strong in general. We are not there yet.
You are on the right track though. Prolog essentially works by enumerating possible solutions and testing them, rejecting those that don't fit the conditions encoded in the program. The skill resides in performing a "good enumeration" (traversing the domain in certain ways, exploiting domain symmetries and overlaps etc) and subsequent "fast rejection" (quickly throwing away whole sectors of the search space as not promising). The basic pattern:
findstuff(X) :- generate(X),test(X).
And evidently the program must first generate X before it can test X, which may not be always evident to beginners.
Logic-wise,
findstuff(X) :- x_fulfills_test_conditions(X),x_fullfills_domain_conditions(X).
which is really another way of writing
findstuff(X) :- test(X),generate(X).
would be the same, but for Prolog, as a concrete implementation, there would be nothing to work with.
That X in the program always stands for a particular value (which may be uninstantiated at a given moment, but becomes more and more instantiated going "to the right"). Unlike in logic, where the X really stands for an unknown object onto which we pile constraints until -ideally- we can resolve X to a set of concrete values by applying a lot of thinking to reformulate constraints.
Which brings us the the approach of "Constraint Logic Programming (over finite domains)", aka CLP(FD) which is far more elegant and nearer what's going on when thinking mathematically or actually doing theorem proving, see here:
https://en.wikipedia.org/wiki/Constraint_logic_programming
and the ECLiPSe logic programming system
http://eclipseclp.org/
and
https://www.metalevel.at/prolog/clpz
https://github.com/triska/clpfd/blob/master/n_queens.pl
N-Queens in Prolog on YouTube. as a must-watch
This is still technically Prolog (in fact, implemented on top of Prolog) but allows you to work on a more abstract level than raw generate-and-test.
Prolog is radically different in its approach to computing.
Arithmetic often is not required at all. But the complexity inherent in a solution to a problem show up in some place, where we control how relevant information are related.
place_queen(I,[I|_],[I|_],[I|_]).
place_queen(I,[_|Cs],[_|Us],[_|Ds]):-place_queen(I,Cs,Us,Ds).
place_queens([],_,_,_).
place_queens([I|Is],Cs,Us,[_|Ds]):-
place_queens(Is,Cs,[_|Us],Ds),
place_queen(I,Cs,Us,Ds).
gen_places([],[]).
gen_places([_|Qs],[_|Ps]):-gen_places(Qs,Ps).
qs(Qs,Ps):-gen_places(Qs,Ps),place_queens(Qs,Ps,_,_).
goal(Ps):-qs([0,1,2,3,4,5,6,7,8,9,10,11],Ps).
No arithmetic at all, columns/rows are encoded in a clever choice of symbols (the numbers indeed are just that, identifiers), diagonals in two additional arguments.
The whole program just requires a (very) small subset of Prolog, namely a pure 2-clauses interpreter.
If you take the time to understand what place_queens/4 does (operationally, maybe, if you have above average attention capabilities), you'll gain a deeper understanding of what (pure) Prolog actually computes.
First of all, I saw this same question being asked earlier today and I decided to give it a try myself (and had trouble with it). I hope it's alright that I created a new question for this!
I'm trying to make a prolog program that solves the quadratic formula X = (−b±√b2−4ac)/2a. The implemented predicate quadratic([A,B,C], [Result]) takes a list of a, b and c as an the first argument and a list of the result(s) as the second. The lists are giving me trouble however; I wrote this code:
quadratic([A,B,C], [X]):-
underRoot([A,B,C], UnderRootResult),
UnderRootResult<0,
X is 0.
quadratic([A,B,C], [X]):-
underRoot([A,B,C], UnderRootResult),
UnderRootResult=:=0,
X is -B/2*A.
quadratic([A,B,C], [X]):-
underRoot([A,B,C], UnderRootResult),
UnderRootResult>0,
X is -B - sqrt(UnderRootResult)/2*A,
X is -B + sqrt(UnderRootResult)/2*A.
(This is probably full of mistakes, so excuse me in advance)
Running this will give me the undefined procedure error for underRoot/2. I don't really get why this happens. I feel like I have the general idea of how to program this but that I'm making lots of newbie mistakes. I can;t seem to find out what the problem is though, so I would appreciate any help!
edit: Also, if I'm allowed to ask two questions at once, how would I get both X's in the case of >0 in a list as one result?
There are many things here. First of all, usually all calls in the body are put on the same column, but that is not really an error.
Furthermore I do not find an underRoot/2 predicate. We can implement one like:
underRoot([A,B,C],X) :-
X is B*B-4*A*C.
Furthermore you always put [X] in the head of the clauses. But in case UnderRoot is less than zero, there are no solutions, so the list should be []. Furthermore in case UnderRoot > 0, there are two solutions, so the solution should be [X1,X2].
Finally if you write -B/2*A, it will be interpreted like: (-B/2)*A, so you will multiply with A. So you will need to use -B/(2*A).
So this brings us to a following proposal:
quadratic(L, []) :-
underRoot(L, U),
U < 0.
quadratic([A,B,C], [X]) :-
underRoot([A,B,C],0),
X is -B/(2*A).
quadratic([A,B,C], [X1, X2]) :-
underRoot([A,B,C],U) :-
SU is sqrt(U),
X1 is (-B-SU)/(2*A),
X2 is (-B+SU)/(2*A).
The question seems too easy to answer, however it is not, since I have to deal with functions that do not have closed forms (or I don't know how to find them). For example, I would like to find inverse functions for:
y == x Tan[x]
and
y == a x + b Tan[x].
Thus far, I used Newton-Rhapson's method for the inverse transformations. It works fine, but requires iterations. I just wonder whether there is a method to prove that there is a better solution or not. I've tried Wolfram Mathematica to find a solution, but since I'm a beginner. I have had no luck to get anything meaningful.
Seems it can't be done.
Solve[y == x Tan[x], x]
Solve::nsmet: This system cannot be solved with the methods available to Solve.
InverseFunction[# Tan[#] &]
I have defined an extended Euclidean algorithm in Maxima as
ext_euclid(a,b):=block(
[x,y,d,x_old,y_old,d_old],
if b = 0 then return([1,0,a])
else ([x_old,y_old,d_old]:ext_euclid(b,mod(a,b)),
[x,y,d]:[y_old,x_old-quotient(a,b)*y_old,d_old],
return([x,y,d])));
in order to solve linear Diophantine equations of the form a+b=c where gcd(a,b)=1, however if a-b=c I get -1 returned by the algorithm for the divisor but gcd(a,-b) as before. Is my algorithm wrong, or can it be used for a-b=c?
Iain
I don't quite understand what the problem is. Can you please give two examples, one in which the result matches what you expected, and one in which it doesn't (and please say what's your expected result in that case).
EDIT: OP replies: "to solve 5x+7y is 23 ext_euclid (5,7) returns [3,-2,1] where gcd(5,7) is 1 however for 5x-7y is 23 ext_euclid(5,-7) returns [-3,1,-1] but gcd(5,-7) is still 1 this fails Bezout's identity ax+by is gcd(a,b)"
Also if you are trying to implement a particular statement of the algorithm, can you please link to it or copy it here.
OP replies: "code at http://mvngu.wordpress.com/2009/08/25/elementary-number-theory-using-maxima/"
One possible thing to look at: does the mod function behave as you expect it?
OP replies: "mod(5,7) is, mod(5,-7) is -2"
I am a Mechanical engineer with a computer scientist question. This is an example of what the equations I'm working with are like:
x = √((y-z)×2/r)
z = f×(L/D)×(x/2g)
f = something crazy with x in it
etc…(there are more equations with x in it)
The situation is this:
I need r to find x, but I need x to find z. I also need x to find f which is a part of finding z. So I guess a value for x, and then I use that value to find r and f. Then I go back and use the value I found for r and f to find x. I keep doing this until the guess and the calculated are the same.
My question is:
How do I get the computer to do this? I've been using mathcad, but an example in another language like C++ is fine.
The very first thing you should do faced with iterative algorithms is write down on paper the sequence that will result from your idea:
Eg.:
x_0 = ..., f_0 = ..., r_0 = ...
x_1 = ..., f_1 = ..., r_1 = ...
...
x_n = ..., f_n = ..., r_n = ...
Now, you have an idea of what you should implement (even if you don't know how). If you don't manage to find a closed form expression for one of the x_i, r_i or whatever_i, you will need to solve one dimensional equations numerically. This will imply more work.
Now, for the implementation part, if you never wrote a program, you should seriously ask someone live who can help you (or hire an intern and have him write the code). We cannot help you beginning from scratch with, eg. C programming, but we are willing to help you with specific problems which should arise when you write the program.
Please note that your algorithm is not guaranteed to converge, even if you strongly think there is a unique solution. Solving non linear equations is a difficult subject.
It appears that mathcad has many abstractions for iterative algorithms without the need to actually implement them directly using a "lower level" language. Perhaps this question is better suited for the mathcad forums at:
http://communities.ptc.com/index.jspa
If you are using Mathcad, it has the functionality built in. It is called solve block.
Start with the keyword "given"
Given
define the guess values for all unknowns
x:=2
f:=3
r:=2
...
define your constraints
x = √((y-z)×2/r)
z = f×(L/D)×(x/2g)
f = something crazy with x in it
etc…(there are more equations with x in it)
calculate the solution
find(x, y, z, r, ...)=
Check Mathcad help or Quicksheets for examples of the exact syntax.
The simple answer to your question is this pseudo-code:
X = startingX;
lastF = Infinity;
F = 0;
tolerance = 1e-10;
while ((lastF - F)^2 > tolerance)
{
lastF = F;
X = ?;
R = ?;
F = FunctionOf(X,R);
}
This may not do what you expect at all. It may give a valid but nonsense answer or it may loop endlessly between alternate wrong answers.
This is standard substitution to convergence. There are more advanced techniques like DIIS but I'm not sure you want to go there. I found this article while figuring out if I want to go there.
In general, it really pays to think about how you can transform your problem into an easier problem.
In my experience it is better to pose your problem as a univariate bounded root-finding problem and use Brent's Method if you can
Next worst option is multivariate minimization with something like BFGS.
Iterative solutions are horrible, but are more easily solved once you think of them as X2 = f(X1) where X is the input vector and you're trying to reduce the difference between X1 and X2.
As the commenters have noted, the mathematical aspects of your question are beyond the scope of the help you can expect here, and are even beyond the help you could be offered based on the detail you posted.
However, I think that even if you understood the mathematics thoroughly there are computer science aspects to your question that should be addressed.
When you write your code, try to make organize it into functions that depend only upon the parameters you are passing in to a subroutine. So write a subroutine that takes in values for y, z, and r and returns you x. Make another that takes in f,L,D,G and returns z. Now you have testable routines that you can check to make sure they are computing correctly. Check the input values to your routines in the routines - for instance in computing x you will get a divide by 0 error if you pass in a 0 for r. Think about how you want to handle this.
If you are going to solve this problem interatively you will need a method that will decide, based on the results of one iteration, what the values for the next iteration will be. This also should be encapsulated within a subroutine. Now if you are using a language that allows only one value to be returned from a subroutine (which is most common computation languages C, C++, Java, C#) you need to package up all your variables into some kind of data structure to return them. You could use an array of reals or doubles, but it would be nicer to choose to make an object and then you can reference the variables by their name and not their position (less chance of error).
Another aspect of iteration is knowing when to stop. Certainly you'll do so when you get a solution that converges. Make this decision into another subroutine. Now when you need to change the convergence criteria there is only one place in the code to go to. But you need to consider other reasons for stopping - what do you do if your solution starts diverging instead of converging? How many iterations will you allow the run to go before giving up?
Another aspect of iteration of a computer is round-off error. Mathematically 10^40/10^38 is 100. Mathematically 10^20 + 1 > 10^20. These statements are not true in most computations. Your calculations may need to take this into account or you will end up with numbers that are garbage. This is an example of a cross-cutting concern that does not lend itself to encapsulation in a subroutine.
I would suggest that you go look at the Python language, and the pythonxy.com extensions. There are people in the associated forums that would be a good resource for helping you learn how to do iterative solving of a system of equations.