Sieve of Eratosthenes is a method for finding prime numbers:
take the sequence of all positive integer numbers starting from 2 then for each remaining number drop all its multiples.
2 3 x 5 x 7 x 9 x 11 x 13 x 15 x 17 x ...
2 3 _ 5 _ 7 _ x _ 11 _ 13 _ x _ 17 x ...
Task
Given a number \$n \ge 2\$, find the minimum amount of numbers you have to drop before you can determine whether n is a prime number or not using the Sieve of Eratosthenes.
For example if you want to check if \$15\$ is prime you start dropping multiples of \$2\$:
2 3 x 5 x 7 x 9 x 11 x 13 x 15 ..
then \$3\$:
2 3 _ 5 _ 7 _ x _ 11 _ 13 _ X ..
Here we are! 15 was dropped so it's not prime.
We dropped 8 numbers.
Note that we don't need to check more numbers.
That is, you don't have to find how many not-primes are there.
Note also that if the number you are testing is prime then you have to try all numbers up to it( without considering optimizations) , and you have to drop all not primes actually in that case.
Test cases
Terms for n from 2 to 100
0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 8, 7, 9, 8, 10, 9, 12, 10, 13, 11, 15, 12, 16, 13, 18, 14, 19, 15, 20, 16, 23, 17, 24, 18, 24, 19, 27, 20, 28, 21, 28, 22, 31, 23, 33, 24, 32, 25, 36, 26, 37, 27, 36, 28, 41, 29, 42, 30, 40, 31, 45, 32, 47, 33, 44, 34, 50, 35, 51, 36, 48, 37, 55, 38, 56, 39, 52, 40, 59, 41, 59, 42, 56, 43, 64, 44, 66, 45, 60, 46, 67, 47, 71, 48, 64, 49
Rules
This is code-golf, the aim is to have the shortest code(in bytes) possible within the respect of golfing rules.
This is also sequence so as usual you can output:
- n-th term.
- First n terms.
- The infinite sequence.
Where n refers to the actual number you have to check so the sequence starts at \$n = 2\$.
You can include 0 and 1, however those are considered undefined behaviour so the result doesn't matter.