Pascal's rule on counting the subset's of a set works great, when the set contains unique entities.
Is there a modification to this rule for when the set contains duplicate items?
For instance, when I try to find the count of the combinations of the letters A,B,C,D, it's easy to see that it's 1 + 4 + 6 + 4 + 1 (from Pascal's Triangle) = 16, or 15 if I remove the "use none of the letters" entry.
Now, what if the set of letters is A,B,B,B,C,C,D? Computing by hand, I can determine that the sum of subsets is: 1 + 4 + 8 + 11 + 11 + 8 + 4 + 1 = 48, but this doesn't conform to the Triangle I know.
Question: How do you modify Pascal's Triangle to take into account duplicate entities in the set?
It looks like you want to know how many sub-multi-sets have, say, 3 elements. The math for this gets very tricky, very quickly. The idea is that you want to add together all of the combinations of ways to get there. So you have C(3,4) = 4 ways of doing it with no duplicated elements. B can be repeated twice in C(1,3) = 3 ways. B can be repeated 3 times in 1 way. And C can be repeated twice in C(1,3) = 3 ways. For 11 total. (Your 10 you got by hand was wrong. Sorry.)
In general trying to do that logic is too hard. The simpler way to keep track of it is to write out a polynomial whose coefficients have the terms you want which you multiply out. For Pascal's triangle this is easy, the polynomial is (1+x)^n. (You can use repeated squaring to calculate this more efficiently.) In your case if an element is repeated twice you would have a (1+x+x^2) factor. 3 times would be (1+x+x^2+x^3). So your specific problem would be solved as follows:
(1 + x) (1 + x + x^2 + x^3) (1 + x + x^2) (1 + x)
= (1 + 2x + 2x^2 + 2x^3 + x^4)(1 + 2x + 2x^2 + x^3)
= 1 + 2x + 2x^2 + x^3 +
2x + 4x^2 + 4x^3 + 2x^4 +
2x^2 + 4x^3 + 4x^4 + 2x^5 +
2x^3 + 4x^4 + 4x^5 + 2x^6 +
x^4 + 2x^5 + 2x^6 + x^7
= 1 + 4x + 8x^2 + 11x^3 + 11x^4 + 8x^5 + 4x^6 + x^7
If you want to produce those numbers in code, I would use the polynomial trick to organize your thinking and code. (You'd be working with arrays of coefficients.)
A set only contains unique items. If there are duplicates, then it is no longer a set.
Yes, if you don't want to consider sets, consider the idea of 'factors.' How many factors does:
p1^a1.p2^a2....pn^an
have if p1's are distinct primes. If the ai's are all 1, then the number is 2^n. In general, the answer is (a1+1)(a2+1)...(an+1) as David Nehme notes.
Oh, and note that your answer by hand was wrong, it should be 48, or 47 if you don't want to count the empty set.
You don't need to modify Pascal's Triangle at all. Study C(k,n) and you'll find out -- you basically need to divide the original results to account for the permutation of equivalent letters.
E.g., A B1 B2 C1 D1 == A B2 B1 C1 D1, therefore you need to divide C(5,5) by C(2,2).
Without duplicates (in a set as earlier posters have noted), each element is either in or out of the subset. So you have 2^n subsets. With duplicates, (in a "multi-set") you have to take into account the number the number of times each element is in the "sub-multi-set". If it m_1,m_2...m_n represent the number of times each element repeats, then the number of sub-bags is (1+m_1) * (1+m_2) * ... (1+m_n).
Even though mathematical sets do contain unique items, you can run into the problem of duplicate items in 'sets' in the real world of programming. See this thread on Lisp unions for an example.
Related
I am sure everyone is familiar with the famous 21 matchsticks game where each person picks up 1,2 or 3 matches and the last person to pick up a match loses.
Let's simplify the game and assume that it is only possible to pick 1 or 2 matches. My question is, how many games are possible?
I know this is very easy to solve recursively, however, I am trying to come up with a combinatorial solution.
To provide an example, let's reduce 21 to just 4 matches. The number of possible games would be 5. {'MCM', 'MMMM', 'CC', 'CMM', 'MMC'}. Where C represents removing 2 matches and M represents removing a single match.
Symbolic method allows us to deduce that the generating function for this combinatorial class is
f(z) = 1/(1 - z - z^2 - z^3)
At this point, we can obtain the answer through a power series expansion, e.g. see here. The coefficient on z^21 will give the number of possible games in "21 matchsticks" (it might be 233317).
Looking back, suppose that players were allowed to take one match only. Then, there would be only one possible scenario. For each game length (power of z), there is only one game outcome:
1/(1 - z) = 1*1 + 1*z + 1*z^2 + 1*z^3 + 1*z^4 + 1*z^5 + ...
If players are allowed to take one or two matches, we have multiple scenarios:
1/(1 - z - z^2) = 1*1 + 1*z + 2*z^2 + 3*z^3 + 5*z^4 + 8*z^5 + ...
The coefficients recover the Fibonacci sequence and can be interpreted as a number of integer compositions of n using only numbers 1 and 2.
Allowing for taking one, two or three matches leads to the following expansion,
1/(1 - z - z^2 - z^3) = 1*1 + 1*z + 2*z^2 + 4*z^3 + 7*z^4 + 13*z^5 + ...
which can be found in this OEIS sequence, cordially named the "Tribonacci numbers".
It is possible to arrive at the 233317 answer using pen, paper and a shifted generalization of the Pascal triangle, although I would leave that task to someone else.
As an aside, I highly recommend the book "Analytic Combinatorics" by Philippe Flajolet and Robert Sedgewick for their introduction to the symbolic method and beyond.
I've been tasked with finding the answers for the questions below, but honestly I'm completely lost as where to start. I'm not looking for straight answers per say (although they would be appreciated), but rather how I can find/derive them. I understand recursion, I just don't understand how to find a recurrence equation.
a.) Give the recurrence for the expected running time of RANDOM.
b.) Give the exact recurrence equation for the expected number of recursive calls executed by a call to RANDOM(n).
c.) Give the exact recurrence equation for the expected number of times the rerun statements on line 14 is executed, in all called to RANDOM(n), recursive or not.
Pseudocode:
Function RANDOM(u)
if u = 1 then
return(1)
else
assign x=0 with probability 1/2, or
assign x=1 with probability 1/3, or
assign x=2 with probability 1/6
if x=0 then
return(RANDOM(u-1) + RANDOM(u-2))
end-if
if x=1 then
return(RANDOM(u) + 2*RANDOM(u-1))
end-if
if x=2 then
return(3*RANDOM(u) + RANDOM(u) + 3)
end-if
end-if
end-RANDOM
First of all it is important to note that, since the questions ask for run time / no. of calls, the coefficients in front of the recursive calls to RANDOM don't matter (because none of the answers depend on the actual return value).
Also, since the questions ask for expected quantities, you can mix the appropriate recursive calls probabilistically.
a)
Starting off quite easy. Probabilistic mixing of functions:
T(u) = [1/2] * [T(u-1) + T(u-2)] +
[1/3] * [T(u) + T(u-1)] +
[1/6] * [T(u) + T(u) ] // + constant amount of work
b)
Same as before, but remember to add one for each call:
N(u) = [1/2] * [N(u-1) + N(u-2) + 2] +
[1/3] * [N(u) + N(u-1) + 2] +
[1/6] * [N(u) + N(u) + 2] // no constants here
c)
This is trickier than the other two. It seems contradictory that the question asks for "[all calls to RANDOM(u)] whether recursive or not", but only the ones on line 14 which are recursive...
Anyways ignoring this minor detail, the key thing to note is that the call to RANDOM(u) on line 11 can also produce the required recursive calls, without contributing to the total count itself. Adapting the above:
R(u) = [1/3] * [R(u) ] + // don't add 1 here
[1/6] * [R(u) + R(u) + 2] // add 2 as before
Note that the question probably expects you to rearrange all of the T(u), N(u), R(u) terms to the LHS. I'll leave that to you since it is trivial.
I'm writing a Python script to generate problems for mental arithmetic drills. The addition and multiplication ones were easy, but I'm running into trouble trying to generate unbiased problems for the subtraction ones.
I want to be able to specify a minimum and maximum value that the minuend (first number) will be -- e.g., for two-digit subtraction it should be between 20 and 99. The subtrahend should also have a range option (11-99, say). The answer needs to be positive and preferably also bounded by a minimum of, say, 10 for this situation.
So:
20 < Minuend < 99
11 < Subtrahend < 99
Answer = Minuend - Subtrahend
Answer >= 10
All the numeric values should be used as variables, of course.
I have these conditions met as follows:
ansMin, ansMax = 10, 99
subtrahendMin, minuendMax = 11,99
# the other max and min did not seem to be necessary here,
# and two ranges was the way I had the program set up
answer = randint(ansMin, ansMax)
subtrahend = randint(subtrahendMin, minuendMax - answer)
minuend = answer + subtrahend # rearranged subtraction equation
The problem here is that the minuend values wind up being nearly all over 50 because the answer and subtrahend were generated first and added together, and only the section of them that were both in the bottom 25% of the range will get the result below 50%. (Edit: that's not strictly true -- for instance, bottom 1% plus bottom 49% would work, and percentages are a bad way of describing it anyway, but I think the idea is clear.)
I also considered trying generating the minuend and subtrahend values both entirely randomly, then throwing out the answer if it didn't match the criteria (namely, that the minuend be greater than the subtrahend by a value at least greater than the answerMin and that they both be within the criteria listed above), but I figured that would result in a similar bias.
I don't care about it being perfectly even, but this is too far off. I'd like the minuend values to be fully random across the allowable range, and the subtrahend values random across the range allowed by the minuends (if I'm thinking about it right, this will be biased in favor of lower ones). I don't think I really care about the distribution of the answers (as long as it's not ridiculously biased). Is there a better way to calculate this?
There are several ways of defining what "not biased" means in this case. I assume that what you are looking for is that every possible subtraction problem from the allowed problem space is chosen with equal probability. Quick and dirty approach:
Pick random x in [x_min, x_max]
Pick random y in [y_min, y_max]
If x - y < answer_min, discard both x and y and start over.
Note the bold part. If you discard only y and keep the x, your problems will have an uniform distribution in x, not in the entire problem space. You need to ensure that for every valid x there is at least one valid y - this is not the case for your original choice of ranges, as we'll see later.
Now the long, proper approach. First we need to find out the actual size of the problem space.
The allowed set of subtrahends is determined by the minuend:
x in [21, 99]
y in [11, x-10]
or using symbolic constants:
x in [x_min, x_max]
y in [y_min, x - answer_min]
We can rewrite that as
x in [21, 99]
y = 11 + a
a in [0, x-21]
or again using symbolic constants
x in [x_min, x_max]
y = y_min + a
a in [0, x - (answer_min + y_min)].
From this, we see that valid problems exist only for x >= (answer_min + y_min), and for a given x there are x - (answer_min + y_min) + 1 possible subtrahents.
Now we assume that x_max does not impose any further constraints, e.g. that answer_min + y_min >= 0:
x in [21, 99], number of problems:
(99 - 21 + 1) * (1 + 78+1) / 2
x in [x_min, x_max], number of problems:
(x_max - x_min + 1) * (1 + x_max - (answer_min + y_min) + 1) / 2
The above is obtained using the formula for the sum of an arithmetic sequence. Therefore, you need to pick a random number in the range [1, 4740]. To transform this number into a subtraction problem, we need to define a mapping between the problem space and the integers. An example mapping is as follows:
1 <=> x = 21, y = 11
2 <=> x = 22, y = 12
3 <=> x = 22, y = 11
4 <=> x = 23, y = 13
5 <=> x = 23, y = 12
6 <=> x = 23, y = 11
and so on. Notice that x jumps by 1 when a triangular number is exceeded. To compute x and y from the random number r, find the lowest triangular number t greater than or equal to r, preferably by searching in a precomputed table; write this number as q*(q+1)/2. Then x = x_min + q-1 and y = y_min + t - r.
Complete program:
import random
x_min, x_max = (21, 99)
y_min = 11
answer_min = 10
triangles = [ (q*(q+1)/2, q) for q in range(1, x_max-x_min+2) ]
upper = (x_max-x_min+1) * (1 + x_max - (answer_min + y_min) + 1) / 2
for i in range(0, 20):
r = 1 + random.randrange(0, upper)
(t, q) = next(a for a in triangles if a[0] >= r)
x = x_min + q - 1
y = y_min + t - r
print "%d - %d = ?" % (x, y)
Note that for a majority of problems (around 75%), x will be above 60. This is correct, because for low values of the minuend there are fewer allowed values of the subtrahend.
I can see a couple of issues with your starting values - if you want the answer to always be greater than 10 - then you need to either increase MinuendMin, or decrease SubtrahendMin because 20-11 is less than 10... Also you have defined the answer min and max as 3,9 - which means the answer will never be more than 10...
Apart from that I managed to get a nice even distribution of values by selecting the minuend value first, then selecting the subtrahend value based on it and the answerMin:
ansMin = 10
minuendMin, minuendMax = 20,99
subtrahendMin = 9;
minuend = randint(minuendMin, minuendMax )
subtrahend = randint(subtrahendMin,(minuend-ansMin) )
answer = minuend - subtrahend
You say you've already got addition working properly. Assuming you have similar restrictions for the addends/sum you could rearrange the factors so that:
minuend <= sum
subtrahend <= first addend
answer <= second addend
A similar mapping can be made for multiplication/division, if required.
I have a value, for example 2.8. I want to find 10 numbers which are on an exponential curve, which sum to this value.
That is, I want to end up with 10 numbers which sum to 2.8, and which, when plotted, look like the curve below (exponential decay). These 10 numbers should be equally spaced along the curve - that is, the 'x-step' between the values should be constant.
This value of 2.8 will be entered by the user, and therefore the way I calculate this needs to be some kind of algorithm that I can program (hence asking this on SO not Math.SE).
I have no idea where to start with this at all - any ideas?
You want to have 10 x values equally distributed, i.e. x_k = a + k * b. They shall fulfill sum(exp(-x_k)) = v with v being your target value (the 2.8). This means exp(-a) * sum(exp(-b)^k) = v.
Obviously, there is a solution for each choice of b if v is positive. Set b to an arbitrary value, and calculate a from it.
E.g. for v = 2.8 and b = 0.1, you get a = -log(v / sum(exp(-b)^k)) = -log(2.8/sum(0.90484^k)) = -log(2.8/6.6425) = -log(0.421526) = 0.86387.
So for this example, the x values would be 0.86387, 0.96387, ..., 1.76387 and the y values 0.421526, 0.381412, 0.345116, 0.312274, 0.282557, 0.255668, 0.231338, 0.209324, 0.189404, 0.171380.
Update:
As it has been clarified that the curve can be scaled arbitrarily and the xs are preferred to be 1, 2, 3 ... 9, this is much more simple.
Assuming the curve function is r*exp(-x), the 10 values would be r*exp(-1) ... r*exp(-9). Their sum is r*sum(exp(-x)) = r*0.58190489. So to reach a certain value (2.8) you just have to adjust the r accordingly:
r = 2.8/sum(exp(-x)) = 4.81178294
And you get the 10 values: 1.770156, 0.651204, 0.239565, 0.088131, 0.032422, 0.011927, 0.004388, 0.001614, 0.000594.
If I understand your question correctly then you want to find x which solves the equation
It can be solved as
(just sum numbers as geometric progression)
The equation under RootOf will always have 1 real square different from 1 for 2.8 or any other positive number. You can solve it using some root-finding algorithm (1 is always a root but it does not solve original task). For constant a you can choose any number you like.
After computing the x you can easily calculate 10 numbers as .
I'm going to generalize and assume you want N numbers summing to V.
Since your numbers are equally spaced on an exponential you can write your sum as
a + a*x + a*x^2 + ... + a*x^(N-1) = V
Where the first point has value a, and the second a*x etc.
You can take out a factor of a and get:
a ( 1 + x + x^2 + ... + x^(N-1) ) = V
If we're free to pick x then we can solve for a easily
a = V / ( 1 + x + x^2 + .. x^(N-1) )
= V*(x+1)/(x^N-1)
Substituting that back into
a, a*x, a*x^2, ..., a*x^(N-1)
gives the required sequence
For a given bitpattern, is there a mathematical relationship between that pattern and the mirror image of that pattern?
E.g. we start with, say, 0011 1011. This mirrors to 1101 1100.
We can mechanically mirror the pattern easily enough.
But is there in fact a mathematical relationship between the pattern and its mirror?
Not really an answer, but too long for a comment and should get you started:
Break down the definition of "bit pattern" (really, just the figures that make up a number) to see if there is any relationship.
For a number X representing in base B, its "digits" are the values a_i that make the following equation correct:
X = a_0 + a_1*B + a_2*B^2 + ... a_n*B^n
So for example, in base 10, the number 42 has the following expansion:
42 = 2 + 4*10
a_0 = 2
a_1 = 4
So then let's define the reversal of that number as follows:
X' = a_n + a_(n-1)*B + a_(n-2)*B^2 + ... a_0*B^n
24 = 4 + 2*10 (the reversal of 42)
From this we can easily operate on X and X' to see if there are any interesting relationships. For example,
X+X' = a_n + a_(n-1)*B + a_(n-2)*B^2 + ... a_0*B^n + a_0 + a_1*B + a_2*B^2 + ... a_n*B^n
= (a_0+a_n) + (a_1+a_(n-1))*B + ... + (a_n+a_0)*B^n
So every "digit" in the sum is equal to the original digit's value in X plus the opposite digit from X. That's kind of obvious when you add something like 42 to 24 to get 66, but less obvious if you were add 67 to 76 to get 143.
I think you'll find that there aren't really many interesting relationships of numbers' reversals.