I'll comment on why your initial attempts failed and attempt to correct them. Others have done a better job suggesting better approaches.
(* Fails -- second argument of Select needs to be a function *)
Select[Table[Prime[n], {n, 1, 100}] , Mod[n, 8] == 1]
In the first argument, you generated a list of the first 100 primes (as desired). But the second argument of Select
needs to be a function. As David Baghdasaryan noted in the comments and David G Stork cleaned abbreviated in his answer, simply making the second argument a pure function works:
(* Works -- as suggested by David G Stork *)
Select[Prime[Range[100]], Mod[#,8] == 1&]
(* Fails -- list of primes constructed improperly *)
Select[Array[Prime, Prime[Range[100]]], Mod[#, 8] == 1 &]
This correctly passes a function in the second argument, but the list of primes is constructed improperly. Actually, if you replaced Array
with Table
, this would have worked, but Array
has different syntax. The second argument of Array
always specifies the length of the list. You passed a list of the first 100 primes, so Array
attempted to construct a list with 100 nestings.
Actually, it would be fairly terse to write
(* Works *)
Select[Array[Prime, 100], Mod[#, 8] == 1 &]
which works similarly to David G Stork's method with marginally fewer characters.
(* Partially succeeds-- list of primes only partially constructed *)
Select[Array[Prime, PrimePi[100]], Mod[#, 8] == 1 &]
PrimePi[100]
returns the number of primes less than, which is 25, so this method is equivalent to writing Select[Array[Prime, 25], Mod[#, 8] == 1 &]
. As you noted, this is not what you want.
Select[Table[Prime[n], {n, 1, 100}], Mod[#, 8] == 1 &]
I'm not sure but maby this $\endgroup$Pick[#, Mod[#, 8], 1] &@Prime@Range@100
$\endgroup$Select[8 Range[100] + 1, PrimeQ]
$\endgroup$