Problem similar to Coin change problem with finite coins, but with intervallic coin values and fixed number of coins - recursion

I have been struggling to create an algorithm, for a problem where we get an array of n coins with their max values. A coin in the array can have a value between 0 and this max value. We want to determine ALL permutations of possible order of coins given a sum target x. For example the array {2,2} with x = 3 has 2 permutations: 1+2, 2+1. I know this problem is related to coin change/knapsack/ sub-set problem. But haven't been able to make an algorithm based on these known solutions. I've tried doing dynamic programing, but all I could conclude that the array of possible targets is some kind of binomial. For example the array of coins with max values {3,3,3}, (n=3) can be any sum value from 0-9, where the number of permutations are:
sum value : permutations
0:1
1:3
2:6
3:10
4:12
5:12
6:10
7:6
8:3
9:1
From doing this I could atleast conclude that the the number of permutations for value 0 and max sum is always 1 and for value 1 and value max-1 is always n (length of the array). But I could not for the life of me figure out a recursion for the rest of the numbers, which is why I need help. Thanks in advance.

Related

Mathematics, combinatorics. What is the maximum number of variations?

There's this line.
X1_X2_X3_X4_X5_X6
It is known that each variable X* can take values from 0 to 100. The sum of all X* variables is always equal to 100. How many possible string variants can be created?
Suppose F(n,s) is the number of strings with n variables, and the variables sum to s, where each variable is between 0 and 100, and suppose s<=100. You want F(6,100).
Clearly
F(1,s) = 1
If the first variable is t, then it can be followed by strings of n-1 variables that sum to s-t. Thus
F(n,s) = Sum{ 0<=t<=s | F(n-1, s-t) }
So its easy to write a wee function to compute the answer.

How to obtain the maximum sum of the array with the following condition?

Suppose the problem posed is as follows:
On Mars there lives a colony of worms. Each worm is represented as elements in an 1D array. Worms decide to eat each other but any worm can eat only its nearest neighbour. Each worm has a preset amount of energy(i.e the value of the element). On Mars, the laws dictate that when a worm i with energy x eats another worm with energy y, the i-th worm’s final energy becomes x-y. A worm is allowed to have negative energy levels.
Find the maximum value of energy of the last standing worm.
Sample data:
0,-1,-1,-1,-1 has answer 4.
2,1,2,1 has answer 4.
What will be the suitable logic to address this problem?
This problem has a surprisingly simple O(N) solution.
If any two members in the array have different signs, the answer is then sum of absolute values of all elements.
To see why, imagine a single positive value in the array, all other elements are negative (Example 1). Now the best strategy would be keeping this value positive and gradually eating all neighbors away to increase this positive value. The position of the positive value doesn't matter. The strategy is same in case of a single negative element.
In more general case, if an array of size N have values of different signs, we can always find an array of size N-1 with different signs, because there must be a pair of neighbors with different sign, which we can combine to form a number of any sign we prefer.
For example with this array : [1,2,-5,4,-10]
we can combine either (2,-5) or (4,-10). Lets combine (4,-10) to get [1,2,-5,-14]
We can only take (2,-5) now. So our array now is : [1,-7,-14]
Again only (1,-7) possible. But this time we have to keep combined value positive. So we are left with: [8,-14]
Final combining gives us 22, sum of all absolute values.
In case of all values with same sign, our first move would be to produce an opposite sign combining a neighbor pair with as little "cost" as possible. Intuitively, we don't want to waste two big numbers on this conversion. If we take x,y neighbor pair, when combined the new value (of opposite sign) will be abs(x-y). Since result is simply sum of absolute values, we can interpret it as - "loosing" abs(x) and abs(y) from maximum possible output and "gaining" abs(x-y) instead. So the "cost" for using this pair for sign conversion is abs(x)+abs(y)-abs(x-y). Since we need to minimise this cost, we choose from initial array neighbor pair that have lowest such value.
So if we take the above array but now all values are positive [1,2,5,4,10]:
"cost" of converting (1,2) to -1 is 1+2-abs(-1)=2.
"cost" of converting (2,5) to -3 is 2+5-abs(-3)=4.
"cost" of converting (5,4) to -1 is 5+4-abs(-1)=8.
"cost" of converting (4,10) to -6 is 4+10-abs(-6)=8.
So, we take and convert pair (1,2) to -1. Then just sum absolute values of resultant array to get 20. Notice that this value is exactly 2 less than our previous example.

