# Optimization problems over the Integers

Should be an easy question. How can I define a function which gets only integers in a form that I can use to find max and min for instance?

Example:

Find the Max of the expression (3 n + 4)/(2 n + 1) where n ∈ Integers

• f[x_Integer] := x? f[1] will work while f[1.] won't.
– Öskå
Nov 26, 2014 at 17:19
• You can use f[x_Integer]:=... but that won't help you to solve diophantine eqs in the general case. Please show what kind of problems you need to solve Nov 26, 2014 at 17:20
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Nov 26, 2014 at 17:21
• I used this expression but couldn't find min and max of this: (3 n + 4)/(2 n + 1). Nov 26, 2014 at 17:22
• i = IntegerPart; sol = NMaximize[{(3 i@ n + 4)/(2 i@n + 1), n > 1}, n]; i@n /. sol[[2]] Nov 26, 2014 at 17:47

Diophantine problems are tough and there is no silver bullet. In your example this works:

i = IntegerPart;
sol = NMaximize[{(3 i@ n + 4)/(2 i@n + 1), n > 1}, n];
i@n /. sol[[2]]

(* 1 *)

• NMaximize[{(3 IntegerPart@n + 4)/(2 IntegerPart@n + 1), n > 1}, n] // First Nov 27, 2014 at 12:36

Bounding the range of n resolves the issue with Maximize

Maximize[{(3 n + 4)/(2 n + 1), Element[n, Integers], -100 <= n <= 100}, n]


{4, {n -> 0}}

Or,

Maximize[{(3 n + 4)/(2 n + 1), -100 <= n <= 100}, n, Integers]


{4, {n -> 0}}

Any large value for the range bound will work since

Limit[(3 n + 4)/(2 n + 1), n -> #] & /@ {Infinity, -Infinity}


{3/2, 3/2}

DiscretePlot[
(3 n + 4)/(2 n + 1),
{n, -10, 10},
PlotRange -> All]