-
something really difficult
--------------------------------------------------------------------------------
i need help to write a code that is very difficult for me i have to know the mathematical formula
explain
example
1.i have 6 groups x 5 textboxes each one with random numbers from 1....8 and each group has unique numbers not doubles (within each group)
2 theses numbers i split them to odd,even,sml,big(small numbers are lower than mnumber5) and (big numbers are numbers bigger than number 5 )
i add all the values of them and gives me a number the total number
example
odd+even+small+big = total
75+58+65+68=266
the random numbers in each group are
group 1 9,5,2,3,6
group 2 9,3,1,5,8
group 3 1,2,5,4,6
group 4 8,7,4,2,3
group 5 2,5,8,3,4
group 6 2,3,1,5,7
another example with the same total number 266
odd+even+small+big=total
59+74+58+75=266
the random numbers are
group 1 5,2,6,1,3
group 2 7,3,8,2,6
group 3 3,8,6,5,7
group 4 5,3,8,1,4
group 5 2,4,6,3,5
group 6 2,4,6,1,7
i want to know the formula that, giving a total number the code will put the apropriate random numbers instead the result after the addition of odd,even,small,big to be the same like the total number that i give
in other words how many combinations are equal to total number 266
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
thank you
memas
-
Re: something really difficult
Every number from 1 to 8 is either odd or even, so adding the number of odd and even numbers is just adding all numbers. From your calculations, you seem to say "small" numbers are <= 5, and "large" numbers are > 5, so adding the number of small and large numbers is again just adding all the numbers.
So, your total is just twice the sum of all of the numbers you choose. Slightly simplified, the number you're really interested in is half of the total you've listed, that is 266/2 = 133. All of your numbers taken together must sum to 133.
How can you choose 6 groups of 5 numbers in the range 1-8 where each group contains no duplicates such that their sum is 133? That's the hard part. First, there are many basically equivalent arrangements. Each group has no repeats, and so can be arranged in 5! = 5*4*3*2*1 = 120 ways. Since all 6 groups can be arranged in any one of these 120 ways, any solution can be mutated into an equivalent solution with just the order of the numbers changed in 120^6 = 2,985,984,000,000 ways. Further, the groups can be interchanged in a solution. However, sometimes groups will contain the same numbers, in which case those groups can't be rearranged. In the case where every group contains a different set of numbers from every other group, there are a further 6! = 6*5! = 720 arrangements to be had. However, this may be as few as 1 arrangement if every group contains the same numbers--e.g. say every group has the numbers {1, 2, 3, 4, 5}, giving a total of (1+2+3+4+5)*6*2 = 180.
Ok, so now let's try to count the number of arrangements which actually yield a solution regardless of their order, since the order of the numbers in each group can be taken into account as above. We'll also have to think about how we can arrange the groups, since we couldn't take it into account in general above.
One way to think about this problem is to say that in each group you're actually choosing which 3 of the numbers from 1-8 should be *left out* of the group. When choosing the group {1, 2, 3, 4, 5}, you've chosen that {6, 7, 8} will be left out. What totals can we get in a group?
We can leave out any of (8 choose 3) = 56 groups of three [*choose here is using the binomial coefficient, which you can look up on Wikipedia if you like]. These are...
{1, 2, 3}, {1, 2, 4}, {1, 2, 5}, ...
Given patience, you could figure out what the sum of each of these 56 3-element groups is, from which you could easily find the sum of the 5-element group they correspond to--it would just be (1+2+3+4+5+6+7+8) - (sum of 3 element group) = 8*(8+1)/2 - (sum of 3 element group) = 36 - (sum of 3 element group). At most, the sum of a 3-element group would be (6+7+8) = 21, giving a 5-element group minimum sum of (36-21) = 15 = (1+2+3+4+5) as expected. At least, the sum of a 3-element group would be (1+2+3) = 6, giving a 5-element group maximum sum of (36-6) = 30 = (4+5+6+7+8), again as expected. We should have sums at each intermediate value from 15 to 30 as well from some group mutation logic that I won't go into.
Ok. Now the question becomes, "how can we arrange these possible sums between 15 and 30 of 5-element groups into a total sum that matches the sum we wish?" This is a hard problem in general. It is related to the concept of an "integer partition" in number theory, and perhaps someone has looked into this particular case. You could probably brute force it here--that is, try all possible sums of such groups. The search would be significantly shorter if you limited yourself to increasing terms--i.e. 15+20+22+24+24+28 = 133, so their "total" from your original post is 133*2 = 266 as you wanted.
To keep track of the number of possible correct combinations instead, you would have to keep track of how many distinct ways you have to construct each 5-element group in your sum. That is, 20 can be constructed by subtracting off 16 from 36, so you want a 3-element group which sums to 16. Many can be found: {1, 7, 8}, {2, 6, 8}, ..., resulting in 5-element groups of {2, 3, 4, 5, 6}, {1, 3, 4, 5, 7}, .... Suppose (this may not be true) that you can construct a group that has a sum of 20 in 12 different ways. You could then fulfill the requirement of 15+20+22+24+24+28 using any of those 12 different groups which sum to 20, resulting in 8 different but correct combinations from this one partition of 133.
To brute force this computation, you would have to run a loop like the following:
Code:
for i[1] = 15 to 30
for i[2] = i[1] to 30
for i[3] = i[2] to 30
for i[4] = i[3] to 30
for i[5] = i[4] to 30
for i[6] = i[5] to 30
if Sum(i[], k=1 to 6) = 133 then
{magic to keep track of number of combinations;
product of ways to compute, but also multiplied by
number of group rearrangements depending on the
number of equivalent choices}
end if
next
next
next
next
next
next
This loop would run many fewer times than 16^6 = 16,777,216, so it would definitely be doable by a computer. There may be a cleverer way to do this loop, but the weights are different depending on which numbers are part of the integer partition which is not the usual case from what I've seen of integer partitions, so I dunno if it would actually be solvable more easily. I feel like perhaps a dynamic programming solution exists, if you're at all interested in generalizing this step of the algorithm.
At this point, you've counted every possible arrangement, somehow accounting for the order of groups when you actually have a group to work with. (Inside that for loop, this shouldn't actually be that hard, but I won't figure it out unless you implement the rest of this algorithm and happen to be stuck there, which to be honest I don't find likely considering the complexity of this solution.) Now you can account for the order of elements inside each group by multiplying by the above value of 120^6. This is the total number of ways to form the given sum in the given way.
So... to succinctly answer your question of
Quote:
in other words how many combinations are equal to total number 266
about 2,985,984,000,000*m for some number m which is very large and can be brute force computed using the above ideas. As an estimate, m should be around 360*(56/16)^6 = about 660,000, giving as a guestimate a total number of 2,000,000,000,000,000,000 arrangements.
Did you actually just want a way to construct a valid solution instead of counting all of them? Because that's much easier.
-
Re: something really difficult
jemidiah
thank you for your help i need time to understand your logic
thank you
memas
-
Re: something really difficult
Unless you have some experience with combinatorics, I would be surprised if you could follow it in its entirety, especially the missing pieces I didn't fully reason through dealing with group orderings. I'm not trying to be mean; just honest. For instance, if you threw a person off the street in, say, a Quantum Physics class, they'd be utterly lost. The difference is a little smaller between CS and combinatorics, but it's still sizable.
Of course I'm happy to explain individual parts. I believe the above to be a correct way to realistically solve your problem in a reasonable amount of time. I would guess I could implement it in a language that supplied arbitrary precision integers in a few hours.
-
Re: something really difficult
thank you again for your help
but because i am only 3 months programmer i need help to understand all that you wrote for me i think i will try in another way like that
i have a code that give random numbers to the groups under the conditions you know no dublicates whithin the group and numbers from 1..8 and i need help to put in the first textbox of each group the numbers that i want and ask the code to fill the rest of the textboxes with random numbers but not equal with the numbers that i put and then a code to mesure the sum of odd,even,small,big and give the total
-
Re: something really difficult
Does the code have to generate numbers which give a given total? If not, your question would be better asked in one of the main programming forums since it would have very little mathematical content.
For example, would you like to enter...
in the first box of each group and then have the computer come up with...
Code:
9,5,2,3,6
9,3,1,5,8
1,2,5,4,6
8,7,4,2,3
2,5,8,3,4
2,3,1,5,7
along with summary statistics like
Code:
odd = 75
even = 58
small = 65
big = 68
total = 266
where the final total of 266 was *not* known beforehand?
-
Re: something really difficult
i prefered to give the total number 266 and the computer to put the apropriate random numbers but because i cannot make the code and i dont have help it i ll try the second solution
-
Re: something really difficult
Given the constraint that you are given a particular total number that must be met, one simple solution is to just try guessing repeatedly until the total is correct. For most totals this shouldn't take terribly long, but for totals that are closer to the "edge" (i.e. require groups that almost all have the minimum possible sum with overall totals near 15*6*2=180 or the maximum with totals near 30*6*2=360) you'd be guessing for a very long time. Your original question explicitly wanted to know how many solutions there were, which is a somewhat involved calculation (as my first post demonstrates :) ). Solving this version with the given total constraint in every case seems similarly involved to ensure that every case is covered, but it doesn't seem that you're actually that interested in an absolutely perfect solution when the "try again and again until it works" solution should be fine for most cases.
Hmm, another interesting solution is to generate a solution randomly, calculate the total, and see which way the total must move to give you your required total. You could then ease things towards that total by swapping individual numbers in your groups (without ever swapping a number given to you by the user, and without ever doubling up a number). I would guess that this will always generate a solution if it exists, or show that no solution exists if that is the case, but I haven't carefully reasoned through some possibly problematic edge cases.
-
Re: something really difficult
thank you for your help
as i told you before i have a code that fills the textboxes with the conditions you know and exept this 6 groups gives me an extra six textboxes each textbox is connect with each group and take numbers from them so because i cannot make the perfect that you show me (i dont even know how to loop) i am trying to change the code so the groups has to take care the numbers in the extra 6 textboxes and not to put the numbers that i give in their random action
so in this case if i divide free in 6 pieces the number 266
example 75,58,65,68=266 give me
23,12,9,26,35 theses numbers i will give and the programe after i can change it will give me the 266
or 59,74,58,75=266
15,16,10,21,31
-
Re: something really difficult
if this is a combination we have 2,985,984,000,000*m?
9,5,2,3,6
9,3,1,5,8
1,2,5,4,6
8,7,4,2,3
2,5,8,3,4
2,3,1,5,7
odd = 75
even = 58
small = 65
big = 68
total = 266
thank you
memas
-
Re: something really difficult
I'm sorry, I can't really understand the last two posts. Perhaps you could say the same thing in several different ways and I would be able to catch one of them well enough to help.
-
Re: something really difficult
i am asking you how many like the combination you posted me has number 266
-
Re: something really difficult
If you're asking how many combinations use the same numbers as
9,5,2,3,6
9,3,1,5,8
1,2,5,4,6
8,7,4,2,3
2,5,8,3,4
2,3,1,5,7
in the same groups but with different orders of the groups and different orders of the numbers inside groups, then the answer is (5!)^6*6! = about 2.15*10^15 = about 2,150,000,000,000,000. This is indeed 2,985,984,000,000*m for m=6!=720.
-
Re: something really difficult
if i dont care about the order and only the total (sum)how many combinations to equal are 266?
thank you
memas
-
Re: something really difficult
-
Re: something really difficult
I implemented the middle part of my algorithm. It's a bit cryptic; if you want parts explained please ask. It's written in VB6. It's somewhat terrible code because you just wanted a number.
Code:
Private Sub Form_Load()
Dim t1 As Integer
Dim t2 As Integer
Dim t3 As Integer
Dim t4 As Integer
Dim t5 As Integer
Dim groupSum As Integer
Dim groupCombs(15 To 30) As Integer
For t1 = 1 To 8
For t2 = t1 + 1 To 8
For t3 = t2 + 1 To 8
For t4 = t3 + 1 To 8
For t5 = t4 + 1 To 8
groupSum = t1 + t2 + t3 + t4 + t5
groupCombs(groupSum) = groupCombs(groupSum) + 1
Next
Next
Next
Next
Next
Dim i1 As Integer
Dim i2 As Integer
Dim i3 As Integer
Dim i4 As Integer
Dim i5 As Integer
Dim i6 As Integer
Dim combs As Long
For i1 = 15 To 30
For i2 = i1 To 30
For i3 = i2 To 30
For i4 = i3 To 30
For i5 = i4 To 30
For i6 = i5 To 30
n = n + 1
If i1 + i2 + i3 + i4 + i5 + i6 = 133 Then
combs = combs + getNumGroupCombs(i1, i2, i3, i4, i5, i6, groupCombs())
End If
Next
Next
Next
Next
Next
Next
MsgBox combs
End Sub
Private Function getNumGroupCombs(i1 As Integer, i2 As Integer, i3 As Integer, i4 As Integer, i5 As Integer, i6 As Integer, groupCombs() As Integer) As Long
'Figure out how many different groups could be chosen to
' have an in-group sum of i1 through i6, orderlessly
Dim i(1 To 6) As Integer
Dim j As Long, j0 As Long
i(1) = i1: i(2) = i2: i(3) = i3: i(4) = i4: i(5) = i5: i(6) = i6
getNumGroupCombs = 1 'multiplicative unity
j = 1
Do Until j > 6
j0 = j
If j <> 6 Then
Do Until i(j) <> i(j + 1)
'How many i's have the same value consecutively?
j = j + 1
If j = 6 Then Exit Do
Loop
End If
getNumGroupCombs = getNumGroupCombs * multiChoose(groupCombs(i(j0)), j - j0 + 1)
j = j + 1
Loop
End Function
Private Function multiChoose(n As Integer, k As Integer) As Long
multiChoose = choose(n + k - 1, k)
End Function
Private Function choose(n As Integer, k As Integer) As Long
choose = factorial(n) / (factorial(k) * factorial(n - k))
End Function
Private Function factorial(ByVal n As Integer) As Long
If n = 0 Then
factorial = 1
Exit Function
End If
factorial = 1
Do Until n = 1
factorial = factorial * n
n = n - 1
Loop
End Function
When run the answer is 2,504,606.
-
Re: something really difficult
i have theses errors
ErrError 2
name 'groupCombs' is not declared.
Error 1 Array lower bounds can be only '0'.
-
Re: something really difficult
Even after 3 months of programing memas you should be able to figure out those two errors (the first one especially)...
-
Re: something really difficult
-
Re: something really difficult
declared menas you have declared a variable for use since the error says 'groupCombs' is not declared
that means? what to you that you are using a variable called 'groupCombs' and have not declared it in the scope you are using that variable in.
Again after 3 months programing and seeing the same error in other threads you have posted you should be able it figure that one out at least.
-
Re: something really difficult
i declare this but with what is equal
Dim groupcombs As Integer
Dim n As Integer
Dim i As Integer
-
Re: something really difficult
Since the second error says that arrays can only start at 0 the declareation of groupcombs never occurs. The code supplied is based on VB6 (where you can set the lower bound of an array to anything you wanted as long as it was less then the upper bound). You can not do this in .Net the lower bound of any array must be 0
-
Re: something really difficult
gary
i want to know how many combinations there are
example the total sum is 100
the groups are 6 with 5 textboxes each one
the range of the numbers are 1..8
and there not dublicates numbers within each group
i dont care about the order of the numbers or the place in of each number in the group only the sum to be 100
if i give you this number 1,1,1,1,1,1 one for each group and i ask you to put numbers that total will be 100 without to care about different orders of the groups and different orders of the numbers inside groups how many combinations we could have ?
-
Re: something really difficult
I don't know the math to do this I'm just a dumb programer/dba tell me logic and I code. The error you got is code related and I gave you the reason. You nedd to look as see if you can fix it.
-
Re: something really difficult
thank you for your help
memas
-
Re: something really difficult
may be you know something about transfer data from vb 8 programe to excel?
-
Re: something really difficult
I have written code for that yes.... In fact there are tutorials on this web site (I believe they were written by RobDog for doing that).
This should be discussed in a sperate thread of course.
-
Re: something really difficult
if i make a new threat and i send you the link will you help me?
-
Re: something really difficult
Did you look at the tutorials first. (last post on this thread about Excel topic for me)
-
Re: something really difficult
english is not my language is a little difficult to understand the way the tell something
i have a code that works ok but everytime i click make new sheet i dont want that i need in the same sheet to add the data instead in the end to have all data in the same book (sheet)
-
Re: something really difficult
Since the number of answers without looking at the order is small for a computer (under one million) you can try all combinations of groups that work and see if any of these groups have the numbers you want. To avoid getting into issues with order, you would want to make sure that your textboxes follow two conditions:
1. Each group of 5 textboxes is in ascending order
2. Each group is positioned after a group that has a smaller or equal sum.
This becomes a bunch of loops. First you have loops to find every possible sum of a group of 5 textboxes. Second you have loops to find which group sums can give you the correct overall total. Third you have loops which find all of the possible particular choices of groups which satisfy a given set of group sums which gives the correct total. At this point you're able to find every arrangement of numbers in all of your textboxes, ignoring the order of groups and of numbers inside a group, which satisfy your total. For each possible arrangement, you then have to check if it satisfies your conditions.
The above is tedious to code, but would be a great exercise. I won't code it for you. If you need help on particular bits of logic I'll answer, but I've given you the answer to your question and now it's time for you to put it into practice.
Good luck :).
-
Re: something really difficult
jemidiah
i think i make you tired but because for a new programmer like me it is impossible to write this code i can you tell if it is a professional site that i can pay for that?
-
Re: something really difficult
-
Re: something really difficult
tell me how gary?tell me details
-
Re: something really difficult
go to rent-a-coder.com
they will bid for your work
-
Re: something really difficult