6
$\begingroup$

I am given the following set:

$\qquad \{(x,y)\in\mathbb{R}^2:x^2+y^2\le16 \wedge |y|\gt x\}$

I want find and count the number of points in this set that have integer coordinates.

I drew this to help me visualize the problem.

Plot

ClearAll["Global`*"];
Plot[{y /. Solve[x^2 + y^2 == 16, y], x, -x}, {x, -5, 5}, 
  PlotStyle -> {Solid, Dashed, Dashed}, 
  AspectRatio -> 1, 
  PlotRange -> {{-5, 5}, {-5, 5}}, 
  PlotTheme -> "Detailed", 
  GridLines -> {{-4, -3, -2, -1, 0, 1, 2, 3, 4}, {-4, -3, -2, -1, 0, 1, 2, 3, 4}}]

I want to mark all of those points which are inside the set red as well as count them.

What should I do?

$\endgroup$

3 Answers 3

6
$\begingroup$

Just solve it:

sol = Solve[x^2 + y^2 <= 16 && Abs[y] > x, {x, y}, Integers];

Get how many:

Length@sol

Make the points (set point size or use Disk if you want bigger):

gr = Graphics[{Red, Point[{x, y}] /. sol}];

Then show them together:

Show[{Plot[{y /. Solve[x^2 + y^2 == 16, y], x, -x}, {x, -5, 5}, 
   PlotStyle -> {Solid, Dashed, Dashed}, AspectRatio -> 1, 
   PlotRange -> {{-5, 5}, {-5, 5}}, PlotTheme -> "Detailed", 
   GridLines -> {{-4, -3, -2, -1, 0, 1, 2, 3, 4}, {-4, -3, -2, -1, 0, 
      1, 2, 3, 4}}], gr}]
$\endgroup$
5
$\begingroup$

Here is one way to do it.

grid = Catenate @ CoordinateBoundsArray[{{-4, 4}, {-4, 4}}];
belongsQ[{x_, y_}] := x^2 + y^2 <= 16 && Abs[y] > x
pts = Pick[grid, belongsQ /@ grid];
Length[pts]

34

Plot[{y /. Solve[x^2 + y^2 == 16, y], x, -x}, {x, -5, 5}, 
  PlotStyle -> {Automatic, Dashed, Dashed},
  AspectRatio -> 1,
  PlotRange -> {{-5, 5}, {-5, 5}},
  PlotTheme -> "Detailed",
  PlotLegends -> False, 
  GridLines -> {{-4, -3, -2, -1, 0, 1, 2, 3, 4}, {-4, -3, -2, -1, 0, 1, 2, 3, 4}},
  Epilog -> {Red, AbsolutePointSize[5], Point[pts]}]

plot

$\endgroup$
2
  • $\begingroup$ Was Epilog intended to do only things like this ? $\endgroup$
    – AHB
    Jan 11, 2017 at 12:00
  • $\begingroup$ @AHB. It is certainly much used to do things like this, but I would not say "only". $\endgroup$
    – m_goldberg
    Jan 11, 2017 at 16:18
3
$\begingroup$
plot1 = Plot[{y /. Solve[x^2 + y^2 == 16, y], x, -x}, {x, -5, 5}, 
  PlotStyle -> {Automatic, Dashed, Dashed}, AspectRatio -> 1, 
  PlotRange -> {{-5, 5}, {-5, 5}}, PlotTheme -> "Detailed", 
  GridLines -> {{-4, -3, -2, -1, 0, 1, 2, 3, 4}, {-4, -3, -2, -1, 0, 
     1, 2, 3, 4}}]

This shot works:

pts = {x, y} /. FindInstance[x^2 + y^2 <= 16 && Abs[y] > x, {x, y}, Integers, 100]
 (* 100 was chosen big enough to have all points found *)

plot2 = ListPlot[pts, PlotStyle -> {Red, PointSize[Large]}];

Show[plot1, plot2]

just to pass the 6 char limit

Length@pts

34

$\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.