Prime counting function

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 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

Pavel

Posted 2016-09-23T19:49:27.680

Reputation: 8 585

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.027

I think this is what @TheBikingViking meant to link to: Related

– mbomb007 – 2016-09-23T20:52:42.947

The "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

Answers

36

05AB1E, 3 bytes

!fg

This assumes that factorization built-ins are allowed. Try it online!

How it works

!    Compute the factorial of the input.
 f   Determine its unique prime factors.
  g  Get the length of the resulting list.

Dennis

Posted 2016-09-23T19:49:27.680

Reputation: 196 637

5That's really clever! – mbomb007 – 2016-09-23T21:01:50.370

5

Damn, I'm getting rekt in my own language for the second time haha. +1

– Adnan – 2016-09-23T21:24:39.137

Why does this work? – Oliver Ni – 2016-10-23T05:36:08.730

1@Oliver Because the factorial of n is divisible by all integers 1, ..., n (in particular, the primes p ≤ n), and by no other prime q > n since it cannot be expressed as a product of smaller numbers. – Dennis – 2016-10-23T05:38:42.503

10

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 prime generator. The product p tracks (k-1)!^2, and p%k is 1 for primes and 0 for nonprimes.

xnor

Posted 2016-09-23T19:49:27.680

Reputation: 115 687

Calculating the factorial from the bottom up is a great trick. +1 – ETHproductions – 2016-09-23T23:38:46.720

6

MATL, 11, 10, 8, 5 bytes

:pYFn

Try it online!

I wrote a version that had a really cool explanation of how MATL's matrices work:

:YF!s1=1

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

James

Posted 2016-09-23T19:49:27.680

Reputation: 54 537

It's shorter to use the function "exponents of prime factorization", because that one vectorizes: YF!s1=s – Luis Mendo – 2016-09-23T20:18:49.793

@LuisMendo That's a totally different approach, so feel free to go ahead and post it. (Although if you don't want to, I happily would) – James – 2016-09-23T20:20:48.340

Go ahead. I'll port that to Jelly to practice :-) – Luis Mendo – 2016-09-23T20:22:58.123

5

Jelly, 8 5 bytes

3 bytes saved thanks to @Dennis!

RÆESL

Try it online!

Port of DJMcMayhem's MATL answer (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

Luis Mendo

Posted 2016-09-23T19:49:27.680

Reputation: 87 464

1Correction: port of Luis Mendo's: DJMcMayhem's MATL answer. :P – James – 2016-09-23T20:26:51.943

2You only need the maximal length of the results of ÆE, as each exponent corresponds to a different prime factor. RÆESL achieves just that. !ÆEL would be even shorter. – Dennis – 2016-09-23T21:43:08.750

1@Dennis Thanks! I've used the first suggestion. The second one is too different, and is your approach – Luis Mendo – 2016-09-23T21:54:28.437

5

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.

Ton Hospel

Posted 2016-09-23T19:49:27.680

Reputation: 14 114

5

MediaWiki templates with 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.

DuhHello

Posted 2016-09-23T19:49:27.680

Reputation: 499

4

Retina, 31 bytes

Byte count assumes ISO 8859-1 encoding. Convert input to unary, generate the range from 1 to n, each on its own line. Match the primes.

.*
$*
\B
¶$`
m`^(?!(..+)\1+$)..

Try it online - Input much larger than 2800 either times out or runs out of memory.

References:

Martin's range generator

Martin's prime checker

mbomb007

Posted 2016-09-23T19:49:27.680

Reputation: 21 944

4

Jelly, 13 11 10 9 8 7 6 bytes

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
Or see the first 100 terms of the series n=[1,100], also at TryItOnline

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)

Jonathan Allan

Posted 2016-09-23T19:49:27.680

Reputation: 67 804

