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?
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?
I 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. – Roman May 31 '19 at 08:49PrimeQyou could start by implementing the Sieve of Eratosthenes and then count the number of integers left over. – Roman May 31 '19 at 09:15[2,n]is invalid syntax, the second argument ofCountmust be a pattern, etc. Go through these, look upCountin the docs, and ask about the specific problem where you got stuck (e.g. not sure how to write a correct pattern). – Szabolcs May 31 '19 at 13:06