ECLiPSe Prolog-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)、失敗すると「No」を出力します(-10)。
Sp3000はコメントで適切に指摘しているように、成功すると「Yes」も出力します。それは確かに私がさらに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個のソリューションを見つけます(ただし、一部のソリューションが不足している可能性があります)。