I was solving below question in Automaton theory

$$L=\{a^m|m=3x+5y; x,y\geq 0\}$$ then the number of states in minimal dfa is?

What I observed is that $3x+5y$ generated every number after $7(8,9,10,11,12 ------)$ and is there any property that I can use to see why this is so or at least could someone provide source from which I can learn more about this

I put the title "$ax+by$ when a,b are relatively prime" because I thought this might be due to 3,5 are relatively prime (correct me if I am wrong) , I don't know anything about this except that it might be related to number theory.

  • 1
    $\begingroup$ Here's a MathJax tutorial :) $\endgroup$
    – Shaun
    Jan 10, 2018 at 14:06
  • 2
    $\begingroup$ Check out this post. Robjohn's answer in particular gets to the point. I refrain from voting to close this as a duplicate, because I don't know if that answers the question about a minimal dfa (whathever that is). Anyway, you were right in that the gcd-condition is crucial. I'm sad to say the answerers got it wrong given that they failed to take notice of the condition $x,y\ge0$. $\endgroup$ Jan 10, 2018 at 14:27
  • $\begingroup$ @JyrkiLahtonen Thank you so much. Yes,this is what I was looking for you may close this question $\endgroup$
    – manifold
    Jan 10, 2018 at 14:33
  • 1
    $\begingroup$ If $k=5x+3y$ then $k+3=5x+3(y+1)$. So $8=5\cdot 1+3\cdot 1, 9=5\cdot 0+3\cdot 3, 10=5\cdot 2+3\cdot 0$ means that every integer greater than $7$ can be written as $5x+3y.$ $\endgroup$ Jan 10, 2018 at 14:33
  • 2
    $\begingroup$ This is a famous question, sometimes known as the Coin problem: en.wikipedia.org/wiki/Coin_problem $\endgroup$ Jan 10, 2018 at 14:40

5 Answers 5


You are asking about The coin problem, which is reasonably well-studied. I encourage you to check out the Wiki article I linked there.

The "Frobenius" of a set of numbers is the smallest one that you can't make as a sum of the numbers in the set - in a land with 3-penny pieces and 5-penny pieces only, you can't pay 7 pennies, but you can pay any amount larger than that.

The Frobenius of a two number set $\{m,n\}$ is given by the formula $F=mn-m-n$. In this case, $15-3-5=7$.

How do we know that every number greater than $7$ is accessible? Here's a not-too-technical proof: If you have enough $5$'s, you can "add $1$" to your total by getting rid of a $5$ and replacing it with two $3$'s $(1=-5+6)$. If you have enough $3$'s, you can add $1$ to your total by getting rid of three of them and replacing them with two $5$'s $(1=-9+10)$.

Thus, if you have at least one $5$ or at least three $3$'s in your sum expressing $n$, then you can also express $n+1$. For numbers over $7$, there is always at least one $5$ or at least three $3$'s in its composition. This is particularly clear for numbers $\ge 15$, and it can be seen with a little more work, for the numbers $8,9,\ldots,14$.

  • $\begingroup$ Thanks,especially for not-too-technical proof :) $\endgroup$
    – manifold
    Jan 10, 2018 at 15:05

This is a consequence of Bezout's Identity, which implies that for $a,b$ relatively prime, there are $\hat{x},\hat{y}$ such that $a\hat{x} + b\hat{y} = 1$. Then for any integer $n$, we choose $x = n\hat{x}$ and $y= n\hat{y}$ and we have $ax + by = n$.

These $x,y$ aren't necessarily $\geq 0$. However, we can determine a lower bound for which we can find a solution with $x,y \geq 0$. We can always adjust a solution by changing $x$ to $x \pm b$ and $y$ to $y \mp a$. If we can guarantee that some value $x = n\hat{x} \pm mb$ satisfies $0 \leq x\leq \frac{n}{a}$, the corresponding value of $y$ will be nonnegative. This is possible if $\frac{n}{a} \geq b-1$. Similarly, we can guarantee a nonnegative solution if $\frac{n}{b} \geq a -1$. So If $n \geq ab - \max\{a,b\}$, we can guarantee a nonnegative solution.

Clearly this isn't always the best lower bound; in your case this gives $n \geq 10$, while you've showed it possible for $n \geq 8$.

EDIT: As Jyrki Lahtonen pointed out in the comments, robjohn does a much better job doing what I was trying to do (and he actually gets the best bound) in his answer here. For an answer without Bezout's identity, see my other answer to this question, which gets the best bound.


