# Manipulate Slider to take its values from primes

How do I make the slider values in Manipulate be values that are prime numbers? Many thanks for any assistance.

Firstly, many thanks for all your considered and rapid responses. It really is very much appreciated. Here is my Mathematica 'code' to produce some tables that demonstrate a small Lemma about residue classes. In the last section I am seeking that the p-input variable only selects primes. Manipulate[{MatrixForm[Table[x + y p^(r - 1 - t), {x, 0, p^t - 1}, {y, 0, p - 1}]], MatrixForm[Table[Mod[x + y p^(r - 1 - t), p^t], {x, 0, p^t - 1}, {y, 0, p - 1}]], Transpose[Table[Sort[Table[Mod[x + y p^(r - 1 - t), p^t], {x, 0, p^t - 1}, {y, 0, p - 1}][[All, i]]], {i, 1, p}]] // MatrixForm}, {p, 2, 100, 1}, {r, 1, 10, 1}, {t, 0, r - 1, 1}]

Just as a side-bar, sorting the individual matrix columns was difficult for me with no easy global sort function (like Excel) readily available. Cheers.

• Firstly, many thanks for all your considered and rapid responses. It really is very much appreciated. Here is my Mathematica 'code' to produce some tables that demonstrate a small Lemma about residue classes. In the last section I am seeking that the p-input variable only selects primes. – user48633 Jul 31 '16 at 3:00
• If you have version 10+ make use of TrackingFunction, and replace controlling of p with {p, 2, 100, 1, Appearance->"Labeled", TrackingFunction -> (If[PrimeQ[#], p=#]&)}. – BoLe Jul 31 '16 at 7:37

Here is the example from TrackingFunction:

Manipulate[x, {{x, 2}, 0, 100, 1, TrackingFunction -> (If[PrimeQ[#], x = #] &)}]

• You can also interconnect in both directions: Manipulate[{x, y}, {x, 0, 1, TrackingFunction -> (x = #; y = (1 - #); &)}, {y, 0, 1, TrackingFunction -> (y = #; x = (1 - #); &)}] – Bob Hanlon Jul 30 '16 at 14:44
• Combined with the approach by BoLe: Manipulate[p, {{n, 1}, 1, 100, 1, Slider, TrackingFunction :> (n = #; p = Prime[n]; &), Appearance -> "Labeled"}, {{p, Prime[n]}, None}] – Karsten 7. Jul 30 '16 at 18:29
• @Karsten7. Yep. If I were to do it BoLe's way, I would use With: Manipulate[With[{p = Prime[n]}, p], {n, 1, 10, 1}]. But the solution in the docs has a certain cuteness in how the slider jumps from a prime position to the next, if one finds the gaps between primes interesting. And if you open the slider (or set it to be "Labeled"), it displays the prime number, instead the prime's ordinal position, the reasons for wanting it to be one way or the other depending on the application. – Michael E2 Jul 30 '16 at 20:38

Keep slider linear and have another variable.

Manipulate[
DynamicModule[{p = Prime[n]}, p],
{n, 1, 10, 1, Appearance -> "Labeled"}]


Or, specify a setter bar but then force slider.

With[{k = 10},
Manipulate[n,
{n, Prime /@ Range[k], ControlType -> Slider}]]


Or, make a custom interactive structure without Manipulate.

With[{k = 10},
DynamicModule[{x},
Column[{
Slider[Dynamic[x], {Prime /@ Range[k]}],
Dynamic[(a + b)^x]}]]]


Depending in what you actually want with the Manipulate, DynamicModule[] is not really required:

data = Table[i^2 - i, {i, 30}];
Manipulate[
ListPlot[data/Prime[a], PlotLabel -> {"i^2-i/", Prime[a]}], {a, 1, 30, 1}]