0

I need to find the number of total possible different solution to an equation with more than one variable.

For example:

$$ 1x + 2y + 8z = 13 $$

What is the total number of different combinations for the values of $x$, $y$ and $z$? I can't think of an algorithm to solve this. I don't need the answers printed, just the total number of different combinations! The coeficients will always be positive, so are the variables and the final number too.

$x, y ,z$, and possibly more are always positive integers (or $0$)!


Guys, the question above is not what I really wanted to ask, you can find it better formulated bellow. I tried to solve it with this code here, and it did not work, it returns 647 instead of the expected 415 for the inputs ({1,2,8}, 13):

public static int getProbability(int[] distances, int n){ if(n==distances[0]) return 1; else if(n<distances[0]) return 0; else { int returnInt = 0; for (int i=0 ;i<distances.length; i++){ returnInt += getProbability(distances, n-distances[i]); } if (Arrays.binarySearch(distances, n) != -1) { returnInt = returnInt+1; } return returnInt; } }

  • $x,y,z \in \mathbb{N}, \mathbb{Z}, \mathbb{R}$ ? – Jean Marie Aug 21 '16 at 21:14
  • 2
    Assuming $x,y,z$ are all non-negative integers and you are asking for number of solutions to a Diophantine equation like this, you can approach via generating functions quite easily. The number of solutions to $x+2y+8z=13$ is the coefficient of $a^{13}$ in the expansion of $(1+a+a^2+a^3+\dots)(1+a^2+a^4+a^6+\dots)(1+a^8+a^{16}+\dots) = \frac{1}{1-a}\cdot\frac{1}{1-a^2}\cdot\frac{1}{1-a^8}$ which we see to be $10$. – JMoravitz Aug 21 '16 at 21:19
  • If $x,y,z$ are able to be real numbers, rational numbers, negative integers, etc... there are very clearly going to be infinitely many solutions. – JMoravitz Aug 21 '16 at 21:21
  • @JMoravitz, I might have modelled the problem wrongly then, because the correct answer for this particular case is 415.

    The quesiton is: Given the length 13, and N possible movements (3 in this case, movements of length 1, 2 and 8). What is the total number os possibilities to get to the total length ? So in this case, a 2, 2, 2, 2, 2,2 ,1 is different then 1, 2, 2, 2, 2, 2, 2.

    – Felipe Ribeiro R. Magalhaes Aug 21 '16 at 21:22
  • 1
    It is possible that $415$ is an answer to some question somewhere, but it is definitely not the answer to how many non-negative integer solutions there are to $x+2y+8z=13$. This related question shows an answer dealing with Ehrhart Polynomials. As an aside, the generating function method agrees with the answer given for that question. – JMoravitz Aug 21 '16 at 21:37
  • Ah, the question where the order of terms matters is a different question indeed. The question you posted above is instead where order is completely irrelevant. You are instead asking in how many ways you can have a $13\times 1$ grid filled with monominos ($1\times 1$), dominos ($2\times 1$) and octominos ($8\times 1$). Approach instead via a recurrence relation. $a_n = a_{n-1}+a_{n-2}+a_{n-8}$. – JMoravitz Aug 21 '16 at 21:41
  • @JMoravitz and how could I solve that? Didn't get that.. Know if Java can natively do something to help? – Felipe Ribeiro R. Magalhaes Aug 21 '16 at 22:02

1 Answers1

2

As mentioned in the comments above, the number of solutions to $x+2y+8z=13$ is ten. From the comments, we learn that the real question being asked is the following:


In how many ways can we tile a $13\times 1$ grid with monominos ($1\times 1$ tiles) dominos ($2\times 1$ tiles) and octominos ($8\times 1$ tiles)?

(I.e. how many ways can we partition the number $13$ into parts of size $1,2$ and $8$ respectively where order of parts matters)

We form a recurrence relation: Letting $a_n$ be the number of arrangements for a length $n$ grid, we can break $a_n$ up based on the length of the furthest right tile. If the furthest right tile were a monomino, then there are still $n-1$ spaces to be filled which can be done in $a_{n-1}$ ways. If it were a domino, there are still $n-2$ spaces to be filled which can be done in $a_{n-2}$ ways. If it were an octomino, there are still $n-8$ spaces to be filled which can be done in $a_{n-8}$ ways.

This gives the recurrence relation $a_n = a_{n-1}+a_{n-2}+a_{n-8}$.

We need to find initial conditions to proceed. $a_1=1$ as the only arrangement is a single monomino.

$a_2=2$ as it could be two monominos (1+1) or it could be a single domino (2)

