Chapter 4 Dealing with Large Catalan Numbers
In this part, we will explore some of the properties of large catalan numbers.
4.1 Catalan and modulos: the Bostan Conjecture
Information 4.1
Alin Bostan (computer scientist at INRIA and Ecole Polytechnique) conjectured a few years ago (see this link p.26) that in basis 10 the last digit of is never . So far this is still an open problem.
Check the conjecture for . The output should look like
1 mod 10 is 1: the Conjecture is True
Catalan 2 mod 10 is 2: the Conjecture is True
Catalan 3 mod 10 is 5: the Conjecture is True
Catalan 4 mod 10 is 4: the Conjecture is True
Catalan ...
# We test the conjecture for 1 to 100
for n in range(1, 101):
if CatalanClosedForm(n) % 10 != 3:
print('Catalan', n, 'mode 10 is',
% 10, ':the Conjecture is', True)
CatalanClosedForm(n) else:
print('the Conjecture is', False)
10 mode 10 is 6 :the Conjecture is True
Catalan 11 mode 10 is 6 :the Conjecture is True
Catalan 12 mode 10 is 2 :the Conjecture is True
Catalan 13 mode 10 is 0 :the Conjecture is True
Catalan
...99 mode 10 is 8 :the Conjecture is True
Catalan 100 mode 10 is 2 :the Conjecture is True Catalan
How to check the conjecture for very large values? Try for example with .
def precalc(MOD, num):
= [0] * num
catalan 0], catalan[1] = 1, 1
catalan[for i in range(2, num):
= 0
total for j in range(1, i+1):
= catalan[j-1] % MOD
left = catalan[i-j] % MOD
right = (total + (left * right) % MOD) % MOD
total = total
catalan[i] return catalan
if __name__ == '__main__':
= precalc(10, 8000)
catalan for i in range(7000, 7101):
if catalan[i] != 3:
print('Catalan', i, 'mode 10 is', catalan[i], ':the Conjecture is',
True)
else:
print('the Conjecture is', False)
7000 mode 10 is 4 :the Conjecture is True
Catalan 7001 mode 10 is 4 :the Conjecture is True
Catalan 7002 mode 10 is 8 :the Conjecture is True
Catalan
...7099 mode 10 is 0 :the Conjecture is True
Catalan 7100 mode 10 is 0 :the Conjecture is True Catalan
Since our previous function is not that efficient, we need to apply other algorithms. We use the property that This is easy to prove, we assume that and . Therefore, we have and where . We have Then we can use this property to write the recursive function as above. After all the computation, we find that the conjecture is true for .
More generally, what is the frequency of among last digits of the first Catalan numbers?
# we use a dictionary to store the frequency of the last digit of Catalan numbers
# we test the case with n = 1000, 5000, 10000
= precalc(10, 1000)
catalan1 = precalc(10, 5000)
catalan2 = precalc(10, 10000)
catalan3
def count_last_digit(catalan):
= {}
count for el in catalan:
if el in count:
+= 1
count[el] else:
= 1
count[el]
for key in count:
= count[key] / len(catalan)
count[key]
return count
= count_last_digit(catalan1)
count1 = count_last_digit(catalan2)
count2 = count_last_digit(catalan3)
count3
# print catalan 1 and catalan 2
def print_catalan(count):
for key in count:
print('The frequency of the last digit', key, 'is ', count[key])
-------- The test case with n = 1000 --------
1 is 0.002
The frequency of the last digit 2 is 0.056
The frequency of the last digit 5 is 0.004
The frequency of the last digit 4 is 0.073
The frequency of the last digit 9 is 0.003
The frequency of the last digit 0 is 0.781
The frequency of the last digit 6 is 0.03
The frequency of the last digit 8 is 0.05
The frequency of the last digit 7 is 0.001
The frequency of the last digit -------- The test case with n = 5000 --------
1 is 0.0004
The frequency of the last digit 2 is 0.0292
The frequency of the last digit 5 is 0.0014
The frequency of the last digit 4 is 0.0414
The frequency of the last digit 9 is 0.0006
The frequency of the last digit 0 is 0.8692
The frequency of the last digit 6 is 0.0224
The frequency of the last digit 8 is 0.0352
The frequency of the last digit 7 is 0.0002
The frequency of the last digit -------- The test case with n = 10000 --------
1 is 0.0002
The frequency of the last digit 2 is 0.0244
The frequency of the last digit 5 is 0.0008
The frequency of the last digit 4 is 0.0319
The frequency of the last digit 9 is 0.0003
The frequency of the last digit 0 is 0.9021
The frequency of the last digit 6 is 0.0162
The frequency of the last digit 8 is 0.024
The frequency of the last digit 7 is 0.0001
The frequency of the last digit
# We can also plot the bar chart to show it
= list(count1.keys())
x1 = list(count1.values())
y1 = list(count2.values())
y2 = list(count3.values())
y3 = range(len(x1))
x
=(13, 9))
plt.figure(figsize# plot the bar separately
=0.4, color='b', label='n = 1000')
plt.bar(x1, y1, width+ 0.4 for i in x1], y2, width=0.4, color='r', label='n = 5000')
plt.bar([i + 0.8 for i in x1], y3, width=0.4, color='g', label='n = 10000')
plt.bar([i 'Last digit of Catalan Number')
plt.xlabel('Frequency of the last digit')
plt.ylabel('The frequency of the last digit of Catalan numbers')
plt.title(
plt.legend()
plt.show()
::include_graphics('static/pic9.png') knitr
We test the case for , , and . From the bar chart and the output of our test, we may deduce from that in basis 10 the last digit of is never . When increase, we can find following - The probability for last digits of Catalan numbers is 0 getting increases. - The probability for last digits of Catalan numbers is even number getting decreases. - The probability for last digits of Catalan numbers is odd number (3 not included) is really small, which means the last digits of Catalan numbers is rarely odd when is a large number.
4.2 The length of large Catalan numbers
Definition 4.1
It can be proved (this is beyond the level of Bachelor 2, a possible reference is p.384 in Ph.Flajolet, R.Sedgewick, Analytic Combinatorics) that for every we have
which yields very good approximations when is large.
We will use this approximation to estimate the length (i.e. the number of digits) of when is a power of ten.
Consider the following table which records the length of , , ,…
Number of digits of | |
5 | |
57 | |
598 | |
6015 | |
60199 | |
602051 |
(For instance, which has digits.)
The goal is to complete the table. (An interesting challenge could be to break the record of this sequence in the Online Encyclopedia of Integer Sequences!) For that purpose you have to
- (Theory) Find somewhere (or reprove) the formula which gives the number of digits of a given integer.
- Use this formula and python to complete the table. Warning: Do not try to explicitly compute since they grow too fast. Instead you need to figure out how to use equation ($) above.
For any positive integer , we assume the digits of is . We have the relation that Then we take the for both side we have Then we can prove that the digits of any given number is
From the equation from Definition 4.1 and above we have and . Then we take the for both side we have Since is a number of power 10, then we denote where and is an integer. Then we have Since and , then we know that goes really fast, therefore tend close to and we can neglect it. Then we have Therefore, we can use the code below to complete the table.
# implement the formula above
def num_digits(n):
= (10**n) * log10(4) - (1 / 2) * log10(pi) - (3 * n / 2) * log10(10)
ub return math.floor(ub) + 1
for i in range(1, 20):
print("The number of digits of Catalan, 10 **", i, "is", num_digits(i))\
10 ** 1 is 5
The number of digits of Catalan, 10 ** 2 is 57
The number of digits of Catalan, 10 ** 3 is 598
The number of digits of Catalan, 10 ** 4 is 6015
The number of digits of Catalan, 10 ** 5 is 60199
The number of digits of Catalan, 10 ** 6 is 602051
The number of digits of Catalan, 10 ** 7 is 6020590
The number of digits of Catalan, 10 ** 8 is 60205987
The number of digits of Catalan, 10 ** 9 is 602059978
The number of digits of Catalan, 10 ** 10 is 6020599899
The number of digits of Catalan, 10 ** 11 is 60205999117
The number of digits of Catalan, 10 ** 12 is 602059991310
The number of digits of Catalan, 10 ** 13 is 6020599913260
The number of digits of Catalan, 10 ** 14 is 60205999132775
The number of digits of Catalan, 10 ** 15 is 602059991327940
The number of digits of Catalan, 10 ** 16 is 6020599913279601 The number of digits of Catalan,
We can see from the result that grows really fast. The number of digits of is and the number of digits of is . Therefore we use the formula gave above and simluated with the python
code.
For larger and larger ’s the right column always begins with the same digits () and explain this.
We have
Since we want to prove that the right column always begins with the same digits (), then we can ignore the smaller part of , which means can be ignore since there are quite small compared to . Then compute with python
code and we have for the value of for . Since , so we consider the marginal case, when , , and , therefore it would not influnce the second digits of . This also applys to the value of with bigger . Then we prove the theorem.
# return the number of 10^k log(4)
def num_digits2(n):
= (10**n) * log10(4)
ub return math.floor(ub)
print("--------- Question 5.2.3 ----------")
print('The value of log(4) is', log10(4))
--------- Question 5.2.3 ----------
4) is 0.6020599913279624 The value of log(