Tag Archives: sagemath

Happy Birthday Ramanujam


Today is the 130th birthday of Srinivasa Ramanujam Iyengar.

I will discuss the easiest-to-follow work of Ramanujam, from G. H. Hardy’s Ramanujan: Twelve lectures on subjects suggested by his life and work.

A partition of n is a division of n into any number of positive integral parts. Thus, the sum of digits of 130 = 1+3+0=4 has 5 partitions:

\displaystyle{4 = 3+1 = 2+2 = 2+1+1 = 1+1+1+1}

The order in which the partitions are arranged is irrelevant, so we may think of them, as arranged in descending order. We denote the number of partitons of n by p(n); thus p(4) = 5. Also, by convention, we define p(0) = 1.

Very little was known about the arithmetical properties of p(n); when Ramanujam started his investigations. Though we still don’t know when p(n) is even or odd, there has been a lot of progress in this domain of research. For an overview, see the first section of Ken Ono’s “Distribution of the partition function modulo m” (it’s 17-year-old paper…)

Ramanujam was the first, and up to his death, the only, mathematician to discover the arithmetical properties of p(n). His theorems were discovered by observing Percy MacMahon‘s table of p(n) for the first 200 values of n. Ramanujan observed that the table indicated certain simple congruence properties of p(n). In particular, the numbers of the partitions of numbers 5m+4, 7m+5 and 11m+6 are divisible by 5, 7 and 11 respectively, i.e.

  • p(5m+4) \equiv 0 \pmod{5}
  • p(7m+5) \equiv 0 \pmod{7}
  • p(11m+6) \equiv 0 \pmod{11}

Hence, for example, for n=130+1 (Chinese way of calculating age) p(131) \equiv 0 \pmod{7}. And we can verify this using SageMath:


Now, to check its divisibility by 7, take the last digit of the number you’re testing and double it. Then, subtract this number from the rest of the remaining digits. If this new number is either 0 or if it’s a number that’s divisible by 7, then the original number is divisible by seven. [Derive it yourself!]

This process is lengthy but it converts the process of division by a simpler operation of subtraction.

Here, we have:

5964539504 \rightarrow 596453942\rightarrow 59645390\rightarrow 5964539\rightarrow 596435\rightarrow 59633\rightarrow 5957\rightarrow 581\rightarrow 56 = 7\times 8.

If you know how SageMath calculates the number of partitions, please let me know in the comments below.

A complex log inequality


Let z be a complex number. The power series expansion of \text{log}(1+z) about z_0=0 is given by

\displaystyle{\text{log}(1+z) = \sum_{n=1}^\infty (-1)^{n-1}\frac{z^n}{n} = z-\frac{z^2}{2} + \ldots}

which has radius of convergence 1. If |z|<1 then

\displaystyle{\left|1-\frac{\log(1+z)}{z}\right|\leq \frac{|z|}{2(1-|z|)}}

If we further assume |z|<1/2 then

\displaystyle{\left|1-\frac{\log(1+z)}{z}\right|\leq \frac{1}{2}}

This gives,

\displaystyle{\frac{|z|}{2}\leq |\log(1+z)|\leq \frac{3|z|}{2}, \quad |z|<\frac{1}{2}}

I just wanted to see how this inequality will appear graphically, so here are the plots made using SageMath 7.5.1 (by fixing the real part of z to zero and varying the imaginary part till 1/2)


i = CDF.0; p1 = plot(lambda t: abs(log(1+t*i)), 0, 0.5, rgbcolor=(0.8,0,0),legend_label=’ $|log(1+z)|$’, thickness=2); p2 = plot(lambda t: abs((t*i)/2), 0, 0.5, rgbcolor=(0,0.8,0), legend_label=’$|z|/2$’, thickness=2); p3 = plot(lambda t: abs(3*(t*i)/2), 0, 0.5, rgbcolor=(0,0,0.8), legend_label=’ $3|z|/2$’, thickness=2); p1+p2+p3

I tried to get a graph where this inequlaity fails (i.e. the plots intersect), but failed to do so.

Cross Diagonal Cover – VI


The problem has finally been solved by Matthew Scroggs.

{\text{\# filled squares} = (\mathrm{lcm}(m-1,n-1)+1 )- \frac12\left(\frac{\mathrm{lcm}(m-1,n-1)}{m-1}-1\right)\left(\frac{\mathrm{lcm}(m-1,n-1)}{n-1}-1\right)}

He and I, independently,  found a counterexample  for the conjecture by replacing lowest common multiple by greatest common factor using the relation: ab=\mathrm{lcm}(a,b)\gcd(a,b).

In 15×5, there are 26 filled squares and gcd(15,5)=5, so 15×5 is a counterexample to the conjecture!

As it turns out, SAGE is giving float(26/5) = 5.0 when I run it inside the program, but return 5.2 when I run it independently. Leading to wrong conjecture.

The solution is pretty beautiful and  this is the outline:

There are 3 levels of simplifications of the problem:

Original Grid –> Dual Grid –> Mirror Grid –> Inner-point Grid

Counting the “Corners visited twice” (the subtracted term) was something I wasn’t able to do. Corners visited was essentially what I called “number of steps needed for algorithm to stop“. So, his mirror argument provides a proof without words of that result.

Cross Diagonal Cover – V


This has been an exciting week! Prof. Sukanta Pati proved an interesting theorem that enables us to get decomposition of 2m-1\times n grids into simpler grids, hence simplifying counting to large extent (note that m=n is also allowed). It enables us to surpass the difficulty posed by “more than two crosses in one square”, thus supporting the idea of colouring (i.e. not giving importance to two crosses in a square).

Given m\times n grid which has consisting of m rows and n columns such that the terminating corner square lie in m^{th} row. Let the number of covered squares in m\times n grid be C(m,n) . Then for  2m-1\times n grid we have C(2m-1, n)= 2C(m,n)-\beta(m,n)  where \beta(m,n) is the number of covered squares in m^{th} row of m\times n grid.

Instead of giving its proof, I will give an  example when m=5, n=10.

New Doc 26_1

I omitted the repetitions of crosses (x) since the number of x in each square doesn’t matter. Note that if we reflect 5×10 about the middle of 5th row we will get 9×10.

Here, C(5,10) = 25 and C(9,10) = 2\times 25 - 5 = 45. Using this example you can easily prove the generalized statement by exploiting the bilateral symmetry of grid and x about ythe dotted line.

Though  I suspected that least common multiple of m-1, n-1 determine the number of steps my algorithm evaluates, Pritam Laskar  found the exact formula.

Given a m\times n grid, the Cross Diagonal Cover algorithm terminates after  lcm(m-1, n-1) + 1  steps.

For proof, follow the arguments of grant93jr in his/her comment (he/she mistakenly called their lcm to be their gcd).

Using the SageMath Program, about which I wrote in previous post, I am pretty sure that \gcd(m,n) always divides the number of filled squares. So, now the question is

Why the greatest common divisor of m and n always divides the number of filled squares?


Cross Diagonal Cover – IV


While discussing this problem with Dr. Shailesh Shirali, he commented that there has to be a way to phrase the problem in terms of a ray of light being reflected off the walls of the rectangle, bouncing around, proceeding from one corner to some other corner.

The problem in re-stating my problem in “light reflection” form was that reflections must be from center of squares and this is not the way light “actually” reflects from surfaces. But, I clubbed that idea with “graph theory” (directed graphs), which actually answers the first question I asked in my previous post:

Why no filled square has more than 2 crosses?

As per my “Cross diagonal cover algorithm” we can’t retrace a path and each square has just two diagonals. Hence, if we replace all the squares by their centers then each center is of degree two (i.e. can allow intersection of at most two distinct paths). Hence proving that no filled square can have more than 2 crosses. For example:

New Doc 22_1

Now moving to the bigger question of counting the total number of filled squares, there hasn’t been much progress yet (even on arriving at a concrete conjecture). But, Prof. Amritanshu Prasad wrote a SageMath  program for my algorithm which enables us to find number of the filled squares for individual cases without actually drawing them! For Example: for m=101 , n=102 grid, there will be 5151 = (101 * 102 )/ 2 filled squares. Pretty cool 🙂

SageMath is is an open source implementation of mathematics and scientific software based on Python 2.  Unfortunately, since the SageMath program is essentially a Python script I am not allowed to embed  it in my WordPress.com blog. But, motivated by my discussions with Ms. Marina Ibrishimova, I tweaked Prof. Prasad’s original source-code and added comments (initialized by #) to make it self explanatory.

Here is the SageMath (or Python) code to count the number of filled squares for 2 \leq m\leq n\leq 10


def cover(m,n):
#a function to count the number of squares covered in a grid with m rows
#and n columns
    xinc = 1
#we will use this variable to increment (or decrement) the value of
#x-coordinate (row position) by 1 unit after each step
    yinc = 1
#we will use this variable to increment (or decrement) the value of
#y-coordinate (column position) by 1 unit after each step
    pos = [1, 1]
#we are assuming our grid to have at least 2 rows and 2 columns
#and will initialize counting from (1,1) position.
    visited = [[0, 0], [1, 1]]
#since we start from (1,1) position, we implicitly assume to have
#counted (0,0) position.
    while pos != [m-1, 0] and pos != [0, n-1] and pos != [m - 1, n - 1]:
#whenever we reach any corner the algorithm terminates, so need to include
#position of other three corners apart from starting corner in condition.
          if pos[0] in [0, m-1]:
#evaluates to true if x-coordinate of current position is a
#member of the collection [0,m-1]
             xinc = - xinc
#if x-coordinate is either 0 or m-1 then we will switch diagonal
#by switching the sign of xinc
          if pos[1] in [0, n-1]:
#evaluates to true if y-coordinate of current position is a member
#of the collection [0,n-1]
             yinc = -yinc
#if y-coordinate is either 0 or n-1 then we will switch diagonal
#by switching the sign of yinc
          pos[0] += xinc
#update the x-coordinate of new position as x(new) = x(old) + xinc,
# where xinc = 1 or -1.
          pos[1] += yinc
#update the y-coordinate of new position as y(new) = y(old) + yinc,
# where yinc = 1 or -1.
          if not (pos in visited):
#if this new state is not there in visited positions list
             visited.append([pos[0], pos[1]])
#then add this new position to the list of visited positions.
    return visited 

#---------------END OF FUNCTION---------------

#we will write a small code to be able to use above function to find number of
#filled squares for all grids where m,n lie between 1 and 11 (both excluded).

for i in range (2, 11):
#The range() function provides an easy way to construct a list of integers.
#The range() function only does numbers from the first to the last,
#not including the last.
    for j in range(i, 11):
        print i, j, len(cover(i, j))
# len(indata) counts the bytes of data in indata here it
#counts the number of elements is visited list.

To run the above code, please copy-paste it in SageMathCell and click evaluate button. You will get the list displaying m, n and the number of filled squares in each of the grid (with 1<m,n<11).

In case you want to understand above Python function, here is an illustration when m=3 and n=4:

New Doc 23_1

Note that though (1,1) position is encountered twice but it is added to “visited squares” list only once.

So far I haven’t been able to derive a useful interpretation even after getting access to lot of data. I hope to formulate a conjecture soon…