Here's an easier way to see this:

Let $b>a$. Consider the sequences of numbers:

  • $0$, $a$, $2a$, $3a$, $\dots$
  • $b$, $b+a$, $b+2a$, $b+3a$, $\dots$
  • $2b$, $2b+a$, $2b+2a$, $2b+3a$, $\dots$
  • $\dots$
  • $(a-1)b$, $(a-1)b+a$, $(a-1)b+2a$, $(a-1)b+3a$, $\dots$

The first row contains integers congruent to $0 \mod a$, the second row congruent to $b \mod a$, etc. Since $b$ and $a$ are coprime, each row represents a different modulus, and the $a$ rows exhaust all moduli $\mod a$.

Now every number has some modulus $\mod a$, and a number appears in some row iff it is $\geq$ the lowest number in that row. Thus all numbers $\geq (a-1)b$ appear somewhere, as I showed in my other answer.

We can now improve on this bound. The number $(a-1)b -a = ab - b - a$ does not appear because it has modulus $-b$ and is less than $(a-1)b$. However, $(a-1)b-a > (a-1)b - b = (a-2)b$, so all numbers greater than $ab - b - a$ with modulus other than $-b$ appear. Since the next number with modulus $-b$ is $(a-1)b$, which appears, all numbers greater than $ab - b - a$ appear.

So if $n \geq ab - b - a + 1 = (a-1)(b-1)$, we can write $n = ax + by$ with $x,y \geq 0$.


A slightly magic proof.

Lemma: If $a,b$ are relatively prime positive integers, then for any integer $n$ there is solution to $ax+by=n$ with $x,y$ integers and $0\leq x\leq b-1.$ (So, possibly with $y<0$.)

Proof: We know we can find integers $x_0,y_0$ so that $ax_0+by_0=n.$

By the division algorithm, we can find integers $q,r$ so that $x_0=bq+r$ with $0\leq r\leq b-1.$ Then:

$$ar + b(y_0+aq)=a(r+bq)+by=ax_0+by_0=n$$

So there is a solution $(x,y)=(r,y_0+aq)$ with $0\leq r=x\leq b-1.$

Theorem: If $a,b$ are relatively prime positive integers, then for each integer $n$, there is a non-negative integer solution to at least on of the equations $ax+by=n$ or to $ax+by=ab-a-b-n.$

(There is a stronger statement, which is that for each $n$, exactly one of those two equations has a solution.)

In your case, this would mean for each $n$, one of $3x+5y=n$ and $3x+5y=7-n$ has a non-negative solution.

Proof: Solve $ax_0+by_0=n$ with integers $x_0,y_0$ and $0\leq x_0\leq b-1.$

If $y_0\geq 0$ we are done.

If $y_0<0$ then:

$$ab-a-b-n = a(b-1)-a-b-(ax_0+by_0)=a(b-1-x_0)+b(-1-y_0)$$

Since $0\leq b-1-x_0$ and $0\leq -1-y_0$ we either have non-negative solutions ot $ax+by=n$ or to $ax+b=ab-a-b-n.$

Now, if $N>ab-a-b$, or $N\geq (a-1)(b-1)$, then there there can't be non-negative solutions to $ax+by=ab-a-b-N<0,$ so there must be a solution to $ax+by=N.$

The stronger version of the theorem is that, for any integer $n,$ exactly one of the equations $$\begin{align}ax+by&=n\\ax+by&=ab-a-b-n\end{align}$$

has a non-negative solution.

You can show this by showing that $ax+by=ab-a-b$ has no non-negative integer solution $(x,y)$. This is because if both the equations have non-negative solutions, you could add them to get a non-negative solution for $ax+by=ab-a-b.$

If $ax+by=ab-a-b$ then $a(x+1)+b(y+1)=ab.$ So $b\mid a(x+1)$ and $a\mid b(y+1).$ But since $a,b$ are relatively prime, we get that $b\mid x+1$ and $a\mid y+1.$

But if $x,y\geq 0,$ $x\geq b-1$ and $y\geq a-1$, and thus $ax+by\geq 2ab-a-b>ab-a-b,$ reaching a contradiction.

This lets us count the number of non-negative integers $n$ for which there is no solution:


A generating function approach is to ask which coefficients of:


are $0$. We can write:


