Re: Efficiently finding combinations



Hello,

On Thu, 19 May 2005 22:00:33 +0200, Bart Demoen wrote:

> publiustemp-googlegroups@xxxxxxxxx wrote:

> How about
>
>
> one_hundred_bis(A,B,C,D,E) :-
> numbers(Numbers),
> member(A, Numbers),
> member(B, Numbers),
> A =< B, AB is A + B, AB < 100,
> member(C, Numbers),
> B =< C, ABC is AB + C, ABC < 100,
> member(D, Numbers),
> C =< D, ABCD is ABC + D, ABCD < 100,
> member(E, Numbers),
> D =< E,
> 100 is ABCD + E.

If the list is sorted the predicate can be faster (when a number is
choosen, we can forget previous numbers). For instance:

one_hundrer(A,B,C,D,E) :-
numbers(Numbers),
sort(Numbers, Sorted),
get_number(Sorted, A, Sorted_after_A),
A =< 100,
get_number(Sorted_after_A, B, Sorted_after_B),
AB is A+B, AB =< 100,
get_number(Sorted_after_B, C, Sorted_after_C),
ABC is AB+C, ABC =< 100,
get_number(Sorted_after_C, D, Sorted_after_D),
ABCD is ABC+D, ABCD =< 100,
E is 100 - ABCD,
get_number(Sorted_after_D, E, _).

get_number([X|T], X, [X|T]).
get_number([_|T], X, XT) :- get_number(T, X, XT).
.



Relevant Pages

  • Re: Need help with user-written routine in SOR$
    ... > ADRS2 is the other record contents by reference ...
    (comp.os.vms)
  • Repost: DrawString and ellipsis problem ... please help
    ... I have a rectangle with a string inside and the user can drag the mouse ... a string "abcd" is progressively displayed as: ... abc... ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: File parsing
    ... Build an array or associatiave array... ... I can NOT read until the next occurance ot "abc"- abc 5 ...
    (comp.lang.awk)
  • DrawString and ellipsis problem
    ... I have a rectangle with a string inside and the user can drag the mouse ... While shrinking, a string "abcd" is ... abc... ...
    (microsoft.public.dotnet.languages.csharp)
  • DrawString and ellipsis problem
    ... I have a rectangle with a string inside and the user can drag the mouse ... While shrinking, a string "abcd" is ... abc... ...
    (microsoft.public.dotnet.framework.drawing)