Section 21.1 First Steps

plot(prime_pi,2,100)
)Sage note 21.1.2. Syntax for counting primes.
The syntax for this function is prime_pi(n)
.
Subsection 21.1.1 A funky formula
Given the skepticism of the paragraphs so far this chapter, you may be surprised to learn there are exact formulas for this function, as well as for thexxxxxxxxxx
def primeish(n):
if n==1:
return 0
elif n==2:
return 1
elif n==3:
return 2
else:
result = -1
fact = 1
for j in range(3,n+1):
fact = fact*(j-2)
result += (fact - j*floor(fact/j))
return result
β
import math
def plotprimeish(n):
n = int(math.floor(n))
return primeish(n)
β
pretty_print(html("The number of primes up to 20000 this formula gives is $%s$"%primeish(20000)))
pretty_print(html("The real function in Sage gives $%s$"%prime_pi(20000)))
pretty_print(html("And let's compare plots:"))
plot(lambda x:plotprimeish(x), (x,2,100)) + plot(prime_pi,2,100,color='black')
Sage note 21.1.3. Cython.
It's possible to significantly speed up many such computations by converting to Cython, a way to take Python/Sage and turn it into the much-faster compiled language C. For a project, try to speed this function up using Cython!
Sage note 21.1.4. Not all algorithms are equal.
Don't forget that just because an algorithm works, doesn't guarantee it will be useful in practice! However, it's often useful to get something correct first, and only then try to optimize.
Subsection 21.1.2 A very low bound
On a more computationally feasible note, one can find a very rudimentary (lower) bound on this function. Recall that unadorned logarithms are the natural log.Fact 21.1.5.
There are at least
primes less than or equal to
Proof.
In Saidak's proof [E.7.22] of the infinitude of the primes, he constructs the sequence
Then he shows, similarly to Euclid's proof, that there is at least one new prime divisor in each element of the sequence (even if not necessarily a larger one). So the \(n\)th prime can be no bigger than the \(n\)th term of this sequence. (See Exercise 21.5.3.)
By induction, we see that this term (and hence the \(n\)th prime) is less than or equal to \(2^{2^{n-1}}\text{.}\)
The case \(n=1\) is clear, since the first prime is \(2\text{.}\)
-
The \(n\)th term is the previous terms multiplied together, plus 1, which by induction is less than
\begin{equation*} 2^{2^0}2^{2^1}\cdots 2^{2^{n-2}}+1=2^{1+2+4+\cdots +2^{n-2}}+1=2^{2^{n-1}-1}+1\leq 2^{2^{n-1}} \end{equation*}(this uses the same type of technique as in Subsection 4.5.2).
So when \(\pi(x)=n\text{,}\) the \(n\)th term in the sequence is \(2^{2^{\pi(x)-1}}\text{,}\) which can't be less than \(n\) itself (the \(n\)th prime is certainly at least \(n\)). If we rewrite this as \(2^{2^{\pi(x)-1}}\geq n\text{,}\) we can take two logs to get
This yields the given statementβ1β, with the floor function accounting for the fact that \(\pi\) takes only integer values.

Subsection 21.1.3 Knowledge from nowhere
Finally, although it may not seem evident, you should know that it is not necessary to actually find all the firstDefinition 21.1.7.
Let
%time
when using the following cell.)
xxxxxxxxxx
%time nth_prime(10^7)