What's the probability that the distinct number appear in a size-k subset of n distinct number?

What's the probability that the distinct number appear in a size-k subset of n distinct numbers?
Let A be our target number, S be some the size-k subset of [1,2,3....n].
What's the probability that A is the one of k numbers in S? Many thanks.
PS: I can draw a condition tree diagram, and find the answer maybe the k/n.
But how can I think about it? Thanks again.
The probability is indeed, as you mentioned, k/n. Think about is this way: Let x be an element of [1,2,...,n]. There are binom(n,k) subsets of size k in total, and there are binom(n-1,k-1) subsets of size k that contain x (because x is chosen and we need to choose another k-1 elements). Hence the probability of x to be contained in S is binom(n-1,k-1)/binom(n,k)=k/n.

Stirling numbers of the second kind with multisets

I was looking at [Stirling numbers of the second kind], which are the total number of ways to split a set of length n into k non-empty subsets, where order does not matter.(http://mathworld.wolfram.com/StirlingNumberoftheSecondKind.html), and was wondering how to write a non-naive algorithm to compute
S(n, k {occurences of each element})
Where
S(6, 3, {1, 2, 3} )
would give the total number of ways a set with 6 elements in which 3 are the same element and a different 2 are another element (and 1 is its unique element) could be split into 3 non-empty sets, ignoring permutations.
There is a recursive formula for regular Stirling numbers of the second kind S(n, k), but unlikely to be a comparable function for multisets.
So what's an algorithm that could calculate this number?
Relevant question on Math.SE here, without a real method to calculate this number.

how do I generate 2 random prime numbers that when multiplied, yield a number with X bits? (X given as argument))

I lack the math skills to make this function.
basically, i want to return 2 random prime numbers that when multiplied, yield a number of bits X given as argument.
for example:
if I say my X is 3 then a possible solution would be:
p = 2 and q = 3 becouse 2 * 3 = 6 (110 has 3 bits).
A problem with this statement is that it starts by asking for two "random" prime numbers. Without any explicit statement of the distribution of the required random primes, we are already stuck. (This is the beginning of a classic paradox, where we are asked to generate a "random" integer.)
But suppose that we change the statement to finding any two arbitrary primes, that yield the desired product with a given number of bits x. The answer is trivial.
The set of numbers that have exactly x bits in their binary representation is the half open set of integers [2^(x-1),2^x-1].
Choose an arbitrary prime number that is less than or equal to (2^x-1)/2. Call it p1.
Next, choose a second prime number that lies in the interval (2^(x-1)/p1,(2^x-1)/p1). Call it p2.
It must be true that p1*p2 will be in the desired interval.
For example, given x = 10, so the product must lie in the interval [512,1023], the set of integers with exactly 10 bits. (Note, there are apparently 147 such numbers in that interval, with exactly two prime factors.)
Step 1:
Choose p1 as any prime no larger than 1023/2 = 511.5. I'll pick p1 = 137. Then the second prime factor must be a prime that lies in the interval
[512 1023]/137
ans =
3.7372 7.4672
thus either 5 or 7.
dec2bin(137*[5 7])
ans =
1010101101
1110111111
If you know the number of bits, you can generate a number 2^(x-2) < x < 2^(x-1). Then take the square root and find the closest primes on either side of it. Multiplying them together will, in most cases, get you a number in the correct range. If it's too high, you can take the two primes directly on the lower side of it.
pseudocode:
x = bits
primelist[] = makeprimelist()
rand = randnum between 2^(x-2) and 2^(x-1)
n = findposition(primelist, rand)
do
result = primelist[n]*primelist[n+1]
n--
while result > 2^(x-1)
Note that numbers generated this way will allways have '1' as the highest significant bit, so would be possible to generate a number of x-1 bits and just tack the 1 onto the end.

Resources