1
$\begingroup$

How can I write a code in Mathematica to solve an equation with variable in specific domains? For instance, I want to write a code for

Solve[{2 x + 1 == y && x > 0 && y > 0}, {x, y}, Integers] // Column

with extra conditions that $x\in\{1,5,8,17,31,111\}$. How can I put this condition inside the command Solve[ ] among other conditions?

$\endgroup$
  • 2
    $\begingroup$ When the list is a finite set, could just make an equation: In[51]:= xlist = {1, 5, 8, 17, 31, 111}; Solve[{2 x + 1 == y && x > 0 && y > 0 && Apply[Times, x - xlist] == 0}, {x, y}, Integers] Out[52]= {{x -> 1, y -> 3}, {x -> 5, y -> 11}, {x -> 8, y -> 17}, {x -> 17, y -> 35}, {x -> 31, y -> 63}, {x -> 111, y -> 223}} $\endgroup$ – Daniel Lichtblau Apr 19 '18 at 14:20
3
$\begingroup$

One way is to insert all the equalities directly:

set = {1, 5, 8, 17, 31, 111};
Solve[{2 x + 1 == y && x > 0 && y > 0 && 
    Or @@ (Equal[x, #] & /@ set)}, {x, y}, Integers] // Column
$\endgroup$
  • $\begingroup$ thanks, Do you think it is possible to use something such as "Element" or "Member" here instead of " Or @@ (Equal[x, #] & /@ set)"? $\endgroup$ – asad Apr 19 '18 at 13:25
  • $\begingroup$ I don't know, It doesn't seem to work in Solve as well as in Reduce. Here is another variant of the same direct approach: Solve[{2 x + 1 == y && x > 0 && y > 0 && Times@@(x-set)==0, {x, y}, Integers] // Column $\endgroup$ – Andrew Apr 19 '18 at 13:43
  • $\begingroup$ thanks for your quick and new answer! $\endgroup$ – asad Apr 19 '18 at 14:18
2
$\begingroup$

Alternatively,

xlist = {1, 5, 8, 17, 31, 111};

Table[{x, y} /. Solve[2 x + 1 == y && y > 0, y, Integers][[1]],{x, xlist}]

(* {{1, 3}, {5, 11}, {8, 17}, {17, 35}, {31, 63}, {111, 223}} *)

EDIT: Or more simply,

{#, 2 # + 1} & /@ xlist

(* {{1, 3}, {5, 11}, {8, 17}, {17, 35}, {31, 63}, {111, 223}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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