tags:

views:

757

answers:

6

If

a+b+c=1
a^2+b^2+c^2=2
a^3+b^3+c^3=3

then

a^4+b^4+c^4=?

I've known the result is 25/6,but how to calculate it by prolog?

I tried this but failed:

[1] 5 ?- A+B+C=:=1,A**2+B**2+C**2=:=2,A**3+B**3+C**3=:=3.
ERROR: Unhandled exception: =:=/2: Arguments are not sufficiently instantiated
A: 

You're multiplying, not exponentiating. I don't know if Prolog has a built-in exponentiation operator, but building one shouldn't be more than:

expt(var,0) :- 1, expt(var, n) :- var*expt(var, n-1)

That is, if I actually remember my prolog syntax correctly (it's been, what, close to 20 years since I last wrote prolog code).

Vatine
It's SO making it look like multiplying.It was `a**2...`.I've just corrected it.
+1  A: 

In your question:

a+b+c=1
a^2+b^2+c^2=2
a^3+b^3+c^3=3
a^4+b^4+c^4=?

it's a system of Non-Linear equations. This link describes mathematical way to solve simple(non-linear) systems.

[EDIT] You can't solve this equation by Prolog just with one or two commands, this is not as easy as you think...

but .... you can do some math :

a+b+c=1
a^2+b^2+c^2 = ( a + b + c )^2 - 2ab - 2ac - 2bc = 2
a^3+b^3+c^3 = ( a + b + c )*(a^2 + b^2 + c^2 - ab - ab - bc ) + 3abc = 3
.
.
.

and solve it !!! ( you can implement this sequence in Prolog , but this is not easy ... )

Michel Kogan
I didn't find my answer there,but thank you all the same !
I will write additional information as soon as I came back home :)
Michel Kogan
What I really need is a simple prolog solution
check my answer again ...
Michel Kogan
In fact I know the math solution,that's where 25/6 comes from.
+2  A: 

Unfortunately, using Prolog as a numerical solver is not trivial.

?- X is 1 + 2*X.

will raise the very same error while the answer looks quite obvious.

While Prolog is able to drill through its knowledge base to magically solve a logical problem, it just cannot do the same with numbers. The problem there is not only the infinity of most common number sets but also continuity (i.e. what number comes after 1.1234567890123456789?).

So, in short: I do not believe it is possible to write a simple program to solve this, even in Prolog.

However, there have been some attempts to implement some numeric solvers in Prolog (cf the top right cached version).

Benoit Vidis
A: 

The standard for Prolog, ISO Prolog, does not include constraint logic programming predicates, which are what you need to express your constraints as equations. However, several prolog implementations have their own extension:

SWI-Prolog includes a module for constraint logic programming over real numbers. This allows you to write constraints such as:

:- use_module(library(clpr)).
run(A, B, C) :- {A+B+C=1, A*A+B*B+C*C=2}.

See section A.8 of SWI-Prolog documentation for more information.

SICStus Prolog also has a constraint system called CHR (constraint handling rules).

GNU Prolog has a finite domain solver, but it only works on integers.

Jerome
I'm using swi-prolog,but can't run the code: 3 ?- run(A, B, C) :- {A+B+C=1, A*A+B*B+C*C=2}. ERROR: toplevel: Undefined procedure: (:-)/2 (DWIM could not correct goal)
user198729: Are you writing that in the SWI-Prolog REPL? If you do, you can't. Write that in a file 'foo.pro' and consult it with consult('foo.pro'). Otherwise, put that inside an assert().
Dom De Felice
A: 

You could try adding some additional facts to define the set of positive numbers and the set of positive rational numbers. My Prolog is a bit rusty, but something like:

number(1).
number(N) :- number(N-1).

rational(X) :- number(A), number(B), X is A/B.

Then add the constraints on A,B,C giving:

?- rational(A), rational(B), rational(C), A+B+C=:=1,A**2+B**2+C**2=:=2,A**3+B**3+C**3=:=3.

However, I suspect the order of searching through the set of rational numbers will hit infinity for A before a second value is tried for B. A workaround/cheat (assuming some knowledge of the solution), would be to only define the numbers smaller than (say) 100 to limit the search space.

number(1).
number(N) :- N <= 100, number(N-1).

Sorry if my syntax is a little off. I haven't used Prolog for a long, long time.

MZB
Still it can't be run...1 ?- number(N) :- 1.ERROR: toplevel: Undefined procedure: (:-)/2 (DWIM could not correct goal)
I did warn you I was rusty and the syntax was off! The first line should have read number(1). Fixed. Remaining errors are left as an exercise for the student:-).
MZB
+1  A: 

This is not an answer to your original question, but from some of your responses to other answers I see that you are making a fundamental mistake:

You cannot just enter predicates at the Prolog prompt!

You might want to read that again. (And this is what your Prolog is trying to tell you saying Undefined procedure: :-/2). So if e.g Jerome suggests (quote):

:- use_module(library(clpr)). 
run(A, B, C) :- {A+B+C=1, A*A+B*B+C*C=2}.

you need to put this code into a file and consult/1 the file (look up the manual for "consult"). At a Prolog command prompt you can only enter queries, not predicates. (There are ways around this, but you better get the "consult" thing straight first).

After consulting the file, you will then enter queries like run(A,B,C) and get some results. This way, you will find most of the code offered will run for you, too.

You should really consider reading an introduction to Prolog, as someone commented to one of your other questions on SO, before hasting to solve particular problems.

ThomasH
I've tried both way,not working!