Reply – Re: how many different methods can we find for this question
Subject
Message
or Cancel
Re: how many different methods can we find for this question
— by  dunbarxx
Houston, we have an issue with LC.

I used the "combinatorial" function in the "MasterLibrary" for 26 elements, but the process was too slow. I made a kluge, so that a shorter call to that function was then processed by an even more brute-force routine. It works fine, but it also does not work.

LC stalls if the number of elements is greater than 23. I assume this is some sort of memory issue, unless LC just gets bored with the whole thing.

The following handlers pull an arbitrary value to test, and the gadget does indeed find the appropriate components.

on mouseUp
--FIELD 1 HAS THE LIST OF POPULATIONS
--1,9,18 ARE AN ARBITRARY TARGET SUM
-- THIS IS A SHORTER TEST FOR 18 LINES TO PROVE CONCEPT, AND FINISH IN A SHORT TIME

get line 1 of fld 1 & "," & line 9 of fld 1 & "," & line 18 of fld 1
put sum(it) into magicNumber --26978865

put fld 1 into tList
put combinatorial(12) into accum
repeat with y = 13 to 18 --FAILS WITH A VALUE OF 26, THE ACTUAL NUMBER OF LINES IN THE LIST
put accum into tempAccum
repeat for each line tLine in tempAccum
put tLine & comma & y & return after finalAccum
end repeat
put accum & return & finalAccum into accum
put y after accum
put y into fld 3
put "" into finalAccum
end repeat

repeat for each line tLine in accum
repeat for each item tItem in tLine
put line tItem of tList & "," after temp
end repeat
if sum(temp) = magicNumber then
exit to top
else
put "" into temp
end if
end repeat
end mouseUp

function combinatorial n
put the cursor into tCursor
put 1 into tCursorCtr
if n >= 15 then -- make sure you know what you're in for!
answer n && "objects will result in" && (2^15 -1) && "subsets" with "Continue" or "Cancel"
if it is "Cancel" then exit to top
end if
put 1 into subsetsVar -- initialize variable
repeat until x = n -- this repeats (2^n - 2) times, which is one less than the numer of possible subsets
if tCursorCtr > 5000 then
set the cursor to busy
put 0 into tCursorCtr
end if
put the last line in subsetsVar into x -- each subset is used as input to compute the next subset
put the number of items in x into noItems
if item noItems of x = n then
add 1 to item (noItems -1) of x -- increment 2nd to last item if last item = n
delete the last item of x -- get rid of last item if it = n
put return & x after subsetsVar -- add another subset to list of subsets
else
put ((item noItems of x) + 1) into item (noItems + 1) of x -- increment last item
put return & x after subsetsVar -- add another subset to list of subsets
end if
end repeat
set the cursor to tCursor
return subsetsVar -- return the list of all possible subsets
end combinatorial

-- Population LIST:

18897109
12828837
9461105
6371773
5965343
5946800
5582170
5564635
5268860
4552402
4335391
4296250
4224851
4192887
3439809
3279833
3095313
2812896
2783243
2710489
2543482
2356285
2226009
2149127
2142508
2134411

Craig Newman