0
$\begingroup$

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!

$\endgroup$
2
$\begingroup$
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 *)
$\endgroup$
  • $\begingroup$ 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! $\endgroup$ – LightningStrike Apr 5 '18 at 20:00
  • $\begingroup$ @user57401 The pair # (Slot) and & (Function) is a way to define a function. (Look up Function and Slot in the documentation.) $\endgroup$ – Henrik Schumacher Apr 5 '18 at 21:05
1
$\begingroup$

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]]
$\endgroup$
1
$\begingroup$
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]
$\endgroup$
  • $\begingroup$ The first approach produces values beyond Prime[100] $\endgroup$ – Bob Hanlon Apr 5 '18 at 23:10
  • $\begingroup$ You're right. I edited it in a dirty way. $\endgroup$ – OkkesDulgerci Apr 6 '18 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.