Consider this problem:

Find all $x,y \in \mathbb{N}$ with $\{ x,y \} \leq 100$, $y$ is odd, and

$$\frac{1}{x} + \frac{4}{y} = \frac{1}{12} .$$

One can convert the condition on $y$ to be $y = 2 z + 1$ for $0 \leq z \leq 49$ and get the answer indirectly:

Solve[{0 <= x <= 100, 0 <= z <= 49,
  1/x + 4/(2 z + 1) == 1/12},{x,z},

{{x -> 76, z -> 28}}

and then convert $z$ back to find $y$. (There is a unique solution given the constraint.)

My question, though, is how to solve the equation directly with that oddness constraint. The obvious approach:

Solve[{0<x<=100, 0<=y<=100, OddQ[y],
  1/x + 4/y == 1/12},{x,y},

and straightforward variations do not work.

The closest I could get was

Solve[{0<x<=100, 0<=y<=100,
  1/x + 4/y == 1/12},{x,y},

which gives many answers plus a warning that some of the solutions do not obey the assumption. (Indeed, all but one of the solutions so violate the assumption.)

I hope there is a direct method, not one which relies on numerous intermediate solutions, of which I computationally select the one(s) that satisfy the constraint.

This works, but is not quite what I seek:

Solve[{0<x<=100, 0<=y<=100,Mod[y,2]==1,
  1/x + 4/y == 1/12},{x,y},

My full question involves several such constraints, e.g., some variables are even, some variables are odd...

  • $\begingroup$ FindInstance[ 1/x + 4/y == 1/12 && Mod[y, 2] == 1 && x <= 100 && y <= 100, {x, y}, PositiveIntegers, 10^10] You can't use OddQ and must use Mod because 'Q' style functions don't work with symbolic arguments like that e.g OddQ[y] is False if y is a symbol with no value. $\endgroup$
    – flinty
    Jul 13, 2021 at 21:06
  • 1
    $\begingroup$ In your Assuming[OddQ[y],... above, for the reasons I stated, this is first changed to Assuming[False,... which would explain the warnings. $\endgroup$
    – flinty
    Jul 13, 2021 at 21:08
  • $\begingroup$ @GeorgeVarnavides: Thanks. The constraints I will impose in my true problem are generated elsewhere and come in long lists of OddQ, EvenQ, PositiveQ, and conjunctions and disjunctions thereof. I suppose I could try to express all these in standard equation form, but I was hoping to avoid that. $\endgroup$ Jul 13, 2021 at 21:09
  • $\begingroup$ @flinty: Thanks, but FindInstance isn't quite right because my full (large) equation may have multiple solutions (obeying the constraints). Your explanation about how OddQ won't work with symbolic arguments is a big help... thanks for that insight. $\endgroup$ Jul 13, 2021 at 21:11
  • $\begingroup$ FindInstance returns multiple solutions so I don't see why that's a problem (see the last argument). You cannot use 'Q' functions. Another way to express without using Mod is FractionalPart[y/2] > 0 but that's just more long winded. $\endgroup$
    – flinty
    Jul 13, 2021 at 21:12

1 Answer 1


Here is one way. Admittedly, would like to understand better why this works, and surely there must be a more elegent way.

p = Solve[{0 < x <= 100, 0 <= y <= 100, 1/x + 4/y == 1/12}, {x, y}, Integers];
q = OddQ[y /. p];
p[[Flatten[Position[q, True]]]]
  • $\begingroup$ Weird and inelegant... but helpful ($+1$). It would be truly ugly in my full problem where my constraints are of the form OddQ[y] OR (EvenQ[x] AND NonNegative[y])... $\endgroup$ Jul 13, 2021 at 21:34
  • $\begingroup$ This is just filtering out the odd $y$ solutions. You could do both those lines in a single Select[p, OddQ[y /. #] &] $\endgroup$
    – flinty
    Jul 13, 2021 at 21:35
  • $\begingroup$ @DavidG.Stork, Hahaha, LoL. Glad it was (somewhat) helpful. $\endgroup$
    – mjw
    Jul 13, 2021 at 21:53
  • $\begingroup$ @flinty, that is exactly what I was trying to do, but did not have the necessary vocabulary. Anyway, seems that it is easier for Mathematica to filter out some types of constraints rather than embed them in the problem statement. $\endgroup$
    – mjw
    Jul 13, 2021 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.