计算一系列不同的奇数(如果存在),使它们的总和等于给定的数字
:- use_module(library(clpfd)). % load constraint library
% [constraint] Compute a list of distinct odd numbers (if one exists), such that their sum is equal to a given number.
odd(Num) :- Num mod 2 #= 1.
sumOfList([],N,N) :- !.
sumOfList([H|T],Counter,N) :-
NewN #= H + Counter,
sumOfList(T,NewN,N).
buildOddList(N,InputList,L) :-
%return list when sum of list is N
V in 1..N,
odd(V),
append(InputList,[V],TempL),
sumOfList(TempL,0,N)->
L = TempL;
buildOddList(N,TempL,L).
computeOddList(N) :-
buildOddList(N,[],L),
label(L).
这是我的代码,我似乎无法获得正确的输出,有代码批评家吗? :)
:- use_module(library(clpfd)). % load constraint library
% [constraint] Compute a list of distinct odd numbers (if one exists), such that their sum is equal to a given number.
odd(Num) :- Num mod 2 #= 1.
sumOfList([],N,N) :- !.
sumOfList([H|T],Counter,N) :-
NewN #= H + Counter,
sumOfList(T,NewN,N).
buildOddList(N,InputList,L) :-
%return list when sum of list is N
V in 1..N,
odd(V),
append(InputList,[V],TempL),
sumOfList(TempL,0,N)->
L = TempL;
buildOddList(N,TempL,L).
computeOddList(N) :-
buildOddList(N,[],L),
label(L).
This is my code, I can't seem to get the right output, any code critics? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我对这个问题的看法,通过谓词
nonNegInt_oddPosSummands/2
和辅助谓词list_n_sum/3
实现:现在进行一些查询!
首先,“19 可以分解成哪些列表?”:
接下来是一个更一般的查询,由于解集是无限的,该查询不会终止。 “如果
Zs
的长度为 2,哪些正整数N
可以分解为Zs
?”最后是最一般的查询。与上面的一样,它不会终止,因为解集是无限的。然而,它公平地枚举了所有分解和相应的正整数。
Here my take on this question, realized by a predicate
nonNegInt_oddPosSummands/2
and an auxiliary predicatelist_n_sum/3
:Now on to some queries!
First, "which lists can 19 be decomposed into?":
Next, a more general query that does not terminate as the solution set is infinite. "Which positive integers
N
can be decomposed intoZs
ifZs
has a length of 2?"Finally, the most general query. Like the one above it does not terminate, as the solution set is infinite. However, it fairly enumerates all decompositions and corresponding positive integers.
可以建议您这个解决方案:
示例:
Can suggest you this solution:
Example:
我看到其他人已经发布了完整的解决方案。尽管如此,您的代码只需进行两个细微的修改即可生效:
computeOddList
仅测试这样的列表是否存在。要知道哪个列表与约束匹配,只需返回它即可。因此:列表
TempL
当前可能包含重复项。只需将all_ different(TempL)
放在append
之后即可解决此问题。现在,computeOddList 将返回至少一个不同奇数列表(如果存在)。尽管如此,对于例如computeOddList(17, L),它不会返回所有列表。我自己不了解 clpFD,所以除了建议您将代码与 Xonix' 代码 我无法真正帮助你。
I see others have posted complete solutions already. Still, your code can be made to wok with only two slight modifications:
computeOddList
only tests whether such a list exists. To know which list matches the constraints, just return it. Thus:The list
TempL
may currently contain duplicates. Just placeall_different(TempL)
afterappend
to fix that.Now
computeOddList
will return at least one list of distinct odd numbers if it exists. Still, for e.g.computeOddList(17, L)
it will not return all lists. I don't know clpFD myself, so other than suggesting you compare your code to Xonix' code I cannot really help you.我设法解决了这个问题,但是在案件用完后它并没有正确结束。唔。
I managed to kinda solved it, however it doesn't end properly after it runs out of cases. Hmm.