$a_3 = 3$ as it could be three monominos (1+1+1), a monomino then a domino (1+2), or a domino followed by a monomino (2+1)

From earlier example, we know that the sequence begins $a_1=1,a_2=2,a_3=3,a_4=5,a_5=8,a_6=13,a_7=21$ as until we have the opportunity to place the octomino this exactly matches the problem where we have only monominos and dominos which we know to follow the fibonacci sequence.

We then have $a_8=35$ ways to arrange the length eight grid (as there are $34$ ways to do it without any octominos and exactly one way to do it with)

We can then continue the sequence via brute force by hand, by computer, by linear algebra (but factoring the degree eight polynomial to get a closed form solution will be trouble) etc...

$\begin{array}{|r|l|l|}\hline a_0&1\\ a_1&1&1+0+0\\ a_2&2&1+1+0\\ a_3&3&2+1+0\\ a_4&5&3+2+0\\ a_5&8&5+3+0\\ a_6&13&8+5+0\\ a_7&21&13+8+0\\ a_8&35&21+13+1\\ a_9&57&35+21+1\\ a_{10}&94&57+35+2\\ a_{11}&154&94+57+3\\ a_{12}&253&154+94+5\\ a_{13}&415&253+154+8\\ \hline\end{array}$


Alternatively, we could break apart based on if there is an octomino or not, and where it is. Given that the solution to the length $n$ problem with only monominos and dominos is $F(n+1)$ where $F(n)$ represents the $n$'th fibonacci number, we get:

$a_{13} = F(14)+F(1)F(6)+F(2)F(5)+F(3)F(4)+F(4)F(3)+F(5)F(2)+F(6)F(1) = 415$

JMoravitz
  • 79,518
  • Wow, this will be really hard to implement in Java. And the problem is that I can't code for this specific case, there will be more test cases, with different possible moves! – Felipe Ribeiro R. Magalhaes Aug 21 '16 at 22:09
  • @FelipeRibeiroR.Magalhaes If you have multiple $b$-ominos ($b\times 1$ tiles) each of different size, say $b_1,b_2,b_3,\dots,b_k$ in ascending order, then the recurrence relation will be $a_n = a_{n-b_1}+a_{n-b_2}+a_{n-b_3}+\dots$. You will need to bruteforce search the first $b_k$ terms in the sequence, but from there you can easily generate more terms using recursion. – JMoravitz Aug 21 '16 at 22:12
  • didn't get this fibonacci approach, and can it be used for any problem like this or just in this particular case ? And I also didn't get up to where do I have to brute force, and then procede with the recursion? – Felipe Ribeiro R. Magalhaes Aug 21 '16 at 23:04
  • @Felipe The fibonacci approach at the end was just for this specific case, but you could possibly find similar patterns for other cases using tribonacci or other similar sequences. In order to succesfully use recursion you need a number of initial conditions. If you are okay with using negative numbers, the initial conditions could be $a_n = 0$ for all $n<0$, $a_0=1$ and start the recurrence from there. That should work fine. The point being is that if we talk about $a_{n}=a_{n-1}+a_{n-2}+a_{n-8}$ we want to know what we mean by each term on the right in order to understand the left. – JMoravitz Aug 21 '16 at 23:09
  • I don't know if you are familiar with coding here (I come from Stackoverflow), but I came up with this solution, which doesn't seem to be right (returns 647 instead of 415), what could be wrong? – Felipe Ribeiro R. Magalhaes Aug 21 '16 at 23:51
  • public static int getProbability(int[] distances, int n){ if(n==distances[0]) return 1; else if(n<distances[0]) return 0; else { int returnInt = 0; for (int i=0 ;i<distances.length; i++){ returnInt += getProbability(distances, n-distances[i]); } if (Arrays.binarySearch(distances, n) != -1) { returnInt = returnInt+1; } return returnInt; } } – Felipe Ribeiro R. Magalhaes Aug 21 '16 at 23:51
  • Actually I'll edit the main question to post the code, will be much more legible. – Felipe Ribeiro R. Magalhaes Aug 21 '16 at 23:52
  • @Felipe I am less familiar with the syntax used than I would like to be. Specific help with coding and syntax is best asked elsewhere. Given an input array of distances ${a,b,\dots,k}$ in ascending order and desired total $n$, the pseudo-code I would recommend is: create an array of length equal to the largest "distance" and populate it all with zeroes. Make the $k$'th (largest distance) entry a $1$. For $i=1..n$ append a new entry to the array in slot $k+i$ and set it equal to $\sum\limits_{d\in\text{distances}}\text{array}[k+i-d]$. Output $\text{array}[k+n]$ – JMoravitz Aug 22 '16 at 00:11