Last time I did any maths was A-Level (some time ago!).

This is a programming/layout problem where I need to display 7 items across a page, with 6 gaps between them. I have a fixed width, but need to determine values of x and y that are integers where x > y.

This 'feels' like something I could plot on a graph, hence solve with calculus but I need a pointer in the right direction. Ideally I'd like to solve it programatically but I need an understanding of how to solve it first. Further down the line, I'd like to be able to vary the number of items to get different values.



3 Answers 3


First, find one solution. Here you can say 700 = 7*100 + 6*0, or 700 = 700*1 = 700*(7-6) = 7*700 + 6*(-700).

One you have one solution $(x_1,y_1)$, another solution $(x_2,y_2)$ has to satisfy $7x_1+6y_1 = 700 = 7x_2+6y_2$, so $7(x_1-x_2) + 6(y_1-y_2) = 0$.

Here this means that $7(x_1-x_2) = 6(y_2-y_1)$. Since 6 and 7 are coprime, this number is a multiple of 42, so $7(x_1-x_2) = 6(y_2-y_1) = 42k$.

Now you get $x_2 = x_1 - 6k, y_2 = y_1+7k$, and that gives you the set of solutions $S = \{(100-6k, 0+7k), k \in \mathbb{Z}\}$. If you only want to have solutions with positive $0<y<x$, this means you get constraints on $k$ : $0 < k$ and $k < 100/13$, which means $k \in \{1, 2, \ldots 7 \}$

Edit :

If you want to solve $a = nx + (n-1)y$, since $n$ and $n-1$ are coprime, the same method works : start with the particular solution $a = a*1 = n*a + (n-1)*(-a)$, so $(x=a, y= -a)$. the solutions in $\mathbb{Z}^2$ are $\{(x=a-k(n-1), y= kn-a)), k \in \mathbb{Z}\}$. Then you write the constraints $0<y<x$ in terms of $k$ and you get $a/n < k < 2a/(2n-1)$

  • $\begingroup$ Thanks Chandok, I'm looking to solve this programatically. Any ideas how I might set this up? Ultimately I want to solve the equation totalwidth = n(imagewidth) + (n-1)(gapwidth). I will always know totalwidth and n, just need to determine the values of imagewidth and gapwidth. $\endgroup$
    – Jeepstone
    Feb 25, 2011 at 10:08
  • $\begingroup$ have you looked at this question math.stackexchange.com/questions/20906/… ? $\endgroup$
    – mercio
    Feb 25, 2011 at 10:09

If you want integer solutions, you have a linear diophantine equation. You can start by observing that $y$ must be a multiple of $7$, then simplify the equation using this.


$$700 = 7x + 6y\implies y = \frac{-7(x - 100)}{6}$$ Experimentation in a spreadsheet shows that [integer] $x,y$ values increase/decrease by amounts corresponding to their opposite coefficients. For example, a valid $x$-value occurs only every $6$ integers and a resulting $y$-value occurs every $7$ integers. Here is a sample to show the effect.

$$(-14,133)\quad (-8,126)\quad (-2,119)\quad (4,112)\quad (10,105)\quad (16,98)\quad $$

A little math shows the predictable values and relationships to be as follows.

$$x = 6 n + 4 \qquad y = 112 - 7 n \qquad n \in\mathbb{Z}$$

Now, if we have a known $x$-range, we can find $n$ by plugging $x$-lo and $x$-hi into $$n = \frac{(x - 4)}{6}$$ and rounding up or down to get the integer values desired and needed.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .