28
5
Introduction
The Prime Counting Function, also known as the Pi function \$\pi(x)\$, returns the amount of primes less than or equal to x.
Challenge
Your program will take an integer x which you can assume to be positive, and output a single integer equal to the amount of primes less than or equal to x. This is a code-golf challenge, so the winner will be the program with the fewest bytes.
You may use any language of your choosing provided that it existed before this challenge went up, but if the language has a built-in prime-counting function or a primality checking function (such as Mathematica), that function cannot be used in your code.
Example Inputs
Input:
1
Output:
0
Input:
2
Output:
1
Input:
5
Output:
3
3What about other prime-related functions? For example, "next prime" funciton – Luis Mendo – 2016-09-23T20:09:29.637
6what about prime factorization functions? – Maltysen – 2016-09-23T20:13:00.940
4Welcome to Programming Puzzles and Code Golf! – Adnan – 2016-09-23T20:35:08.217
6
As Adnan said, welcome to PPCG! For future challenges, let me recommend the Sandbox where you can post a challenge to get meaningful feedback and critique before posting it to the main site.
– AdmBorkBork – 2016-09-23T20:44:02.027I think this is what @TheBikingViking meant to link to: Related
– mbomb007 – 2016-09-23T20:52:42.947The "no primality checking functions" kept me from posting this elegant Oasis solution:
np+U
– Zwei – 2016-09-23T23:05:59.213@mbomb007 Thanks, that was what I meant to link to. I've deleted my comment to prevent confusion. – TheBikingViking – 2016-09-24T18:08:06.180
Can I use isprime ? – RosLuP – 2018-01-18T16:46:39.360
@roslup isprime was explicitly banned. – Pavel – 2018-01-18T16:54:35.080