Niven's proof that the cosine of a non-zero rational number is irrational

First note Niven's Lemma 2.3, concerning a function of the form h(x) = x^n g(x)/n!, where g(x) is a polynomial with integer coefficients:
g(x) = c0 + c1 x + ....  When you take j derivatives, and then evaluate the result at zero, there are three cases.
Case 1: j < n. Every term still has powers of x, and the result is zero.
Case 2: j = n. The result equals c0, an integer.
Case 3; j > n. The result arises from a term in g(x) with a one power of x that got multiplied by x^n and was then differentiated at least (n+1) times.  The differentiations kill of the n! in the denominator and supply at least an extra factor of (n+1).  So the result is an integer divisible by n+1.

Start with Niven's equation (2.1) (page 17) 
We will consider only the case where r = a/b is an
integer and set

b=1;

f[p_,x] := b^(3p-1) (r-x)^(2p) (r^2-(r-x)^2)^(p-1)/(p-1)!

f[p,x]

Note that this is a polynomial of degree 4p-2, and that the lowest exponent that occurs is p-1.  If p =3

f[3,x]//Expand

Now play the same trick as in the earlier proof, subtracting off all the even-numbered derivatives, with alternating signs.

F[p_,x_]:=Sum[(-1)^i D[f[p,x], {x,2i}],{i,0,2p-1}]

Again this is a polynomial of degree 4p-2:

F[3,x]//Expand

This polynomial satisfies the same differential equations as in the earlier proof:
F''[p,x] + F[p,x] = f[p,x]
and
d/dx [F'[p,x] Sin[x] - F[p,x] Cos[x]] = f[p,x] Sin[x].
A check for the case p = 3:

D[ F[3,x] ,{x,2}] + F[3,x] - f[3,x]//Simplify

D[ D[F[3,x],x] Sin[x] - F[3,x] Cos[x],x] - f[3,x]*Sin[x]//Simplify

From the second of the above differential equations we can again determine the integral of f[p,x] Sin[x] from 0 to r, but this time we must leave some cosines and sines in the answer.  Note

integral= Integrate[ f[3,x] Sin[x], {x,0,r}]

upper = D[F[3,x],x] Sin[x] - F[3,x] Cos[x]/.x->r //Simplify

lower = -F[3,x]/.x->0

integral - (upper - lower)//Simplify

Notice that all the terms involving Sin[r] have dropped out.  This is because the coefficient of Sin[r], F'[r], is zero.  See Lemma 2.4 in Niven for an easy explanation.

But in fact this integral is a very small quantity.  Let's take r = 1, for example:

Integrate[ f[3,x] Sin[x], {x,0,1}]/.r->1//N

On the other hand, the integral involves some pretty large numbers.

integral/.r->1

So if we approximate the integral by zero, we get a remarkably good rational approximation to the cosine of 1.

approx = 250576/463770

approx - Cos[1]//N

If we choose a larger prime p, the integral gets smaller, the coefficients get larger, and the approximation gets even better.

upper5 = - F[5,x] Cos[x]/.x->r

lower5 = -F[5,x]/.x->0

approxCos[r_] = lower5/(upper5/Cos[r])

approxCos[1]

approxCos[1] - Cos[1]//N

approxCos[Pi/2]//N

Notice that we have stumbled across an excellent way to approximate the cosine function as a quotient of polynomials.  This is just about as efficient as using Taylor series.

Now back to the proof.  We have 
- F(r) cos(r) + F(0) = the integral
First we will show that the left-hand side is a positive integer if cos(r) is rational.  Then we will show that for a sufficiently large prime p, the right-hand side is less than 1.  For the sake of simplicity, let's stay with the case where r is an integer, leaving b = 1.
By Lemma 2.3, the only terms in F that contributes to F[0] are the ones in which we took (p-1) or more derivatives. If we took more than p-1 derivatives, we got a factor of p from the pth derivative of x.  But when we took precisely p-1 derivatives, we had (for example)

D[ f[5,x], {x,5-1}]/.x->0

As Niven points out (top of page 18), this equals

(2r)^(p-1) * r^(2p)//Simplify

Remember, we are keeping things simple by letting r be an integer.  As long as we choose p to be greater than r, this term is not divisible by p.  So when we add on the other terms (which all have a factor of p) we can conclude that F[0] is not divisible by p.
Examples with r = 1:

-F[5,x]/.{x->0,r->1}

FactorInteger[%]

-F[7,x]/.{x->0,r->1}

FactorInteger[%]

Next we consider F(r), which is easier.  If we set
 r - x = y 
 in the definition of f, we have

f[p,x]/.x->r-y

we see that when y = 0 (i.e x = r) all the derivatives that contribute will have a factor of p.  So F(r) is divisible by p. Examples:

F[5,x]//.{x->r,r->1}

FactorInteger[%]

F[7,x]//.{x->r,r->1}

FactorInteger[%]

F[11,x]//.{x->r,r->1}

FactorInteger[%]

So we can set F(0) = q (not divisible by p)
and F(r) = mp.  Now assume that cos(r) = d/k.
Since
- F(r) cos(r) + F(0) = the integral
-mpd/k + q = the integral
or
-mpd + kq = k (the integral)
Now choose the prime p to be larger than k.  On the left side, the first term is an integer divisible by p, while the second term is an integer not divisible by p.  So the left-hand side is a non-zero integer.
On the other hand, since f(p,x) has a factor of (p-1)! in the denominator, we can make it as small as we like by choosing p large enough (see the tedious details on p.19 of Niven).  We choose to make it less than 1 in magnitude.
Thus we have a contradiction: the left side is a non-zero integer, less than 1 in magnitude!  This means that cos(r) cannot be rational if r is an integer.  The extension of the proof to the case where r = a/b is easy (see Niven).

The final step: if Pi were rational, then cos(Pi) would have to be irrational.  But it isn't -- it's -1.  So Pi must be irrational.