# Evaluating simple algebraic function within restricted range

Okay I get that this should be simple but I'm at my wit's end. Very simple problem:

I have an expression

f[x_] := 5 x + 1


wherein x can be any whole number $0$, $1$, $2$, $3$, $\dots$

I want the list of all possible values up to/including that at the maximum point of the function's allowed range of output.

Obviously we're just incrementing the prior value from and including 1 (when x = 0) until the next incrementation would exceed the maximum value allowed by the restricted range (not the same as the range function in Mathematica of course; I'm talking about the domain/range concepts of functions generally), which I want to be able to assign as a variable (let's say m).

If I were to say the maximum (inclusive) value of the range of the function f is m = 30, then my output should be a list of all values from and including 1, to and including the last that's less than or equal to m = 30, as below. Please help me write the proper code for this.

Expected output: {1, 6, 11, 16, 21, 26} (more than this would exceed 30 so it terminates).

We could use While or Do, but the important point is I can't limit the evaluation based on number of increments because I won't necessarily know how many will be required, only how large a maximal value that the final output must be less than (e.g., m = 30).

• Maybe MaxValue[{5 x + 1, 5 x + 1 <= 30}, x \[Element] Integers] Sep 6 '16 at 20:34
• That would work if I only needed the maximum value, but I need the list of all values within the range in order, so 26 needs to be preceded by 1,6,11,16,21 Sep 6 '16 at 20:37
• I see. Do you want it only over over integers? May I assume that the functions f are not something ill or extremely complex numerically? Sep 6 '16 at 20:39
• f[x_] := 5 x + 1; m = 30; x = 0; Flatten@Rest@Reap[While[f[x] <= m, Sow[f[x]]; x++]] ? Sep 6 '16 at 20:57
• Solve[5 x + 1 == y && 0 <= y <= 30, y, Integers]
– chuy
Sep 6 '16 at 21:57

Let's take the function

f[x_] := 5 x + 1


and set the maximum value m that it is not allowed to exceed:

m = 30;


The domain of x is Integers; Sow and Reap will gather the instances fulfilling the condition f[x]<=m until While is executed:

x = 0;
Flatten@Rest@Reap[While[f[x] <= m, Sow[f[x]]; x++]]


{1, 6, 11, 16, 21, 26}

A different approach would be to find the maximal value of f[x], the Solve to get a corresponding xmax, and finally make a suitable Table:

max = MaxValue[{f[x], f[x] <= m}, x \[Element] Integers]


26

xmax = x /. First@Solve[f[x] == max, x]


5

Table[f[x], {x, 0, xmax}]


{1, 6, 11, 16, 21, 26}

f[x_] = 5 x + 1;

f[x] /. Solve[{f[x] <= 30, x >= 0}, x, Integers]

(*  {1, 6, 11, 16, 21, 26}  *)


You can use ArgMax:

f[x_] := 5 x + 1

f /@ Range[ArgMax[{f[x], f[x] <= 30}, x, Integers]]
(* {6, 11, 16, 21, 26} *)