1You can get to 7 bytes %þ`¬Sċ2 using a table of remainders. – miles – 2016-09-23T22:53:49.830

1ḍþḅ1ċ2 saves a byte. – Dennis – 2016-09-24T19:05:38.897

4

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? 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>

ETHproductions

Posted 2016-09-23T19:49:27.680

Reputation: 47 880

Waw...it took me a while to understand. Nice job! – todeale – 2016-09-24T09:09:36.593

Sadly it doesn't apply to your answer but you can probably get away with one & in the middle of your primality function. – Neil – 2016-09-24T17:50:58.087

4

Jelly, 3 bytes

!Æv

Try it online!

How it works

!Æv  Main link. Argument: n

!    Compute the factorial of n.
 Æv  Count the number of distinct prime factors.

Dennis

Posted 2016-09-23T19:49:27.680

Reputation: 196 637

3

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);
}

Ariel Bereslavsky

Posted 2016-09-23T19:49:27.680

Reputation: 31

@tfbninja yes you right, but I gave the function part only, which does not compile by it's own – Ariel Bereslavsky – 2018-01-16T21:27:27.830

@tfbninja Actually, it's not.

– Erik the Outgolfer – 2018-01-17T13:09:34.130

cool sounds good! – FantaC – 2018-01-17T15:41:08.217

3

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?, 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.

AdmBorkBork

Posted 2016-09-23T19:49:27.680

Reputation: 41 581

3

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!

Adnan

Posted 2016-09-23T19:49:27.680

Reputation: 41 965

ÅPg is what it'd be now, right? – Magic Octopus Urn – 2018-01-18T17:55:00.517

3

CJam, 7 bytes

rim!mF,

Try it online! Uses a factorization function.

Explanation:

ri      | read input as integer
  m!    | take the factorial
    mF  | factorize with exponents (one element per prime)
      , | find length

Linus

Posted 2016-09-23T19:49:27.680

Reputation: 1 948

3

Jelly, 6 bytes

Ḷ!²%RS

This uses only basic arithmetic and Wilson's theorem. Try it online! or verify all test cases.

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.

Dennis

Posted 2016-09-23T19:49:27.680

Reputation: 196 637

2

APL (Dyalog Unicode), 13 bytesSBCS

2+.=0+.=⍳∘.|⍳

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)

Adám

Posted 2016-09-23T19:49:27.680

Reputation: 37 779

2

Python 3, 40 bytes

f=lambda n:1if n<1else(2**n%n==2)+f(n-1)

An odd integer k is prime if an only if 2**(k-1) is congruent to 1 mod k. Thus, we just check for this condition and add 1 for the case of k=2.

Sandeep Silwal

Posted 2016-09-23T19:49:27.680

Reputation: 129

2**n % n==2 is not enough as primaly test – RosLuP – 2018-01-18T16:42:37.340

@RosLuP That is why the base case of n==0 should add 1 (to account for the n=2 case). – Sandeep Silwal – 2018-01-19T00:02:25.473

2**n % n==2 is not enough in general... Exist many (infinite in what I would remember) numbers where 2^n%n=2 that are not primes – RosLuP – 2018-01-19T07:48:19.580

For example 341=11*31 but ( 2^341) mod 341==2 – RosLuP – 2018-01-19T08:44:21.517

@RosLuP: Ah ok yes, I looked it up. These numbers are called Fermat Psuedoprimes but they appear to be quite rare :P – Sandeep Silwal – 2018-01-19T22:55:28.080

2

Pyke, 8 6 bytes

SmPs}l

Try it here!

Thanks to Maltysen for new algorithm

SmP    -    map(factorise, input)
   s   -   sum(^)
    }  -  uniquify(^)
     l - len(^)

Blue

Posted 2016-09-23T19:49:27.680

Reputation: 26 661

2

Pyth - 7 6 bytes

Since others are using prime factorization functions...

l{sPMS

Test Suite.

Maltysen

Posted 2016-09-23T19:49:27.680

Reputation: 25 023

2

Bash + coreutils, 30

seq $1|factor|egrep -c :.\\S+$

Ideone.


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.

Digital Trauma

Posted 2016-09-23T19:49:27.680

Reputation: 64 644

2

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.

Yodle

Posted 2016-09-23T19:49:27.680

Reputation: 2 378

2

Jelly, 3 bytes

Æ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)

Jonathan Allan

Posted 2016-09-23T19:49:27.680

Reputation: 67 804

2

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

ptev

Posted 2016-09-23T19:49:27.680

Reputation: 111

2

MATL, 9 bytes

This avoids prime-factor decomposition. Its complexity is O(n²).

:t!\~s2=s

Try it online!

:     % 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

Luis Mendo

Posted 2016-09-23T19:49:27.680

Reputation: 87 464

2

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

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

Mario

Posted 2016-09-23T19:49:27.680

Reputation: 3 043

Why do you use isInt(...)?1:0 and not just isInt(...) – Roman Gräf – 2016-09-25T18:09:52.973

@RomanGräf Thanks, you are right. I left the ternary after a lot of code semplification, and that was so obvious that I couldn't see it... – Mario – 2016-09-25T20:12:59.223

2

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!

;╗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.

Sherlock9

Posted 2016-09-23T19:49:27.680

Reputation: 11 664

1

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.

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

Kevin Cruijssen

Posted 2016-09-23T19:49:27.680

Reputation: 67 575

1

Perl 6, 31 bytes

{+grep {$_%%none 2..^$_},2..$_}

Try it online!

Sean

Posted 2016-09-23T19:49:27.680

Reputation: 4 136

1

(pure) BASH, 138 bytes

Look, Mom!
No preloaded prime tables, no prime related builtins, not even a division or plultimication!

a=2
l=0
while((a<=$1));do if((b[a]))
then((c=b[a]));else((c=a,l++));fi;((d=a+c))
while((b[d]));do((d+=c));done
((b[d]=c,a++));done;echo $l

Eating the pudding:

$ for i in 1 2 3 11033 ; do bash fsoe-primecounter-in-bash $i ; done
0
1
2
1337

Ok, probably looping while looking at candidates modulo all possible divisors would have been shorter in "pure BASH" too but we've seen those loops at least 1001 times now.

This solution builds its own prime table on the fly and so is bearably fast even in "pure BASH".

user19214

Posted 2016-09-23T19:49:27.680

Reputation:

1

APL NARS, 150 bytes

∇r←g n;h;i;k
i←0⋄h←1+⍳n-1⋄→B
A:k←i⊃h⋄h←k∪(0≠k∣h)/h
B:→A×⍳(⍴h)≥i+←1
r←⍴h
∇

this would be the "Crivello di Eratostene" or something as that.

If h=2..n

In the first iteration of the loop eliminate each multiple of 2 in h except 2

in the second iteration of the loop eliminate each multiple of 3 in h except 3

...

return the number of final element of h.(in h primes return ⍴h)

  g¨1 2 5
0  1  3 
  g 20000
2262

As note negative for the question: There would be a range limit for the arg of the function; something as 1..20000 and each answer would indicate the range the argument has to be for to be safe to pass to the function (without one seg fault in the code, without one memory insufficient for the program, without one incorrect value return as result )

RosLuP

Posted 2016-09-23T19:49:27.680

Reputation: 3 036

1

Pyth, 6 bytes

l{P.!Q

Explanation follows:

   .!Q Factorial of input
  P    List primes of factorial with multiplicity
 {     Remove duplicates formed by said multiplicity
l      Length of deduplicated list

ericeschnei

Posted 2016-09-23T19:49:27.680

Reputation: 71

1

Add++, 17 bytes

L,RB*f0b]@bLA1=!*

Try it online!

caird coinheringaahing

Posted 2016-09-23T19:49:27.680

Reputation: 13 702

1

Kotlin, 85 bytes

{n:Int->var r=0
for(i in 2..n){var t=1
for(p in 2..i/2)if(i%p==0){t--
break}
r+=t}
r}

Try it online!

Try lambda for first time as saved 16 bytes.

Kotlin, 101 bytes

fun c(n:Int):Int{var r=0
for(i in 2..n){var t=1
for(p in 2..i/2)if(i%p==0){t--
break}
r+=t}
return r}

Try it online!

JohnWells

Posted 2016-09-23T19:49:27.680

Reputation: 611

1

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)

Hedi

Posted 2016-09-23T19:49:27.680

Reputation: 1 857

1At least on my browser, eval can access the n parameter to your function (which you forgot to name, costing you 2 bytes; it's good to know that I'm not the only one who makes that mistake) which saves you 5 bytes. – Neil – 2016-09-24T17:47:07.763

@Neil I didn't know for eval. Tested with firefox, chrome and edge it worked for me. The explanation is eval() parses in statement context. Two examples: a=12;f=b=>eval('a + 5');f(8) displays 17 and a=12;f=a=>eval('a + 5');f(8) displays 13.

– Hedi – 2016-09-24T19:11:48.273

1

Java 7,102 bytes

Brute force

int f(int n){int i=2,j=2,c=1,t=0;for(;i<=n;j=2,c+=t==1?1:0,i++)for(;j<i;t=i%j++==0?j=i+1:1);return c;}

Ungolfed

int f(int n){
int i=2,j=2,c=1,t=0;
for(;i<=n;j=2,c+=t==1?1:0,i++)
    for(;j<i;)
        t=i%j++==0?j=i+1:1;
    return c;
 }

Numberknot

Posted 2016-09-23T19:49:27.680

Reputation: 885

This is currently giving an incorrect result for input 1. It currently returns 1 instead of 0. You can fix this by either changing return c; to return n<2?0:c; or changing ,c=1, to ,c=n<2?0:1,. – Kevin Cruijssen – 2017-05-12T14:27:50.857

1

q 35 bytes

{sum 1=sum'[0=2_' a mod\: a:til x]}

reuben taylor

Posted 2016-09-23T19:49:27.680

Reputation: 11

1

Actually, 10 bytes

If my first Actually answer is disallowed for using a prime-generating function, here is a backup answer using Wilson's theorem. Golfing suggestions welcome. Try it online!

R`;D!²%`MΣ

Try it online

         Implicit input n.
R        Push range [1..n]
`...`M   Map the following function over the range. Variable k.
  ;        Duplicate k.
  D        Decrement one of the copies of k.
  !²       Push ((k-1)!)².
  %        Push ((k-1)!)² % k. This returns 1 if k is prime, else 0.
Σ        Sums the result of the map, adding all the 1s that represent primes, 
          giving the total number of primes less than n.
         Implicit return.

Sherlock9

Posted 2016-09-23T19:49:27.680

Reputation: 11 664

1

Racket 60 bytes

(require math)(λ(n)(length(filter prime?(range 1(+ 1 n)))))

Testing:

(require math)
(define f
    (λ(n) (length (filter prime? (range 1 (+ 1 n)))))
)

(f 1)
(f 2)
(f 3)
(f 5)

Output:

0
1
2
3

rnso

Posted 2016-09-23T19:49:27.680

Reputation: 1 635

If (require math) is required for your program to work, you need to include it in your byte count. – Pavel – 2017-05-11T15:41:23.527

1

PARI/GP, 15 bytes

n->#factor(n!)~

Take the factorial and count its unique prime factors.

alephalpha

Posted 2016-09-23T19:49:27.680

Reputation: 23 988

1

Actually, 3 bytes

This uses a prime factorization function, which may or may not be allowed after the OP clarifies. Golfing suggestions welcome. Try it online!

!yl

Ungolfing

     Implicit input n.
!    Push n factorial.
 y   Push a list of all of the positive prime factors of n! (every prime < n)
  l  Take the length of that list of primes.
     Implicit return.

Sherlock9

Posted 2016-09-23T19:49:27.680

Reputation: 11 664

0

Pyt, 3 bytes

řṗƩ

Explanation:

ř         Push [1,2,...,input]
 ṗ        Elementwise isPrime(k)
  Ʃ       Sum of array (autoconverts booleans to 0/1)

Try it online!

mudkip201

Posted 2016-09-23T19:49:27.680

Reputation: 833

1So one can use isprime right? – RosLuP – 2018-01-18T16:45:36.497