ECLiPSe前言-118(138-20)
我使用了以下Prolog实现:http : //eclipseclp.org/
:-lib(util).
t(0,S,[]):-!,S<0.00001,S> -0.00001.
t(N,S,[X|Y]):-A is integer(ceiling(S*S)),between(1,A,X),M is N-1,T is S-sqrt(X),t(M,T,Y).
这是一种非常幼稚的指数方法。列出所有可能的解决方案需要花费时间来覆盖所有组合(编辑:现在访问的整数的范围在每一步都会减小,从而消除了许多无用的组合)。
以下是测试会话的笔录。默认情况下,环境将尝试查找所有可能的解决方案(-10),并在失败时显示“否”(-10)。
正如Sp3000在注释中正确指出的那样,它在成功时也会打印“是”。那肯定意味着我可以再删除10点;-)
[eclipse 19]: t(1,0.5,R).
No (0.00s cpu)
[eclipse 20]: t(2,3.414213562373095,R).
R = [2, 4]
Yes (0.00s cpu, solution 1, maybe more) ? ;
R = [4, 2]
Yes (0.00s cpu, solution 2, maybe more) ? ;
No (0.01s cpu)
[eclipse 21]: t(3,7.923668178593959,R).
R = [6, 7, 8]
Yes (0.02s cpu, solution 1, maybe more) ? ;
R = [6, 8, 7]
Yes (0.02s cpu, solution 2, maybe more) ? ;
R = [7, 6, 8]
Yes (0.02s cpu, solution 3, maybe more) ?
[eclipse 22]: t(5,5.0,R).
R = [1, 1, 1, 1, 1]
Yes (0.00s cpu, solution 1, maybe more) ? ;
^C
interruption: type a, b, c, e, or h for help : ? abort
Aborting execution ...
Abort
[eclipse 23]: t(5,13.0,R).
R = [1, 1, 1, 1, 81]
Yes (0.00s cpu, solution 1, maybe more) ? ;
R = [1, 1, 1, 4, 64]
Yes (0.00s cpu, solution 2, maybe more) ? ;
R = [1, 1, 1, 9, 49]
Yes (0.00s cpu, solution 3, maybe more) ?
[eclipse 24]:
(编辑)关于性能,至少与其他性能相比,它是相当不错的(例如,参见FryAmTheEggman的评论)。首先,如果要打印所有结果,请添加以下谓词:
p(N,S):-t(N,S,L),write(L),fail.
p(_,_).
有关(5,13.0)的情况,请参见http://pastebin.com/ugjfEHpw,该情况在0.24秒内完成并找到495个解决方案(但也许我缺少一些解决方案,我不知道)。