1

I'm looking for a code that finds the number of primes p less than or equal to X satisfying p is congruent to 1 (mod 4) and another code that finds the number of primes p less than or equal to X satisfying p is congruent to 3 (mod 4).

Karam
  • 197
  • 5

2 Answers2

3
Select[Range[100],And[PrimeQ[#],Mod[#,4]==1]&]
(* {5,13,17,29,37,41,53,61,73,89,97} *)

Select[Range[100],And[PrimeQ[#],Mod[#,4]==3]&]
(* {3,7,11,19,23,31,43,47,59,67,71,79,83} *)
user293787
  • 11,833
  • 10
  • 28
  • One can observe that PrimePi4m1[x]+PrimePi4m3[x]+1==PrimePi[x]. This is the first step to make it more efficient. Another step would be harnessing PrimePi in a more extended way, something like it was done here. – Artes Oct 16 '22 at 13:34
  • Thank you. This code is really just a translation of an English sentence into Mathematica (more or less). There is certainly room for improvement if efficiency is a concern. – user293787 Oct 16 '22 at 13:59
  • I’m not sure how to make this code work since it’s giving me the primes and not the number of primes. – Karam Oct 16 '22 at 16:36
  • 1
    You can apply Length to the result. – user293787 Oct 16 '22 at 16:43
3

Define a function that takes in a range and a criterion function.

fPrime[X_?NumericQ, f_Function] := {Length@#, #} &@
  Table[If[f@Prime[i], Prime[i], Nothing], {i, PrimePi[X]}]

Usage

fPrime[100, Mod[#, 4] == 1 &]

{11, {5, 13, 17, 29, 37, 41, 53, 61, 73, 89, 97}}

fPrime[100, Mod[#, 4] == 3 &]

{13, {3, 7, 11, 19, 23, 31, 43, 47, 59, 67, 71, 79, 83}}

Syed
  • 52,495
  • 4
  • 30
  • 85