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

Title says it all.

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

• Is this a specific Mathematica-related question? Commented Apr 23, 2014 at 14:43
• Title is ambiguous. Commented Apr 23, 2014 at 14:55
• You could use this to start you off: stackoverflow.com/questions/11814011/… Commented Apr 23, 2014 at 15:20

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:

n=200;
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)
(*41*)

• Prime[ Range[ PrimePi[n]]] finds all primes <= n. Commented Apr 23, 2014 at 16:51
• @Artes thanks. I knew there must be a better way to find primes less than N. Will update this part to use your method. Commented Apr 23, 2014 at 16:54