We will look at two more libraries — math
and random
— and use them to solve some fascinating problems in mathematics.
math
Consider the following sequence:
Mathematically, it is known that this sequence converges or approaches a specific value. In other words, this sequence gets closer and closer to a well defined number as more terms are added. This number is called the limit of the sequence. What is the limit for the above sequence? Can we use whatever we have learned so far to estimate this value?
xxxxxxxxxx
51import math
2x = 0
3for n in range(1, 6):
4 x = math.sqrt(2 + x)
5 print(f'n = {n}, x_n = {x:.3f}')
If we execute the above code, we get the following output:
xxxxxxxxxx
51n = 1, x_n = 1.414
2n = 2, x_n = 1.848
3n = 3, x_n = 1.962
4n = 4, x_n = 1.990
5n = 5, x_n = 1.998
sqrt
is a function in the math
library that returns the square root of the number that is entered as argument. Representing the output shown above as a table:
Approximate value | ||
---|---|---|
1 | 1.414 | |
2 | 1.848 | |
3 | 1.962 | |
4 | 1.990 | |
5 | 1.998 |
Isn't that beautiful? It looks like this sequence — the train of square roots — is approaching the value 2. Let us run the loop for more number of iterations this time:
xxxxxxxxxx
51import math
2x = 0
3for n in range(1, 20):
4 x = math.sqrt(2 + x)
5print(x)
After just 20 iterations, the value is so close to two: 1.9999999999910236
. But we have used trial and error to decide when to terminate the iteration. A better way to do this is to define a tolerance: if the difference between the previous value and the current value in the sequence is less than some predefined value (tolerance), then we terminate the iteration.
xxxxxxxxxx
91import math
2x_prev, x_curr = 0, math.sqrt(2)
3tol, count = 0.00001, 0
4while abs(x_curr - x_prev) >= tol:
5 x_prev = x_curr
6 x_curr = math.sqrt(2 + x_prev)
7 count += 1
8print(f'Value of x at {tol} tolerance is {x_curr}')
9print(f'It took {count} iterations')
random
How do we toss a coin using Python?
xxxxxxxxxx
21import random
2print(random.choice('HT'))
That is all there is to it! random
is a library and choice
is a function defined in it. It accepts any sequence as input and returns an element chosen at random from this sequence. In this case, the input is a string, which is nothing but a sequence of characters.
We know that the probability of obtaining a head on a coin toss is 0.5. This is the theory. Is there a way to see this rule in action? Can we computationally verify if this is indeed the case? For that, we have to set up the following experiment. Toss a coin times and count the number of heads. Dividing the total number of heads by will give the empirical probability. As becomes large, this probability must approach 0.5.
xxxxxxxxxx
81import random
2n = int(input())
3heads = 0
4for i in range(n):
5 toss = random.choice('HT')
6 if toss == 'H':
7 heads += 1
8print(f'P(H) = {heads / n}')
Let us run the above code for different values of and tabulate our results:
10 | 0.2 |
100 | 0.52 |
1,000 | 0.517 |
10,000 | 0.5033 |
100,000 | 0.49926 |
1,000,000 | 0.499983 |
The value is approaching 0.5
as expected! random
is quite versatile. Let us now roll a dice!
xxxxxxxxxx
21import random
2print(random.randint(1, 6))
randint(a, b)
returns a random integer such that . We can do a similar experiment for finding the probability of obtaining a number, say 1, when a dice is thrown.