The denominator contributes only additional multiples of $ab$ to the exponents, and since we know that all sufficiently large values have a solution, this means that the exponents in the numerator must cover all the values modulo $ab$, which means that they must all have coefficient $1$. If $S$ is the set of positive values $n$ for which no $ax+by=n$, we can write the numerator as:

$$\sum_{k=0}^{ab-1} x^k -\left(1-x^{ab}\right)\sum_{k\in S} x^k$$

So, one way to enumerate the numbers in $S$ is:

$$S=\{ax+by-ab\mid 1\leq x\leq b-1, 1\leq y\leq a-1, ax+by\geq ab\}$$

We can see exactly half the pairs $x,y$ have $ax+by>ab$ beacuse if $ax+by>ab$ then $a(b-x)+b(a-y)=2ab-(ax+by)<ab$. (Not that there is no $x,y$ with $ax+by=ab$ since $x\leq b-1,y\leq a-1$.) So again we get that there must be $\frac{(a-1)(b-1)}{2}$ elements of $S$.

We can count $S$ another way and get the result: $$\sum_{x=1}^{b-1}\left\lfloor \frac{ax}{b}\right\rfloor=\frac{(a-1)(b-1)}{2}.$$

Now, our generating function is: $$f(x)=\frac{\frac{x^{ab}-1}{x-1}+\left(x^{ab}-1\right)\sum_{k\in S} x^k}{(1-x^{ab})^2}=\frac{1}{(x-1)(x^{ab}-1)}-\frac{\sum_{k\in S} x^k}{1-x^{ab}}$$

The coefficient of $x^n$ in the left part is $1+\left\lfloor \frac{n}{ab}\right\rfloor$ the coefficient of $x^n$ in the right term is $1$ if $n$ is congruent to some $s\in S$ modulo $ab$. So the number of non-negative solutions to $ax+by=n$ is:

$$\left\lfloor \frac{n}{ab}\right\rfloor+\begin{cases}0&n\equiv s\pmod{ab} \text{ for some } s\in S\\ 1&\text{otherwise}\end{cases}$$

The case when $b=2a-1$ gives us an easy way to list $S$:

$$S=\{i+aj\mid 1\leq i\leq a-1, 0\leq j\leq 2(a-i-1)\}$$

This gives, for $a=3,b=5$ that $S=\{1,4,7,2\}.$

For $a=5,b=9$ this gives $S=\{1,6,11,16,21,26,31,2,7,12,17,22,3,8,13,4\}.$

More generally, if $a<b$, we can list, for each $i\in\{1,\dots,a-1\}$ the values $i+aj$ until we get one divisible by $b$.

For example, $a=3,b=10$ gives:


If $a<b$ and $ax\equiv -1\pmod {b}$ for $1\leq x\leq b-1$, then you get:

$$S=\{i+bj\mid 1\leq i\leq a-1, 0\leq j<(ix\bmod b)\}$$

If $b=ax+1$ then $0\leq j< xi$. For example, when $a=4,b=13,x=3,$ you get: $$S=\{1,1+4,1+4\cdot 2,\\ 2,2+4,\dots,2+4\cdot 5,\\ 3,3+4,\dots,3+4\cdot 8\}$$

If $b=ay-1$ for $y>1,$ then $x = b-y$ and for $i$ you get $0\leq j< b-iy.$

This formula also lets you count $S$ again, since the number of elements for $i$ is $ix\bmod b$ and the number of elements for $a-i$ is $(a-i)x\bmod b$, and $xi+(a-i)x=ax\equiv -1\pmod b$. Since $(xi\bmod b)+((a-i)x\bmod b)\leq 2b-2$, we must have the total for these $i,a-i$ to be $b-1$, so the average over all $i$ must be $\frac{b-1}{2}$, and we get, again, that $|S|=(a-1)\frac{(b-1)}{2}.$

  • $\begingroup$ See also this answer for a geometric view of the lemma. $\endgroup$ Oct 10, 2018 at 21:19

Indeed, as you write, since $3$ and $5$ are relatively prime, there are integers $x, y$ such that $3x + 5y = 1$ (this is Bézout's identity).

In particular, this means that, to generate an integer $k$, you have the pair $(kx, ky)$, so that

$$3kx + 5ky = k.$$

  • $\begingroup$ but for x,y >= 0 there is no solution for 3x+5y=1 $\endgroup$
    – manifold
    Jan 10, 2018 at 14:19

You must log in to answer this question.

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