Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Problem regarding binomials from programming, formula derivation.?
Lets say I have the following matlab code for illustration:
n = 0;
For w = 1:5
For x = w + 1:6
For y = x + 1:7
For z = y + 1:8
n = n + 1;
A(n,:) = [w x y z];
End
End
End
End
The result of A will produce sort of like a combinatorial matrix with 8C4 or 70 rows. By having this matrix, we can see that for any given row number n, we can refer to the matrix and obtain the values w,x,y,z for that row.
The problem is, if given a random row number between 1 to 70, can we find the values of w,x,y,z for that particular row without referring to the matrix? Meaning is there a formula or method that is able to input row number,n, and outputs values w,x,y,z?
----------------------------------------------------------------------------------------------------------------------------
Extended Problems
The previous solution must be able to accommodate changes in the code.
An extension to this problem would be:
1) The solution must be able to cope if more variables/for-loops are added.
2) The solution must be able to cope if the limits for each variable changes, EG: 5,6,7,8 becomes 57,58,59,60
One condition is set, that is, the limits must always be consecutive with increments of one. For example in the code, w-5, x-6, y-7, z-8, are consecutive with increments of one.
Example
Example:
Given row number, 1, the result should give w = 1, x = 2, y = 3, z = 4.
Given row number, 28, the result should give w = 1, x = 4, y = 5, z = 8.
For me it is possible to generate matrix A as in the example by referring to the row number and obtaining the solution, but when dealing with huge combinations like 100C6, it consumes too much memory and processing power just to generate the matrix, ideally I'm trying to find a method that doesn't require the matlab code above.
1 Answer
- 6 years ago
Hello, I have created a function for you that can generate the nth row for you, If you have any question feel free to PM me.
function [Vector] = aCb(n,a,b)
j = a+1-b;
Matrix = ones(b,j);
factor = 1;
Temporary = [0:j-1];
Addition = [0:j-1];
for i = b:-1:1
Matrix(i,:) = Matrix(i,:)*factor + Addition;
Addition = [0 cumsum(Temporary(end:-1:2))];
Temporary = cumsum(Temporary);
factor = Matrix(i,end);
end
Matrix = [zeros(b,1) , Matrix];
T2 = Matrix(i,:);
constants(b)=0;
for i = 1:b
constants(i) = find(T2<n,1,'last');
if i==b;break;end
T = Matrix(i+1,:) - Matrix(i+1,constants(i));
T(T<0) = 0;
T2 = T2(constants(i)) + T;
end
Vector = constants + [0:b-1];