# Select Elements from a List which are of the form

I have this problem:

Produce a list of those of the first 100 primes which are of the form 8k + 1 for an integer k.

So far I know how to create a list of the first 100 Primes, simply using;

Prime[Range[100]]


I have been trying to use the Select command to return the value which match the criteria:

Select[Prime[Range[100]],{criteria}]


But I do not know how to express my criteria for this to work!

How would I return the prime numbers of the form 8k+1 where k is an Integer?

Any help is appreciated!

sol = Select[Prime[Range[100]], Element[(# - 1)/8, Integers] &]

(* {17, 41, 73, 89, 97, 113, 137, 193, 233, 241, 257, 281, 313, 337, 353, 401, \
409, 433, 449, 457, 521} *)


The corresponding values of k are

kValues = k /. Solve[8 k + 1 == #, k, Integers][[1]] & /@ sol

(* {2, 5, 9, 11, 12, 14, 17, 24, 29, 30, 32, 35, 39, 42, 44, 50, 51, 54, 56, 57, \
65} *)

Verifying,

(8 # + 1 & /@ kValues) == sol

(* True *)

• Perfect! I've only started using Mathematica. I'm guessing the # allows for the selection of the element from the original list of prime values? Thank you! Commented Apr 5, 2018 at 20:00
• @user57401 The pair # (Slot) and & (Function) is a way to define a function. (Look up Function and Slot in the documentation.) Commented Apr 5, 2018 at 21:05

Another way, slightly faster than Bob Hanlon's because it uses vectorization (by using Mod), would be

sol = With[{list = Prime[Range[n]]}, Pick[list, Mod[list, 8], 1]]

list = TakeWhile[8 # + 1 & /@ Range@100, # <= Last@Prime@Range@100 &];
Pick[list, PrimeQ@list]


{17, 41, 73, 89, 97, 113, 137, 193, 233, 241, 257, 281, 313, 337, 353, 401, 409, 433, 449, 457, 521}

Intersection[Prime[Range[100]], 8 # + 1 & /@ Range@100]

• The first approach produces values beyond Prime[100] Commented Apr 5, 2018 at 23:10
• You're right. I edited it in a dirty way. Commented Apr 6, 2018 at 0:02