Prolog中的算术 [英] arithmetic in Prolog

查看:43
本文介绍了Prolog中的算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有

A in 0..4, A * A #=A.

有没有可能

A in 0..1.

?

我会说不,因为 0*0 = 0,而 1*1 = 1,所以这两个不能是 A?

I would say no because 0*0 = 0, and 1*1 = 1, so these two can not be A?

提前致谢.

推荐答案

您在这里询问的是关于 CLPFD 系统的一致性.一般来说,系统试图通过不同类型的一致性与速度的权衡来尽可能地"保持一致性.但大多数情况下,它们只提供安全的近似值.然而,在这种情况下,一切似乎都很完美(此处使用 SICStus):

What you are asking here is about the consistency of CLPFD-systems. Generally speaking, systems try to maintain consistency "as good as they can" with different kinds of tradeoff to consistency vs. speed. But most of the time they deliver only safe approximations. In this case, however, everything seems perfect (here using SICStus):

| ?- A in 0..4, A * A #=A.
clpfd:(A*A#=A),
A in 0..1 ? ;
no

所以,我们得到一个答案:是的,有解决方案,前提是 clpfd(:A*A#=A), A in 0..1 为真.先验我们不能说是不是这样,但我们可以尝试一下:

So, we get as an answer: Yes, there are solutions, provided clpfd(:A*A#=A), A in 0..1 is true. A priori we cannot say whether or not that is the case, but we might try it out:

| ?- A in 0..4, A * A #=A, A = 1.
A = 1 ? ;
no
| ?- A in 0..4, A * A #=A, A = 0.
A = 0 ? ;
no

所以:0 和 1 都是一个解,因此 A in 0..1 是一个完美的答案!

So: Both 0 and 1 are a solution, and thus A in 0..1 is a perfect answer!

顺便说一句,您在 SICStus、SWI、B 和 GNU 中(基本上)得到相同的答案.

BTW, you get (essentially) the same answer in SICStus, SWI, B, and GNU.

这篇关于Prolog中的算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