# Writing a program that will filter the integer solutions

I've the following equation:

b==(-12 +
Sqrt Sqrt[
3 (-4 + r)^2 + 12 a^2 (-2 + r) - 4 a (-5 + r) (-2 + r) +
4 a^3 (-2 + r)^2] + 3 r)/(6 (-2 + r))


And $$r$$ is a given number and I'll run $$a$$ from $$2$$ to a given number n, which are all integers.

How can I write a code that will give me only an output of $$b$$ when the equation gives an integer back?

For example, when I use $$r=4$$ and use the following code:

Table[Sqrt[a (1 + a) (1 + 2 a)]/Sqrt, {a, 1, 100, 1}]


It will find that $$a=24$$ gives $$b=70$$, so I want to see only that solution.

Maybe I can use the If[] function?

You can use Solve. Your equation:

eqn[r_] := b == (-12 + Sqrt Sqrt[3 (-4 + r)^2 + 12 a^2 (-2 + r) - 4 a (-5 + r) (-2 + r) + 4 a^3 (-2 + r)^2] + 3 r)/(6 (-2 + r))


Using Solve (with $$r=4$$):

Solve[eqn && 1 < a < 100, {a, b}, Integers]


{{a -> 24, b -> 70}}

Using Solve (with $$r=11$$):

Solve[eqn && 1 < a < 100, {a, b}, Integers]


{{a -> 25, b -> 73}}

You might try IntegerQ

Table[b = (-12 +Sqrt Sqrt[3 (-4 + r)^2 + 12 a^2 (-2 + r) - 4 a (-5 + r) (-2+ r) +4 a^3 (-2 + r)^2] + 3 r)/(6 (-2 + r)) /. r -> 4;
If[IntegerQ[b], {b, a}, Nothing], {a, 2, 50}]
(*{{70, 24}}*)


You can use Cases here:

b[r_, a_] := (-12 + Sqrt Sqrt[3 (-4 + r)^2 + 12 a^2 (-2 + r) - 4 a (-5 + r) (-2 + r) + 4 a^3 (-2 + r)^2] + 3 r)/(6 (-2 + r));


With r=4

Cases[Table[b[4, a], {a, 2, 100}], _Integer]


{70}

If you want to retrieve {a,b} pairs:

Cases[Table[{a, b[4, a]}, {a, 2, 100}], {_, _Integer}]


{{24, 70}}

It's interesting to look at {a,b} points for a bunch of r values:

t = Cases[
Table[Cases[Table[{a, b[r, a]}, {a, 2, 100}], {_, _Integer}], {r, 3, 20}], Except[{}]];

ListPlot[t, AxesLabel -> {a, b}] 