Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Mathematics knundrum
Message
From
23/04/2003 17:44:09
 
 
To
23/04/2003 17:38:46
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00780672
Message ID:
00780896
Views:
15
I formatted the data entry a bit better here.

>>I'm working on a reconciliation project and i'm stuck. I have a list of numbers lets say 100 numbers and I have a target amount that some permutation of these numbers adds up to. I don't know how many of them will add up to this number. I need the list of numbers that adds up to my target number. I tried writing this but my brain overworked itself. Anybody ever write something like this or have any ideas? Thanks in advance!!!
>>
>>Example
>>Say my number is 362.21 and my number list is:
>>201.10
>>25.18
>>150.11
>>778.88
>>151.21
>>11.31
>>10.00
>>...
>>The answer is 201.10 150.11 and 10.00
>
>Marvin --
>
>I've wondered about a SQL solution to this. If 2 tables are joined without a join condition, a Cartesian join results -- all possible combinations of rows of the 2 tables. This is similar to your problem. The issue is how to tame it.
>
>I don't know if this will suit your needs. You mention 100 possible values. If that's realistic, this approach may work. However, orders of magnitude greater numbers of values would run up against VFP limits, though T-SQL might work.
>
>Conceptually, what we'll do is create a crosstab from your data and sum the values of the fields. We'll then do a selfjoin with the table into itself for the number of values that you have. The key is adding 1 more value -- a zero --into the data. The selfjoin will then allow us either to represent the actual value of the data, or 0. Finally, our where clause will be based on the desired sum.
>
>Consider the following table structure:
>TABLE t1
> nID N(10)
> nVal N(10)
>
>and values:
>  nID      nVal
>   0       0           This is added to our data set.
>   1       3
>   2       5
>   3       4
>   4       6
>   5       7
>
>Let's take a first cut at the SQL:
>
>
>SELECT x1.nval, x2.nval, x3.nval, x4.nval, x5.nval ;
>	FROM t1 x1, t1 x2, t1 x3, t1 x4, t1 x5 ;
>	into cursor temp
>
>
>The syntax t1 x1... uses the t1 table under other aliases. Each value is represented by the nVal field, but of different tables. This will combine all values with all values including themselves. A bit more than we need.
>
>The second cut :
>
>SELECT x1.nval, x2.nval, x3.nval, x4.nval, x5.nval ;
>	FROM t1 x1, t1 x2, t1 x3, t1 x4, t1 x5 ;
>	where x1.nval + x2.nval + x3.nval + x4.nval + x5.nval  = 7 ;
>	into cursor temp
>
>
>will give us all combinations equalling 7. However, there are many duplicated combinations which you don't need.
>
>The final cut restricts the value to its position in the list, and allows it the value of either itself or of 0. This will give the distinct combinations of the value you need.
>
>
>SELECT  x1.nval, x2.nval, x3.nval, x4.nval, x5.nval ;
>	FROM t1 x0, t1 x1, t1 x2, t1 x3, t1 x4, t1 x5 ;
>	where INLIST (x1.nid, 1, 0) ;
>          AND INLIST (x2.nid, 2, 0);
>          AND INLIST (x3.nid, 3, 0) ;
>          AND INLIST (x4.nid, 4, 0) ;
>          AND INLIST (x5.nid, 5, 0) ;
>          AND x0.nid = 0;
>	AND x1.nval + x2.nval + x3.nval + x4.nval + x5.nval  = 7 ;
>	into cursor temp
>
>
>This solution can be generalized quite easily, but you would run into SQL language limits with larger data sets.
>
>Hope you arrive at a solution for your specific need.
>
> Jay
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform