Background: http://xkcd.com/c287.html
(That's not really an np-complete problem, near as I can tell.)

Given
  ap=:2.15 2.75 3.35 3.55 4.2 5.8

This will list distinct combinations of ap which total to 15.05:
  ~./:~&.>(#~15.05=+/&>)(#~15.05>:+/\)&.>,{7#<ap

But, that's not very efficient.

A more efficient approach might be
    15.05 order ap
where:

order=:4 :0
 min=.<./y
 pos=.,:y
 r=.''
 while.{:$pos=.pos#"1~x>:min++/pos do.
   pos=.(#"1~ <:/@(_2{.])) pos((#"1~#), {:@[EMAIL PROTECTED] ,@# ,:@]) y
   r=.r,<"1|:pos#"1~x=+/pos
 end.
 /:~r
)

But, maybe that could be more elegant.

Does anyone have any good suggestions?

(Does J's library have any built-ins for addressing this kind
of problem?)

Thanks,

--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to