5
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ f[x_Integer] := x? f[1] will work while f[1.] won't. $\endgroup$
    – Öskå
    Commented Nov 26, 2014 at 17:19
  • $\begingroup$ 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 $\endgroup$ Commented Nov 26, 2014 at 17:20
  • $\begingroup$ 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! $\endgroup$ Commented Nov 26, 2014 at 17:21
  • $\begingroup$ I used this expression but couldn't find min and max of this: (3 n + 4)/(2 n + 1). $\endgroup$
    – Fabio
    Commented Nov 26, 2014 at 17:22
  • $\begingroup$ i = IntegerPart; sol = NMaximize[{(3 i@ n + 4)/(2 i@n + 1), n > 1}, n]; i@n /. sol[[2]] $\endgroup$ Commented Nov 26, 2014 at 17:47

2 Answers 2

1
$\begingroup$

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 *)
$\endgroup$
1
  • $\begingroup$ NMaximize[{(3 IntegerPart@n + 4)/(2 IntegerPart@n + 1), n > 1}, n] // First $\endgroup$
    – Fabio
    Commented Nov 27, 2014 at 12:36
3
$\begingroup$

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]

enter image description here

$\endgroup$

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.