Re: Efficiently finding combinations
- From: Bart Demoen <bmd@xxxxxxxxxxxxxxxxx>
- Date: Thu, 19 May 2005 22:00:33 +0200
publiustemp-googlegroups@xxxxxxxxx wrote:
Recently I was trying to figure out the most efficient way of answering someone's question about finding all combinations of five numbers which add to 100, given that each number must be divisible by 2 or 5 (no, I don't know why he needed that either.) My solution didn't seem very clean (assumes that Numbers is a list with all of the allowed numbers to pick from):
one_hundred(A,B,C,D,E) :- number(Numbers), member(A, Numbers), member(B, Numbers), member(C, Numbers), member(D, Numbers), member(E, Numbers), 100 is A + B + C + D + E.
That turned out to be rather slow. Is there a cleaner way of expressing this?
Also, as a side note, I also couldn't think of an efficient way of ensuring that (2,2,2,5,85) and (2,2,2,85,5) wouldn't both be represented, though that wasn't specifically asked for.
Cheers, Ovid
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.
Does it meet your goals ? Can you generalise from that to other situations ?
Cheers
Bart Demoen .
- Follow-Ups:
- Re: Efficiently finding combinations
- From: Christophe Delord
- Re: Efficiently finding combinations
- From: publiustemp-googlegroups
- Re: Efficiently finding combinations
- References:
- Efficiently finding combinations
- From: publiustemp-googlegroups
- Efficiently finding combinations
- Prev by Date: Efficiently finding combinations
- Next by Date: Re: Efficiently finding combinations
- Previous by thread: Efficiently finding combinations
- Next by thread: Re: Efficiently finding combinations
- Index(es):
Relevant Pages
|
|