计算的列表不同的奇数(如果存在),例如,他们的总和等于一定数目

StackOverflow https://stackoverflow.com/questions/1786365

  •  21-09-2019
  •  | 
  •  

:- 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)).

all_odd([]) :-!.
all_odd([H | T]) :-
 H mod 2 #= 1,
 all_odd(T).

solve(N,L) :-
 N2 is floor(sqrt(N)),
 Len in 1..N2,
 label([Len]),

 length(L, Len),

 L ins 1..N,

 all_different(L),
 all_odd(L),

 sum(L,#=,N),

 label(L),

 % only show sorted sets
 sort(L,L).

示例:

?- solve(17,L).
L = [17] ;
L = [1, 3, 13] ;
L = [1, 5, 11] ;
L = [1, 7, 9] ;
L = [3, 5, 9] ;
false.

其他提示

下面我采取在这个问题上,由一个谓词nonNegInt_oddPosSummands/2和辅助谓词list_n_sum/3实现的:

:- use_module(library(clpfd)).

list_n_sum([],_,0).
list_n_sum([Z|Zs],N,Sum) :-
    Z #>= 1,
    Z #=< N,
    Z mod 2 #= 1,
    Sum  #=  Z + Sum0,
    Sum0 #>= 0,
    list_n_sum(Zs,N,Sum0).

nonNegInt_oddPosSummands(N,List) :-
    length(_,N),
    list_n_sum(List,N,N),
    chain(List,#<),
    labeling([],List).

现在到一些查询!

首先, “其列表可以19被分解为?”:

?- nonNegInt_oddPosSummands(19,Zs).
Zs = [19] ;
Zs = [1, 3, 15] ;
Zs = [1, 5, 13] ;
Zs = [1, 7, 11] ;
Zs = [3, 5, 11] ;
Zs = [3, 7, 9] ;
false.

接着,不终止作为解集是无限的更一般的查询。 “哪个正整数N可以分解成Zs如果Zs具有2的长度是多少?”

?- Zs=[_,_], nonNegInt_oddPosSummands(N,Zs).
N = 4,  Zs = [1,3] ;
N = 6,  Zs = [1,5] ;
N = 8,  Zs = [1,7] ;
N = 8,  Zs = [3,5] ;
N = 10, Zs = [1,9] ...

最后,最一般的查询。就像它上面的一个不会终止,因为解集是无限的。然而,它的相当枚举所有分解和相应的正整数。

?- nonNegInt_oddPosSummands(N,Zs).
N = 0,  Zs = []      ;
N = 1,  Zs = [1]     ;
N = 3,  Zs = [3]     ;
N = 4,  Zs = [1,3]   ;
N = 5,  Zs = [5]     ;
N = 6,  Zs = [1,5]   ;
N = 7,  Zs = [7]     ;
N = 8,  Zs = [1,7]   ;
N = 8,  Zs = [3,5]   ;
N = 9,  Zs = [9]     ;
N = 9,  Zs = [1,3,5] ;
N = 10, Zs = [1,9] ...

我看到别人已经发布完整的解决方案。不过,你的代码可以做到锅中只有两个微小的修改:

  1. computeOddList测试 无论这种名单的存在。要知道的 这列表 相匹配的制约,只是返回。因此:

    computeOddList(N, L) :-
        ...
    
  2. 列表 TempL 目前可能含有重复。刚刚的地方 all_different(TempL)append 要解决这个问题。

现在 computeOddList 将返回的至少一个列表中的独特奇怪的数字,如果它的存在。然而,对于如 computeOddList(17, L) 它将不会返回的所有名单。我不知道clpFD自己,所以比其他暗示你的代码 广场填补没有击中的代码 我真的不能帮助你。

:- 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).

oddList([]) :- !.
oddList([H|T]) :-
  odd(H),
  oddList(T).

computeOddList(N,L) :-
  (L = [];L=[_|_]),
  length(L,V),
  V in 1..N,
  L ins 1..N,
  all_different(L),
  oddList(L),
  sumOfList(L,0,N).

我设法解决还挺它,但它并没有结束它运行的情况下后正常。 HMM。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top