Before closing this chapter, let us spend some time at the intersection of mathematics and programming.
Consider the following number:
It is known that . From this, it follows that . Now, consider the following sequence:
As becomes very large, the values in this sequence will become smaller and smaller. This is because, if you keep multiplying a fraction with itself, it becomes smaller and smaller. In mathematical terms, the limit of this sequence as tends to infinity is zero. Let us verify this programmatically:
xxxxxxxxxx
71import math
2n = int(input()) # sequence length
3CONST = math.pow(2, 0.5) - 1 # basic term in the sequence
4a_n = 1 # zeroth term
5for i in range(n):
6 a_n = a_n * CONST # computing the nth term
7print(a_n)
Try this out for a few values of . For , the value is , which is so small that for all practical purposes, it is as good as zero.
Now, here is another fact. For every number , there are unique integers and such that:
For , this is obvious: . What about higher values of ? . We can prove this using mathematical induction. The following is a sketch of the inductive proof. If , then:
The equation given above defines what is called a recurrence relation: each new term in the sequence is a function of the preceding terms. In this sequence we have . For , the pair of equations given below forms the recurrence relation:
Loops are useful tools when it comes to computing terms in such sequences:
xxxxxxxxxx
41n = int(input()) # sequence length
2x_n, y_n = -1, 1 # x_1 and y_1
3for i in range(n - 1):
4 x_n, y_n = 2 * y_n - x_n, x_n - y_n
This in turn provides a way to approximate using rational numbers:
As becomes large, this approximation will become increasingly accurate. For example, here is an approximation after 100 iterations. It is accurate up to several decimal places!
Is any of this useful? I don't know. But honestly, who cares? We don't do things because they are useful. We do them because they are interesting. And all interesting things will find their use at some point of time in the future.