I would like to write a function that outputs the built in function PrimePi, which gives the number of primes π(x) less than or equal to x. So far, I have:
enter[n_] := Count[[2, n], Prime]
Any suggestions?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI think the simplest working variation of your code is
enter[n_] := Count[Range[2, n], _?PrimeQ]
The function Count
takes a pattern (look it up in the help). In this case, I'm matching anything that PrimeQ
recognises as a prime.
For
,AppendTo
, andPrint
. More proper Mathematica code would be something likeenter[n_Integer?Positive] := Range[n] // PrimeQ // Boole // Total
. Please have a look at this tutorial and the most common pitfalls awaiting new users. $\endgroup$PrimeQ
you could start by implementing the Sieve of Eratosthenes and then count the number of integers left over. $\endgroup$[2,n]
is invalid syntax, the second argument ofCount
must be a pattern, etc. Go through these, look upCount
in the docs, and ask about the specific problem where you got stuck (e.g. not sure how to write a correct pattern). $\endgroup$