Thursday, February 29, 2024

Happy Leap Day 2024 and a modest proposal of determining leap years.

Today is February 29, 2024, a leap day. It occurs once during leap years once every 4 years when the year is divisible by 4, with some exceptions. Every 400 years, 3 of these years are not leap years. In particular, every century year (e.g. 1800, 1900, ...) that is not divisible by 400 is not a leap year. So there are 97 leap years every 400 years. 

The reason for that is that the mean tropical year which describes a complete cycle of seasons is the basis of the Gregorian calendar.  Currently (as the mean tropical year drifts over thousands of years), the mean tropical year is about 365 days, 5 hours, 48 minutes, 45 seconds = 365.2421875 days. Therefore 97/400 = 0.2425 approximates well the fraction 0.2421875 thus the current rule of leap years. However, there is a small mismatch which might needs to be compensated over hundreds of years.

Since 2421875/10000000 = 31/128, it might be better to have 31 leap years every 128 years, but the resulting rule might be too complicated for years written in decimal. However, if we represent years in binary (which how it is represented in modern computers anyway), then a possible rule is relatively simple. We could define a leap year as a year where in binary, the least significant 2 bits is 0, and the least significant 7 bits are not all 0.  Under this scheme, years divisible by 4 are leap years, except for a year that is divisible by 128. For instance, 1920 and 2048 will not be leap years in this scheme.

Looking at the continued fraction convergents (which describe the best rational approximations) of 2421875/10000000: 1/4, 7/29, 8/33, 31/128, there are other rules possible, but besides 1/4 (once every 4 years) none of the other intermediate convergents lead to an interesting rule.

Monday, February 26, 2024

Python module implementing various classes of graphs for networkx

 I have recently uploaded a python module to pypi:

  1. graph-generators: Python functions to compute various classes of networkx graphs. Available at: graph-generators · PyPI Some of the graphs include Keller graphs, Antelope graphs, Prism graphs, Lucas cube graphs, etc.

Square pandigital numbers

Let a pandigital number in base $n$ be defined as a number that uses the digits from $0$ to $n-1$ exactly once. See OEIS sequence A050278 for pandigital numbers in base 10 (This is called pandigital version 1 in OEIS. Pandigital version 2 are numbers that uses the digits from $0$ to $n-1$ at least once). Adam Partridge gave a wonderful proof of why no pandigital numbers in base 13 can be squares. In particular, he showed in his blog post that if $n$ is odd and $n-1$ has an even 2-adic valuation (OEIS A007814), then there are no pandigital squares in base n. His proof goes as follows: first write a number $m$ as $\sum_{i=0}^{n-1}a_i n^i$ where $a_i$ are the $n$-ary digits of $m$. Next, note that 

$$m-\sum_{i=0}^{n-1}a_i=\sum_{i=0}^{n-1}a_i(n^i-1)=\sum_{i=1}^{n-1}a_i(n^i-1).$$ 

For a pandigital number $\sum_i a_i = n(n-1)/2$. Since  $\sum_i a_i$ is divisible by $\frac{n-1}{2}$ and $n^i-1 = (n-1)(1+n+...+n^{i-1})$ is divisible by $n-1$ for $i>0$, the number $m$ can be factored as

$$m=\frac{n-1}{2}\left(n+2\sum_{i=1}^{n-1} a_i(1+n+...+n^{i-1})\right)$$

If $n$ is odd and $n-1$ has an even 2-adic valuation, then the term $n+2\sum_{i=0}^{n-1}a_i(1+n+...+n^{i-1})$ is odd (i.e. has zero 2-adic valuation) and $(n-1)/2$ has an odd 2-adic valuation, then $m$ has an odd 2-adic valuation and thus cannot be a square, which concludes the proof.

It is interesting to note that the equation of the factorization above does not include $a_0$, the least significant digit. Of course, $a_0$ does play a role since the pandigital property implies that $a_i\neq a_0$ for all $i>0$ and $a_0$ can be obtained from the other digits via $a_0=n(n-1)/2-\sum_{i=1}^{n-1}a_i$.

For an extension of this result, let's consider now zeroless pandigital number in base $n$, i.e. numbers that use the digits from $1$ to $n-1$ exactly once. Then again $\sum_i a_i = n(n-1)/2$ and the same argument shows that if $n$ is odd and $n-1$ has an even 2-adic valuation, then there are no zeroless pandigital square numbers in base $n$.

For numbers $m$ in base $n$ that uses the digits from $0$ to $n-2$ exactly once, $\sum_i a_i = (n-1)(n-2)/2$ and 

$$m=\frac{n-1}{2}\left(n-2+2\sum_{i=1}^{n-2} a_i(1+n+...+n^{i-1})\right)$$ 

and again this shows that if $n$ is odd and $n-1$ has an even 2-adic valuation, then there are no such square numbers in base $n$. Similarly, for such $n$, there are no such square numbers in base $n$ that uses the digits from $1$ to $n-2$ exactly once.

Consider next square numbers in base $n$ whose digits are taken from a range $[j,j+1,..., k]$ exactly once for some $0\leq j\leq k<n$ (the largest of which are listed in OEIS A370371).

If such numbers have $n$ digits, then the range must be of the form $[0,..,n-1]$. If such numbers have $n-1$ digits, then the range must be of the form $[1,..,n-1]$ or $[0,..,n-2]$. The above discussion shows that if $n$ is odd and $n-1$ has even 2-adic valuation, then such square numbers must have at most $n-2$ digits and they cannot be in the range $[1,..,n-2]$. The largest base $n$ number of $n-2$ digits that uses each digit exactly once has digits in the range $[2,...,n-1]$ and is equal to $a=\sum_{i=2}^{n-1} i\times n^{i-2}$ (OEIS A370671) and this is an upper bound of OEIS A370371 when $n$ is odd and $n-1$ has an even 2-adic valuation.