Title says it all.

How do I count a number of primes(say smaller than N) that does not contain 5?


1 Answer 1


Used Prime[ Range[ PrimePi[n]]] (thanks to Artes suggestion below) to find all primes less than n, then used IntegerDigits on each, and looked for 5 using Cases then used Position to find the numbers themselves. I am sure there is a more efficient way to do this:

list = Prime[Range[PrimePi[n]]];
Cases[#, 5] & /@ (IntegerDigits@list);
p = Position[%, 5][[All, 1]];
list[[p]]  (*primes with 5 *)
(* {5, 53, 59, 151, 157} *)

Length[list] - Length[p] (*primes with no 5 in them)
  • $\begingroup$ Prime[ Range[ PrimePi[n]]] finds all primes <= n. $\endgroup$
    – Artes
    Apr 23, 2014 at 16:51
  • $\begingroup$ @Artes thanks. I knew there must be a better way to find primes less than N. Will update this part to use your method. $\endgroup$
    – Nasser
    Apr 23, 2014 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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