# A Difficult Application of Modular Arithmetic

Looking to input a $p$ for the first line:

p=6;


Build a list of primes from the 3rd to $p-1$:

plist=Table[Prime[n],{n,3,p-1}];


Then build a list as follows:

bmod=Outer[Mod,{3*Prime[p]},plist]


Here's the code so far with the output:

p=6;
plist=Table[Prime[n],{n,3,p-1}];
bmod=Outer[Mod,{3*Prime[p]},plist]

{{4,4,6}}


Now I'd like to multiply each element of the list by $2$, but keep them mod their original prime, so instead of:

{{8,8,12}}


I get:

{{3,1,1}}


Then, I'd like to add this list to the original, still keeping the modular bounds:

{{3,1,1}}
+
{{4,4,6}}
=
{{2,5,7}}


And then repeat this process several times, always adding to the previous line:

{{4,4,6}}
{{2,5,7}}
{{0,6,8}}
{{3,0,9}}
etc.


Assuming you've followed my so far, how would I do that?

• Your explanation doesn't seem to match your numbers. Why is the 6 not doubled to 12 in the first step? How does the 2 in {2,5,1} go to 0? 2*2 + 4 is not 0 mod 5. – wxffles Apr 15 '16 at 1:55
• @wxffles Should be fixed now. Feel free to give it a once-over. I can be quite mistake prone. – Elem-Teach-w-Bach-n-Math-Ed Apr 15 '16 at 3:27

Starting with

p = 6;
plist = Table[Prime[n], {n, 3, p - 1}]
bmod = Outer[Mod, {3*Prime[p]}, plist] // Flatten
(* {5, 7, 11} *)
(* {4, 4, 6} *)


the first result is obtained by

Thread[Mod[2 bmod, plist]]
(* {3, 1, 1} *)


To obtain the next results, use

NestList[Thread[Mod[# + bmod, plist]] &, %, 5]
(* {{3, 1, 1}, {2, 5, 7}, {1, 2, 2}, {0, 6, 8}, {4, 3, 3}, {3, 0, 9}} *)


which adds bmod to the previous result, modulo the corresponding prime in plist.

To test whether any of the results immediately above contain 2 or 4, as requested in a comment, use
MemberQ[#, (2 | 4)] & /@ %

• Hey, you've already been immensely helpful, but would you mind one more thing? I need to apply "$MemberQ$" to each individual list in that final line to test if either 2 or 4 are elements of the list. Looking for an output of "False, True, True, False, True, False" but I'm just getting the single output of "True". Thanks for helping out a newb! =) – Elem-Teach-w-Bach-n-Math-Ed Apr 15 '16 at 4:10
• @Elem-Teach-w-Bach-n-Math-Ed I must not understand your last comment. There are three elements in every sublist. In any case, you probably should apply Map to the list to apply whatever criterion you wish to each sublist. I can take a look tomorrow morning, if you like. – bbgodfrey Apr 15 '16 at 4:14