[Question]
  [
## Introduction
The [Prime Counting Function](http://mathworld.wolfram.com/PrimeCountingFunction.html), 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](/questions/tagged/code-golf "show questions tagged '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`
[A000720 - OEIS](http://oeis.org/A000720)
  
[Answer]
# [05AB1E](http://github.com/Adriandmen/05AB1E), 3 bytes
```
!fg
```
This assumes that factorization built-ins are allowed. [Try it online!](http://05ab1e.tryitonline.net/#code=IWZn&input=NQ)
### How it works
```
!    Compute the factorial of the input.
 f   Determine its unique prime factors.
  g  Get the length of the resulting list.
```
[Answer]
## Python 2, 45 bytes
```
f=lambda n,k=1,p=1:n/k and p%k+f(n,k+1,p*k*k)
```
Uses the [Wilson's Theorem](https://codegolf.stackexchange.com/a/27022/20260) prime generator. The product `p` tracks `(k-1)!^2`, and `p%k` is 1 for primes and 0 for nonprimes.
[Answer]
# [MATL](http://github.com/lmendo/MATL), ~~11, 10, 8~~, 5 bytes
```
:pYFn
```
[Try it online!](http://matl.tryitonline.net/#code=OnBZRm4&input=MTU)
I wrote a version that had a really cool explanation of how MATL's matrices work:
[`:YF!s1=1`](http://matl.tryitonline.net/#code=OllGIXMxPTE&inputc=MTU)
But it's no longer relevant. Check out the revision history if you want to see it.
New explanation:
```
:p      % Compute factorial(input)
  YF    % Get the exponenents of prime factorization
    n   % Get the length of the array
```
*Three bytes saved thanks to [Dennis's genius solution](https://codegolf.stackexchange.com/a/94364/31716)*
[Answer]
# [Jelly](http://github.com/DennisMitchell/jelly), ~~8~~ 5 bytes
*3 bytes saved thanks to @Dennis!*
```
RÆESL
```
[Try it online!](http://jelly.tryitonline.net/#code=UsOGRVNM&input=&args=NQ)
Port of [DJMcMayhem's MATL answer](https://codegolf.stackexchange.com/a/94354/36398) (former version) refined by Dennis. 
```
R          Range of input argument
 ÆE        List of lists of exponents of prime-factor decomposition
   S       Vectorized sum. This right-pads inner lists with zeros
    L      Length of result
```
[Answer]
# MediaWiki templates with [ParserFunctions](https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions), 220 + 19 = 239 bytes
```
{{#ifexpr:{{{2}}}+1={{{1}}}|0|{{#ifexpr:{{{3}}}={{{2}}}|{{P|{{{1}}}|{{#expr:{{{2}}}+1}}|2}}|{{#ifexpr:{{{2}}} mod {{{3}}}=0|{{#expr:1+{{P|{{{1}}}|{{#expr:{{{2}}}+1}}|2}}|{{P|{{{1}}}|{{{2}}}|{{#expr:{{{2}}}+1}}}}}}}}}}}}
```
To call the template:
```
{{{P|{{{n}}}|2|2}}}
```
Arranged in Lisp style:
```
{{#ifexpr:{{{2}}} + 1 = {{{1}}}|0|
    {{#ifexpr:{{{3}}} = {{{2}}} |
        {{P|{{{1}}}|{{#expr:{{{2}}} + 1}}|2}} |
            {{#ifexpr:{{{2}}} mod {{{3}}} = 0 |
                {{#expr:1 + {{P|{{{1}}}|{{#expr:{{{2}}} + 1}}|2}} |
                {{P|{{{1}}}|{{{2}}}|{{#expr:{{{2}}} + 1}}}}}}}}}}}}
```
Just a basic primality test from 2 to *n*. The numbers with three braces around them are the variables, where `{{{1}}}` is *n*, `{{{2}}}` is the number being tested, `{{{3}}}` is the factor to check.
[Answer]
# Perl, 33 bytes
Includes +1 for `-p`
Give the input number on STDIN
`primecount.pl`
```
#!/usr/bin/perl -p
$_=1x$_;$_=s%(?!(11+)\1+$)%%eg-2
```
Gives the wrong result for `0` but that's OK, the op asked for support for positive integers only.
[Answer]
# Retina 0.8.2, ~~31~~ 22 bytes
Byte count assumes ISO 8859-1 encoding.
```
.+
$*
(?!(..+)\1+$).\B
```
[**Try it online**](https://tio.run/##K0otycxL/P9fT5tLRYtLw15RQ09PWzPGUFtFUy/G6f9/QwMA) - Input much larger than 2800 either times out or runs out of memory.
### References:
[Martin's range generator](https://codegolf.stackexchange.com/a/58682/34718)
[Martin's prime checker](https://codegolf.stackexchange.com/a/57618/34718)
[Answer]
## PowerShell v2+, 98 bytes
```
param($n)if($j='001'[$n]){}else{for($i=1;$i-lt$n){for(;'1'*++$i-match'^(?!(..+)\1+$)..'){$j++}}}$j
```
*Caution:* This is **slow** for large input.
Basically the unary-based lookup from [Is this number a prime?](https://codegolf.stackexchange.com/a/57636/42963), coupled with a `for` loop and a `$j++` counter. A little additional logic on the front to account for edge cases input `1` and `2`, due to how the fenceposting works in the `for` loops.
[Answer]
# [Jelly](http://github.com/DennisMitchell/jelly), 3 bytes
```
!Æv
```
[Try it online!](http://jelly.tryitonline.net/#code=IcOGdg&input=&args=)
### How it works
```
!Æv  Main link. Argument: n
!    Compute the factorial of n.
 Æv  Count the number of distinct prime factors.
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~13 11 10 9 8 7~~ 6 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
**Using no built-in prime functions whatsoever**  
-1 byte thanks to @miles (use a table)  
-1 byte thanks to @Dennis (convert from unary to count up the divisors)
```
ḍþḅ1ċ2
```
**[TryItOnline](http://jelly.tryitonline.net/#code=4biNw77huIUxxIsy&input=&args=MTA)**  
Or see the first 100 terms of the series `n=[1,100]`, also at **[TryItOnline](http://jelly.tryitonline.net/#code=4biNw77huIUxxIsyCsKzw4figqw&input=)**
How?
```
ḍþḅ1ċ2 - Main link: n
 þ     - table or outer product, n implicitly becomes [1,2,3,...n]
ḍ      - divides
  ḅ1   - Convert from unary: number of numbers in [1,2,3,...,n] that divide x
                             (numbers greater than x do not divide x)
    ċ2 - count 2s: count the numbers in [1,2,3,...,n] with exactly 2 divisors
                   (only primes have 2 divisors: 1 and themselves)
```
[Answer]
# JavaScript (ES6), ~~45~~ 43 bytes
```
f=(n,x=n)=>n>1&&(--x<2)+(n%x?f(n,x):f(n-1))
```
A modification of my ~~36~~ ~~35~~ 33-byte primality function (1 byte saved by @Neil, 2 by @Arnauld):
```
f=(n,x=n)=>n>1&--x<2||n%x&&f(n,x)
```
(I can't post this anywhere because [Is this number a prime?](https://codegolf.stackexchange.com/q/57617/42545) only accepts full programs...)
### Test snippet
```
f=(n,x=n)=>n>1&&(--x<2)+(n%x?f(n,x):f(n-1))
```
```
<input type="number" oninput="console.log('f('+this.value+') is '+f(this.value))" value=2>
```
[Answer]
# [05AB1E](http://github.com/Adriandmen/05AB1E), 5 bytes
Assumes that prime factorization builtins are allowed.
Code:
```
LÒ1ùg
```
Explanation:
```
L      # Get the range [1, ..., input]
 Ò     # Prime factorize each with duplicates
  1ù   # Keep the elements with length 1
    g  # Get the length of the resulting array
```
Uses the **CP-1252** encoding. [Try it online!](http://05ab1e.tryitonline.net/#code=TMOSMcO5Zw&input=NQ)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
Ḷ!²%RS
```
This uses only basic arithmetic and Wilson's theorem. [Try it online!](http://jelly.tryitonline.net/#code=4bi2IcKyJVJT&input=&args=MTAw) or [verify all test cases](http://jelly.tryitonline.net/#code=4bi2IcKyJVJTCsW8w4figqxH&input=&args=MSwgMiwgNQ).
### How it works
```
Ḷ!²%RS  Main link. Argument: n
Ḷ       Unlength; yield [0, ..., n - 1].
 !      Factorial; yield [0!, ..., (n - 1)!].
  ²     Square; yield [0!², ..., (n - 1)!²].
    R   Range; yield [1, ..., n].
   %    Modulus; yield [0!² % 1, ..., (n - 1)!² % n].
        By a corollary to Wilson's theorem, (k - 1)!² % k yields 1 if k is prime
        and 0 if k is 1 or composite.
     S  Sum; add the resulting Booleans.
```
[Answer]
# C# 5.0 78 77
```
int F(int n){int z=n;if(n<2)return 0;for(;n%--z!=0;);return(2>z?1:0)+F(n-1);}
```
Ungolfed
```
int F(int n)
{
    var z = n;
    if (n < 2) return 0;
    for (; n % --z != 0;) ;
    return F(n - 1) + (2 > z ? 1 : 0);
}
```
[Answer]
# Regex (Perl / Java / PCRE), 32 bytes
```
((?=(\2?+x*?(?!(xx+)\3+$))xx)x)*
```
[Try it online!](https://tio.run/##RU7dSsMwGL3fU8QRlnxLu6YVb0yzdjBvvfLOjjBlg0DcalIhEuKlD@Aj@iI1q4I3h@87nL/@YM3N@PKOsBUomPPz3iBciPTKert52Kzj7Hi2FDvJRb0WEJA@pg9Cb/VpQPPuNBcRtUa63uiBkpxk7u3JDcmisrzMSji8XkTNP8sTD7dYgbhkudS4t62pKwiteSx3MiHfiThDaGrWfwTWtZwE6WIMQvJS4onHGuRHgW0BAbvFYto1zUqbS/E7E@sVQd@fX4isaMtSWt7mCUHEGJW6u98qNVLaSNpVDfPLhjZX1HsG3TXDAN6Dh@U48rzi/Ac "Perl 5 – Try It Online") - Perl  
[Try it online!](https://tio.run/##bVLLbtswELz7KzZCA5OyzcjprYxgtEULFGjaIjk6PtASZTOhKJWkYjlBrv2AfmJ/xF3K7CNAdFnt7nCWM9xbcS9mt@XdQdVtYz3cYs5Uw1IO/1c6r/SLNSs3ssfOqO3WWhVQaOEcXApl4BFizXnhMdw3qoQaO@TaW2U2IOzGLVcU/NY2Owcf@kK2XjV4cgTwUWkJVW7kbvglCSuaUjLsJ5S/66pKWlleSVFKC@sB9rxI/pyMaUUpj3Pd1PPdNvAT4vI1ahDlZ2UkofQkN53WVFXEMfm9E9qR5CxN04TSx@fQIwMh/kWCR2TwfxmQ4AwZ1oi7426Sj2/MOETPn461p2/Ce2kN2Dz@odq6DQMc5ejGumm0FAYe8goZJYfrQhiD0pVpOw85BLWxRq73zsuaKUM5RJ0DjG2F@yJ7j9ccLAaIhmi8O3IcQQYRUWLsL1egsR1QzLVaeZLM8BEQ78Fk2PlkPK6BZa2wTmJC9DJb0SmY@YtNpqXZ@O3FOSwgIOENhvkKGYutsMtVH/UMmZmvOLy1Vuwdq5TWpJ@O@/FgCkDV2KANr5GbjIO5yM0cw2SCAi@FL7boUI1sltXHjAyba3DB3yP5cWPYzoqWxBFF0@6/VlfCbCROyqaG0qC0AlLjeFOid@FtH2g0uUHHdlZ5ScKjUv6Qe9uF9/nXbtFDX5HktIRfP37CaZmgM1Oo2cY2XUsyGv1Aav4UvlHYuAMhi5zcnC8mfbogixPS9xN683ryitK@pz1ND2GrDtnsPMt@Aw "Java (JDK) – Try It Online") - Java  
[Try it online!](https://tio.run/##XVDNatwwEL7nKaZC4Jm11ut105ZFa3xpC4HSQ9NbHITjamNTWxayAoaQax@gj9gX2Y6X9NLDDPP3fd/M@M6fj5XvPMgAJYidgAx8sI8mWD80rcVkl20qY7pmiKadRt8PNtRYk66/7eZEQcJ24qJ5tOuAi9bFGY35fPPlkzGkYE9MycQaeteb2UYUvg02e2janzGwM0M/9lEoENfF4frw/kNxeCdIX52mgLIvcy2H8sTsM95@/3jzlTQ9Q39COdzNMQzWcUTb/X1ZitoJ4uH56YE7XFa52u5pxdvFD9MPi2IrFI9rxrfTk4sr9lgw6I4J2Of3@grgouxec@mO5aXPUZoSSwNePjQ2se1QBsVi67tsEzFZEiUdKTkSwfO6ZU@27aZ1NQ18zV7DmoN0mYA/v36DyP5dMbIa6ZeX/56NpM@IVYl1UaXLpsLqDS5LSvXbVBItCy20OZ/zbZHnfwE "PHP – Try It Online") - PCRE
Takes its input in unary, as a string of `x` characters whose length represents the number. Returns its output as the length of the match.
```
        # tail = N = input value; no anchor needed, as every value returns a match
# Count the number of primes <= N, from the largest to the smallest prime.
(       # J = 0
    (?=
        # \2 starts at zero, and on each subsequent iteration, contains the difference
        # N-P-(J-1) where P is the previously found prime, and J is the running total of
        # our prime count.
        (
            \2?+           # Start from the previous value of \2, atomically so that it
                           # can't be backtracked and started again from zero if the
                           # following fails to match. This will make tail = P-1, where
                           # P is the previously found prime.
            x*?            # Advance as little as necessary to make the following match,
                           # and add this to \2, while subtracting it from tail.
            (?!(xx+)\3+$)  # Assert tail is not composite; note that this needs to be
                           # inside group \2 for it to work in PCRE1 and older versions of
                           # PCRE2, which atomicize groups that have nested backreferences
        )
        xx                 # Assert tail is prime by eliminating the false positives 0, 1
    )
    x   # J += 1; tail -= 1
)*      # Iterate zero or more times, until there are no more smaller primes
# Return J as our match
```
# Regex (Python[`regex`](https://bitbucket.org/mrabarnett/mrab-regex/src/hg/) / Ruby), ~~41~~ 39 bytes
```
((?=(?=(\3?))(\2x*?(?!(xx+)\4+$))xx)x)*
```
[Try it online!](https://tio.run/##PY5NTsMwEIX3OcVgIXUmf4pTVglWDpJmEYHbGjlOZBvhXoADcEQuEuwikGbx5ud987abv67muKtlW60Hd3OllRcZyje3mh6ssIyxHXEQqU7HgQhPbcgHHB4whIJOT8UjUQgUKN/j7ci7ik89KNFk59WCBmUStnb@VZkuAy107TatPLKKUQbqDFoa1PTcdqBHPgk9NlMGyWyS2c7mIlEZj2lB5a/iExWcIg8WcU9cOznblyvaElhguYnoxF7SSRKqg80mL/X3ieD9X6713dcfVnmJzls0VBzg@/MLDkVql5h2NkjxY/WvYw7am6ptmh8 "Python 3 – Try It Online") - Python `import regex`  
[Try it online!](https://tio.run/##PY5BasMwEEX3OcXEhFrj2EJ2u4orjGlM6SaFtDs7mJYojkAoRnGIuumyB@gRexFXdtrAMPAf/898c3r/6A1wWItG2JZqcYZl/ppTI9624QUuFk@Pq@d18ZC/FClIzlI476USoHgjuqNDO1BlFG849yrtpY6rklEaJZsUhN6OgB5bJTviRz5eAlQJ3XR7gveJM5Qu7UKXwARgdzCgQWoYIO0OtSQxQ0oH41WOh4hnvUAj8E8wI5DToWF7GpqN3yWPnTZSd6BDH36@vsEPZzfXAn@2/zWp62K1rOuekIwPU91miKRKbJCRbEqsnWN1N58hWosWg75nUcLYLw "Ruby – Try It Online") - Ruby
This is a port of the Perl/Java/PCRE regex to flavors that have no support for nested backreferences. Python's built-in `re` module does not even support forward backreferences, so for Python this requires [`regex`](https://bitbucket.org/mrabarnett/mrab-regex/src/hg/).
```
        # tail = N = input value; no anchor needed, as every value returns a match
# Count the number of primes <= N, from the largest to the smallest prime.
(       # J = 0
    (?=
        # \2 starts at zero, and on each subsequent iteration, contains the difference
        # N-P-(J-1) where P is the previously found prime, and J is the running total of
        # our prime count.
        (?=(\3?))          # \2 = \3 (or 0 if \3 is unset), to make up for the lack of
                           #      nested backreferences
        (
            \2             # Start from the previous value of \3 (as copied into \2).
                           # This will make tail = P-1, where P is the previously found
                           # prime.
            x*?            # Advance as little as necessary to make the following match,
                           # and add this to \3, while subtracting it from tail.
            (?!(xx+)\4+$)  # Assert tail is not composite; note that this needs to be
                           # inside group \3 for it to work in PCRE1 and older versions of
                           # PCRE2, which atomicize groups that have nested backreferences
        )
        xx                 # Assert tail is prime by eliminating the false positives 0, 1
    )
    x   # J += 1; tail -= 1
)*      # Iterate zero or more times, until there are no more smaller primes
# Return J as our match
```
# Regex (.NET), 35 bytes
```
((?=((?>\2?)x*?(?!(xx+)\3+$))xx)x)*
```
[Try it online!](https://tio.run/##ZZHNTuswEIX39yncCqke8qOk7DBukSIWSCCubpFYAAuTToslx4lsp5gGtvcBeERepLhJYYMlS2PNsec7x6VNytrgTosKbSNKJItX67DqWiv1mhS8qLWtFbLhfIvepf9w3SphLnxj0FoZBKxUwlpSddYJJ0uyqeWSXAupCYVuIwyx/Hy8o3TOw549TOfgj@d0PqLeR/BwEh0BeA8ejndjtpcbrvGFhDnoqU0X7ZN1JoyneWzTK9Rr95xMIe77N43bE6RFXTVS4RLYoCWKrWpDpXZE84xRxYsALpZXUiMFGHHdKsU0z6GTK6pnGRTpnZEOB0GP0XCVLholHZ0kE2D7t2SM7E@40Bw4ZtO3t1FopLfm9a8wFmlznz3GdRuk8Lv1fS3JBw3C91g6vtQboUJuRug1npIuex/HChgqi32Gvg9lMSQx8ZNYwo/FbbAozzgyGUWxj3hoQ3ctXPlMKm7SvqI@WFjRKuRZluHjeuPbX8a3PGc/TAGCfP7/IF0eYPwBPq4OBbD3Ye2yZJplXw "C# (.NET Core) – Try It Online")
This is a direct port of the Perl/Java/PCRE regex.
# Regex (.NET), 35 bytes
```
^(?=(x*?(?!(xx+)\2+$)x)*x)(?<-1>x)*
```
[Try it online!](https://tio.run/##ZZFNbtswEIX3PQVtFPBM9APJyzK0CwhdFEjQog7QRX8AVh47BChKICmXsZJtD9Aj9iIuLTnZhKsh5g3ne4@1y@rW0snIhlwna2KbB@epGXqnzJ5VomqNazXx6X5HwedfaN9raT@EzpJzKgp4raVzrBmcl17V7NCqLbuVyjDA4SAtc@L9/PQT1gLC1RrWMwghwe/L5C0GvAoI6@usXMXyNOdnuRWGfrO4hwK4fNP/ct7G9VCmLr8hs/f32RLTsf@p82eCvGqbTmnaIp@0TPNda0EZz4woOGhRRXC5vVGGAHEmTK81N6LEQe3ArAqs8q9WeZoEI0YndL7ptPKwyBbIz2@plPibONBdOFbLx8dZbOR39uGztI6g@1b8SNs@SvF163ksKycN4fNamH80B6ljblaaPb1jQ/E0TzVy0o7GDMMYymZKYhEWqcIXi8doUV0L4ipJ0pCI2MbhVvr6njXC5mMFIVrYQRPzrOv4caPx4yvjR1HyF6YIwf79@cuGMsKEC3zaXArkT9M5ldmyKP4D "C# (.NET Core) – Try It Online")
This uses the .NET feature of balanced groups to do the counting. It does not return a value for zero, but doing so only requires +1 byte (**36 bytes**):
```
^(?=(x*?(?!(xx+)\2+$)x)*x|)(?<-1>x)*
```
[Try it online!](https://tio.run/##ZZHPTuMwEMbvPIVbIdVD/ijpEeN2pYgDEmhXFIkD7EredFosOU5kO8U07XUfYB@RFyluUrjg01jzjef3fS5tUtYGD1pUaBtRIlm8WYdV11qp16TgRa1trZAN9wf0Lr3HdauEufaNQWtlELBSCWtJ1VknnCzJppZLciekJhS6jTDE8h/jwx8659RfzOl8RL2P4HkanYOHC78DOr9K8lmoD2N21Buu8ZWEReipTRftX@tM2E/z2Ka3qNfuJZlC3Pd/Nu6IkBZ11UiFS2CDlii2qg2V2hHNM0YVLwK5WN5KjRRgxHWrFNM8h06uqJ5lUKSPRjocBD1Gw1W6aJR0dJJMgB3fkjGyszDQnDhm091uFBrpg3n7JYxF2jxlv@O6DVL43vocS/JBg/C5lo5v9EaoEJwReo2XpMv241gBQ2WxD9H3oSyGJCZ@Ekv4srgNFuUVRyajKPYRD23o7oQrX0jFTdpX1AcLK1qFPMsy/FxvfPvN@Jbn7IspQJD3f/9JlwcYf4KPq1MBbD@cQ5ZMs@wD "C# (.NET Core) – Try It Online")
```
^                      # tail = N = input number
(?=
    (
        x*?            # Advance as little as necessary to make the following match
        (?!(xx+)\3+$)  # Assert tail is not composite
        x              # Eliminate the false primality positive of 0, and advance forward
                       # so that the next prime can be found (if we didn't do this, the
                       # regex engine would exit the loop due to a zero-width match)
    )*                 # Every time this loop matches an iteration, the capture group 1
                       # match is pushed onto the stack. This (balanced groups) is how we
                       # count the number of primes.
    x                  # Eliminate the false primality positive of 1
|                      # Allow us to return a value of 0 for N=0
)
(?<-1>x)*              # Pop all of the group 2 captures off the stack, doing head += 1
                       # for each one. This gives us our return value match.
```
---
I strongly suspect this function is impossible to implement in ECMAScript regex, even with the addition of `(?*)` or `(?<=)` / `(?<!)`. There doesn't seem to be room to multiplex the count and the current prime into a single tail variable, but I [don't know if this can be proved](https://cs.stackexchange.com/questions/137112/what-can-be-proven-regarding-the-differences-in-power-between-unary-ecmascript-r).
Only one variable can be modified within a loop, such that must decrease by at least 1 on every step – and only \$O(n)\$ space is available; no variable may contain a value larger than \$n\$, and the language has no concept of arrays (although it is possible to take them as immutable input), only scalar variables (i.e. capture groups and the cursor position).
So it would seem that calculating this function would require \$O({n^2\over log(n)})\$ scratch space, to multiplex the iteration count and current prime into a single number. While multiple loops in a row would be able to get closer to \$\pi(n)\$ than a single loop, the number of such loops could only be a constant; it seems that it would need to be able to grow with \$n\$ to be able to actually asymptotically calculate \$\pi(n)\$. And a nested loop would still have to distill all the information gained by its innermost loop into a single number \$\le n\$.
But these things are just indications and vague evidence, not a proof. I have [set a bounty](https://codegolf.meta.stackexchange.com/a/21941/17216) regarding this open question.
[Answer]
# Pyth - ~~7~~ 6 bytes
Since others are using prime factorization functions...
```
l{sPMS
```
[Test Suite](http://pyth.herokuapp.com/?code=l%7BsPMS&test_suite=1&test_suite_input=1%0A2%0A5%0A100&debug=0).
[Answer]
# Bash + coreutils, 30
```
seq $1|factor|egrep -c :.\\S+$
```
[Ideone.](https://ideone.com/VwKgoN)
---
# Bash + coreutils + BSD-games package, 22
```
primes 1 $[$1+1]|wc -l
```
This shorter answer requires that you have the bsdgames package installed: `sudo apt install bsdgames`.
[Answer]
## Pyke, ~~8~~ 6 bytes
```
SmPs}l
```
[Try it here!](http://pyke.catbus.co.uk/?code=SmPs%7Dl&input=5)
Thanks to Maltysen for new algorithm
```
SmP    -    map(factorise, input)
   s   -   sum(^)
    }  -  uniquify(^)
     l - len(^)
```
[Answer]
# C#, 157 bytes
```
n=>{int c=0,i=1,j;bool f;for(;i<=n;i++){if(i==1);else if(i<=3)c++;else if(i%2==0|i%3==0);else{j=5;f=1>0;while(j*j<=i)if(i%j++==0)f=1<0;c+=f?1:0;}}return c;};
```
Full program with test cases:
```
using System;
class a
{
    static void Main()
    {
        Func<int, int> s = n =>
            {
                int c = 0, i = 1, j;
                bool f;
                for (; i <= n; i++)
                {
                    if (i == 1) ;
                    else if (i <= 3) c++;
                    else if (i % 2 == 0 | i % 3 == 0) ;
                    else
                    {
                        j = 5;
                        f = 1 > 0;
                        while (j * j <= i)
                            if (i % j++ == 0)
                                f = 1 < 0;
                        c += f ? 1 : 0;
                    }
                }
                return c;
            };
        Console.WriteLine("1 -> 0 : " + (s(1) == 0 ? "OK" : "FAIL"));
        Console.WriteLine("2 -> 1 : " + (s(2) == 1 ? "OK" : "FAIL"));
        Console.WriteLine("5 -> 3 : " + (s(5) == 3 ? "OK" : "FAIL"));
        Console.WriteLine("10 -> 4 : " + (s(10) == 4 ? "OK" : "FAIL"));
        Console.WriteLine("100 -> 25 : " + (s(100) == 25 ? "OK" : "FAIL"));
        Console.WriteLine("1,000 -> 168 : " + (s(1000) == 168 ? "OK" : "FAIL"));
        Console.WriteLine("10,000 -> 1,229 : " + (s(10000) == 1229 ? "OK" : "FAIL"));
        Console.WriteLine("100,000 -> 9,592 : " + (s(100000) == 9592 ? "OK" : "FAIL"));
        Console.WriteLine("1,000,000 -> 78,498 : " + (s(1000000) == 78498 ? "OK" : "FAIL"));
    }
}
```
Starts to take awhile once you go above 1 million.
[Answer]
# Matlab, 60 bytes
Continuing my attachment to one-line Matlab functions. Without using a factorisation built-in:
```
f=@(x) nnz(arrayfun(@(x) x-2==nnz(mod(x,[1:1:x])),[1:1:x]));
```
Given that a prime `y` has only two factors in `[1,y]`: we count the numbers in the range `[1,x]` which have only two factors.
Using factorisation allows for significant shortening (down to 46 bytes).
```
g=@(x) size(unique(factor(factorial(x))),2);
```
Conclusion: Need to look into them golfing languages :D
[Answer]
# Actually, 10 bytes
This was the shortest solution I found that didn't run into interpreter bugs on TIO. Golfing suggestions welcome. [Try it online!](http://actually.tryitonline.net/#code=O-KVl3JgUOKVnD5g4paRbA&input=MzA)
```
;╗r`P╜>`░l
```
**Ungolfing**
```
         Implicit input n.
;╗       Duplicate n and save a copy of n to register 0.
r        Push range [0..(n-1)].
`...`░   Push values of the range where the following function returns a truthy value.
  P        Push the a-th prime
  ╜        Push n from register 0.
  >        Check if n > the a-th prime.
l        Push len(the_resulting_list).
         Implicit return.
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 3 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
ÆRL
```
Jelly has a built-in prime counting function, `ÆC` and a prime checking function, `ÆP`, this instead uses a built-in prime generating function, `ÆR` and takes the length `L`.
I guess this is about as borderline as using prime factorisation built-ins, which would also take 3 bytes with `!Æv` (`!` factorial, `Æv` count prime factors)
[Answer]
# PHP, ~~96~~ 92 bytes
```
for($j=$argv[1]-1;$j>0;$j--){$p=1;for($i=2;$i<$j;$i++)if(is_int($j/$i))$p=0;$t+=$p;}echo $t;
```
Saved 4 bytes thanks to Roman Gräf
[Test online](http://sandbox.onlinephpfunctions.com/code/f2572d44a90f3d1e3c4d0b9f0bf839c102b97ea8)
Ungolfed testing code:
```
$argv[1] = 5;
for($j=$argv[1]-1;$j>0;$j--) {
    $p=1;
    for($i=2;$i<$j;$i++) {
        if(is_int($j/$i)) {
            $p=0;
        }
    }
    $t+=$p;
}
echo $t;
```
[Test online](http://sandbox.onlinephpfunctions.com/code/b7278074256589459c294fdeb1f7dc791acec6bb)
[Answer]
# Java 8, ~~95~~ 84 bytes
```
z->{int c=0,n=2,i,x;for(;n<=z;c+=x>1?1:0)for(x=n++,i=2;i<x;x=x%i++<1?0:x);return c;}
```
**Explanation:**
[Try it online.](https://tio.run/##hZDBasMwEETv@Yq9FCTkGMvQS2QlX9Bccgw9qIpSNnXWRpaD4uBvd2U315LLwM5bmGEu5mbWTevocvqZbG26Dj4M0mMFgBScPxvrYD@fiwGWzTpwlZxxlaQLJqCFPRBomIb19rG86SIjXWaYRXVuPFNU6UFZoeNW7uSm4LMZNQmRoS4VVlFFHd9QiEruik3kyrvQewKrxknNOW3/VaecZ9ytwRNcU1F2CB7p@/hp@F/Jw70L7po3fcjbREJNjHLLJF8q/8vLF/z9BZcFf24yTr8)
```
z->{                // Method with integer parameter and integer return-type
  int c=0,          //  Start the counter at 0
      n=2,          //  Starting prime is 2
      i,x;          //  Two other temp integers
  for(;n<=z;        //  Loop (1) as long as `n` is smaller than or equal to the input `z`
       c+=x>1?1:0)  //    and increase the counter if we've came across a prime
                    //    (if `x` is larger than 0, it means the current `n` is a prime)
     for(x=n++,i=2;i<x;x=x%i++<1?0:x);
                    //   Determine if the next integer in line is a prime by setting `x`
                    //   (and increase `n` by 1 afterwards)
                    //  End of loop (1) (implicit / single-line body)
  return c;         //  Return the resulting counter
}                   // End of method
```
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 13 [bytes](https://codegolf.meta.stackexchange.com/a/9429/43319 "When can APL characters be counted as 1 byte each?")[SBCS](https://github.com/abrudz/SBCS ".dyalog files using a single byte charater set")
```
2+.=0+.=⍳∘.|⍳
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R2wQjbT1bAyB@1Lv5UccMvRogDZQ4tMJQwUjBFAA "APL (Dyalog Unicode) – Try It Online")
`⍳` **ɩ**ndices 1…N  
 ⧽ `∘.|` remainder-table (using those two as axes)  
`⍳` **ɩ**ndices 1…N
`0+.=` the sum of the elements equal to zero (i.e. how many dividers does each have)
`2+.=` the sum of the elements equal to two (i.e. how many primes are there)
[Answer]
# [MATL](http://github.com/lmendo/MATL), 9 bytes
This avoids prime-factor decomposition. Its complexity is O(*n*²).
```
:t!\~s2=s
```
[Try it online!](http://matl.tryitonline.net/#code=OnQhXH5zMj1z&input=NTA)
```
:     % Range [1 2 ... n] (row vector)
t!    % Duplicate and transpose into a column vector
\     % Modulo with broadcast. Gives matrix in which entry (i,j) is i modulo j, with
      % i, j in [1 2 ... n]. A value 0 in entry (i,j) means i is divisible by j
~     % Negate. Now 1 means i is divisible by j
s     % Sum of each column. Gives row vector with the number of divisors of each j
2=    % Compare each entry with 2. A true result corresponds to a prime
s     % Sum
```
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 12 bytes
```
(≢⊢~∘.×⍨)1↓⍳
 ≢ ⍝ tally
  ⊢~ ⍝ index values not in matrix of composite numbers
    ∘.×⍨ ⍝ outer product of positive integers with themselves
        1↓⍳ ⍝ range from 1–n less the first element
```
h/t [@adám](https://codegolf.stackexchange.com/users/43319/ad%C3%A1m)
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R2wSNR52LHnUtqnvUMUPv8PRHvSs0DR@1TX7UuxkorWDAlaZgCMRGQGwKxJbmQMLYwMAAAA "APL (Dyalog Unicode) – Try It Online")
[Answer]
# [C (clang)](http://clang.llvm.org/), 49 bytes
```
p(n,i){for(i=n;--i&&n%i;);return--n?p(n)+!--i:0;}
```
Assumes `n` is positive.
[Try it online!](https://tio.run/##DYzBDoIwEAXP7VesJJBuYBPFmyv6I14IiHmJLqRBL4Rvr73OTGaQ4d3bK6UlWAPepjkGdKYiqCoroazxuX6jidg9N1wfsrocdU@w1X96WPjNGNlv3mVCUO/yhAKoo5MS6ErnVqmuwd65JeZoCkUp7Uhyo3J8WNEQGloCmNXv6Q8 "C (clang) – Try It Online")
[Answer]
# [Thunno](https://github.com/Thunno/Thunno), \$ 4 \log\_{256}(96) \approx \$ 3.29 bytes
```
FAFL
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72sJKM0Ly9_UXTsgqWlJWm6FkvcHN18IEyoyIKFphAGAA)
Port of Dennis's 05AB1E answer.
#### Explanation
```
F       # Push the factorial of the input
 AF     # Get the unique prime factors
   L    # Push the length of the list
```
[Answer]
## JavaScript (ES6), ~~50+2~~ ~~46+2~~ 43 bytes
Saved ~~3~~ 5 bytes thanks to Neil:
```
f=n=>n&&eval(`for(z=n;n%--z;);1==z`)+f(n-1)
```
`eval` can access the `n` parameter.  
The `eval(...)` checks if `n` is prime.
---
**Previous solutions:**  
Byte count should be +2 because I forgot to name the function `f=` (needed for recursion)
46+2 bytes (Saved 3 bytes thanks to ETHproductions):
```
n=>n&&eval(`for(z=n=${n};n%--z;);1==z`)+f(n-1)
```
50+2 bytes:
```
n=>n&&eval(`for(z=${n};${n}%--z&&z;);1==z`)+f(n-1)
```
]