0
$\begingroup$

I have two rational points. They are denoted below in black and they define a line segment. Find integer points on this segment.

a = {45/2, 0}; b = {45/3, -45/3};
Plot[2*x - 45, {x, 0, 23}, GridLines -> {Range[23], -Range[30]}, 
 AspectRatio -> 1, PlotRange -> {{0, 23}, {-23, 0}}, 
 Epilog -> {PointSize[Large], Point[{a, b}], Red, PointSize[Medium],
   Point[Table[{i, 2*i - 45}, {i, 15, 22}]]
   }]

It happens that I know the solutions, and they are denoted as red dots. The question is how to get them programmatically, not only in this but in all cases, for instance a = {21, 0}; b = {21, 10/5}.

line segment on a grid

Notice that unlike this post, i) the given points are rational, and ii) all solutions are required, not just positive.

$\endgroup$
3
  • 1
    $\begingroup$ I don't see where the problem is ... FindInstance, Reduce and Solve all work as expected: Solve[2*x - 45 == y && x >= b[[1]] && x <= a[[1]], {x, y}, Integers]. $\endgroup$
    – Domen
    Commented Aug 27 at 9:46
  • $\begingroup$ What do you mean by modifying the solutions? I posted a screenshot of the result as given by Mathematica (14.1). $\endgroup$
    – Domen
    Commented Aug 27 at 9:57
  • $\begingroup$ Obviously the conditions were x > 0 && y > 0 since the OP wanted to find coordinates (which) are positive integer. You don't have this condition, so you have to restrict the coordinates to your bounds. $\endgroup$
    – Domen
    Commented Aug 27 at 10:03

1 Answer 1

3
$\begingroup$

Following the answer by Yves Klett on a practically identical question, this is easily computed via Solve (or any related equation-solving functions), you only have to be careful when you have a vertical line (ie. $x_1 = x_2$):

findIntegerSolutions[{x1_, y1_}, {x2_, y2_}] := Module[{line, x, y},
  line = If[x1 == x2, x == x1, y - y1 == (y2 - y1)/(x2 - x1) (x - x1)];
  SolveValues[line && {x} ∈ Interval[{x1, x2}] && {y} ∈ Interval[{y1, y2}], 
   {x, y}, Integers]
  ]

findIntegerSolutions[{45/2, 0}, {45/3, -45/3}]
(* {{15, -15}, {16, -13}, {17, -11}, {18, -9}, {19, -7}, {20, -5}, 
    {21, -3}, {22, -1}} *)

findIntegerSolutions[{21, 0}, {21, 10/5}]
(* {{21, 0}, {21, 1}, {21, 2}} *)

Or an even simpler solution, as given by @ydd:

findIntegerSolutions[a_, b_] := 
   Module[{x}, SolveValues[x ∈ Line[{a, b}], x, Integers]]
$\endgroup$
3
  • $\begingroup$ Am I misunderstanding something or is just findIntegerSolutions[a_, b_] := SolveValues[x ∈ Line[{a, b}], x, Integers] not sufficient? $\endgroup$
    – ydd
    Commented Aug 27 at 21:27
  • $\begingroup$ @ydd, you're not missing anything :) You can probably add this also as an answer to the original post. $\endgroup$
    – Domen
    Commented Aug 28 at 6:52
  • $\begingroup$ oh cool. I'll credit you for the x localization because I probably should've included that. $\endgroup$
    – ydd
    Commented Aug 28 at 14:07

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.