Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Mathematics knundrum
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00780672
Message ID:
00781115
Vues:
21
>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

I sat down to look at this last night, and see if I had any useful code around, but couldn't find anything directly useful. So I created 2 tables and some new code this morning. Here is the plan to make sure the code works, before doing anything else with it:

1) Table1 contains n records with some numerical data, like the original post has above.
2) Table2 begins empty, contains 2 fields, and is basically for testing purposes, though it might also be useful in real-time. But basically, Table2 Field1 is CHAR for writing all the combinations to test that it works right, and Field2 is NUMERIC for writing combination sums.

Here is what the code does, using table1 with 'n' values to get started. The code writes records to table2, that look like this (for a table1 with n=4 records, as an example, with two columns as noted. Table2 with the Combination looping results:

col1 col2
---------
1 (value)
2 (value)
3 etc
4 etc
12 (sum of records 1 + 2 values)
13 (sum of records 1 + 3 values)
14 etc
23
24
34
123 (sum of records 1 + 2 + 3 values)
124 etc
134
234
1234 (sum of records 1 + 2 + 3 + 4 values)

Each record/combination will have a value/sum for the data involved in those records, and SQL can extract all valid solutions from it easily given a target number at the end, though in fact table2 will not even be necessary in the finished code. But this is the theoretical solution that I think is desired, to begin.

The code is not at all elegant yet, as I just threw it together quickly this morning to get it working. It needs refinement to condense loops - but the first task is to obtain exactly the right loop combinations, like the above list does.

Note: I don't think SQL will yield a good generic solution for this problem, and it has a major problem in that it cannot *stop* when a solution is found (if that's desired). But it may depend on what the needs are. To be completely generic and stoppable at any loop-point, I'm using no SQL.

Let me know if anyone wants some code for this problem, I can either post some ugly unfinished code for someone to fix up, or I can fix it up myself later (kind of busy at work this morning), and post it a little later. Just about anyone here can come up with some good generic, condensed looping code, once the problem and solution are well-understood, I think.
The Anonymous Bureaucrat,
and frankly, quite content not to be
a member of either major US political party.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform