13

OEIS sequence A186759 is a triangle read by rows:

$T(n,k)$ is the number of permutations of $\{1,2,\dots,n\}$ having $k$ nonincreasing cycles or fixed points,

where a cycle $(b_1\ b_2\ \cdots\ b_m)$ is said to be increasing if, when written with its smallest element in the first position, it satisfies $b_1 < b_2 < b_3 < \cdots < b_m$.

According to the OEIS, this sequence is given by the exponential generating function $$ G(t,z)=\frac{\exp[(1-t)(e^z-1-z)]}{(1-z)^t}. $$

The sequence begins $$ 1 \\ 0\hspace{0.5cm}1 \\ 1\hspace{0.5cm}0\hspace{0.5cm}1 \\ 1\hspace{0.5cm}4\hspace{0.5cm}0\hspace{0.5cm}1\\ 4\hspace{0.5cm}9\hspace{0.5cm}10\hspace{0.5cm}0\hspace{0.5cm}1\\ 11\hspace{0.4cm}53\hspace{0.4cm}35\hspace{0.4cm}20\hspace{0.4cm}0\hspace{0.5cm}1\\ \cdots $$

I learned about this sequence when my Twitter bot @oeisTriangles tweeted out a picture of its "parity triangle" earlier this month. This Twitter bot works by drawing a light color when the value at a position is even and a dark color when it is odd. The resulting image, shown below, appears to be a fractal. (In this image, I drew the first 1024 rows.)

Parity Triangle for OEIS sequence A186759

What is the fractal dimension of this parity triangle in the limit?

Or, if this is too hard to determine, what are some upper/lower bounds?

Peter Kagey
  • 5,438

2 Answers2

11

$\def\FF{\mathbb{F}}$I want to make three points.

(1) The generating function for this sequence $\bmod 2$ is a rational function.

(2) There are good tools for understanding the fractal behavior of the coefficients of rational generating functions modulo $p$.

(3) These tools could, in principle, extract the box counting dimension as $\log \lambda$ where $\lambda$ is an eigenvalue of a certain stochastic matrix.


(1) The generating function for this sequence $\bmod 2$ is a rational function.

I'll write $[n]$ for $\{ 1,2,3, \ldots, n \}$.

I will analyze the behavior of the sequence where $1$-cycles DO count as increasing. For a permutation of $n$, let us define the following statistics: Let $k_i$ be the number of increasing $i$-cycles, let $\ell$ be the number of non-increasing cycles (all of which have length $\geq 3$) and let $m$ be the number of elements in non-increasing cycles. So $n = \sum i k_i + m$.

Given a permutation of $[n]$, there are $n+1$ ways that we can turn it into a permutation of $[n+1]$ by inserting $n+1$ into a cycle. For example, given the permutation $(12)$ of $[2]$, we could turn it into $(132)$, $(123)$ or $(12)(3)$; the first two involve inserting $3$ into the cycle $(12)$, and the last involves making a new $1$-cycle for $3$. Let's see how these affect our statistics.

(a) There are $k_i$ ways that we can insert $n+1$ into an increasing $i$-cycle and keep it increasing. In this case, $k_i$ changes to $k_i -1$ and $k_{i+1}$ changes to $k_{i+1}+1$.

(b) There are $(i-1) k_i$ ways that we can insert $n+1$ into an increasing $i$-cycle and make it non-increasing. In this case, $k_i$ changes to $k_i -1$, $\ell$ changes to $\ell+1$ and $m$ changes to $m+i+1$.

(c) There are $m$ ways that we can insert $n+1$ into a non-increasing cycle. In this case, $m$ changes to $m+1$.

(d) There is $1$ way that we can make $n+1$ into a $1$-cycle. In this case, $k_1$ changes to $k_1+1$.

Things get simpler when we only need to count the number of transitions modulo $2$. Put $p = \sum_{i \equiv 1 \bmod 2} k_i$ and $q = \sum_{i \equiv 0 \bmod 2} k_i$. We remark that $n \equiv p+m \bmod 2$. Here are the new cases:

(a1) There are $p$ ways that we can insert $n+1$ into an increasing odd cycle and keep it increasing. In this case, $(p,q,m)$ changes to $(p - 1, q + 1, m)$.

(a2) There are $q$ ways that we can insert $n+1$ into an increasing even cycle and keep it increasing. In this case, $(p,q,m)$ changes to $(p + 1, q - 1, m)$.

(b1) There are $0 \bmod 2$ ways that we can insert $n+1$ into an odd increasing cycle and make it non-increasing. Since there are $0 \bmod 2$ ways to do this, we don't have to think about it.

(b2) There are $q \bmod 2$ ways that we can insert $n+1$ into an even increasing cycle and make it non-increasing. In this case, $(p,q,m) \bmod 2$ changes to $(p, q-1, m+1) \bmod 2$ and $\ell$ changes to $\ell+1$.

(c) There are $m$ ways that we can insert $n+1$ into a non-increasing cycle. In this case, $(p,q,m)$ changes to $(p, q, m+1)$.

(d) There is $1$ way that we can make $n+1$ into a $1$-cycle. In this case, $(p,q,m)$ changes to $(p+1, q,m)$.

In some cases, we understand exactly how $(p,q,\ell, m)$ change but, in other cases, we only understand how they change modulo $2$.

Let's only ever keep track of $(p, q, m)$ modulo $2$. In this case, our six cases collapse back to four, since $(p-1,q+1,m) \equiv (p+1,q-1,m) \bmod 2$ and we can forget about $(b1)$ completely:

(a) There are $p+q \bmod 2$ ways to insert $n+1$ into an increasing cycle and keep it increasing. In this case, $(p,q,m) \bmod 2$ changes to $(p+1, q+1, m) \bmod 2$.

(b) There are $q \bmod 2$ ways to insert $n+1$ into an even increasing cycle and make in non-increasing. In this case, $(p,q,m) \bmod 2$ changes to $(p, q+1, m+1) \bmod 2$ and $\ell$ changes to $\ell+1$.

(c) There are $m \bmod 2$ ways that we can insert $n+1$ into a non-increasing cycle. In this case, $(p,q,m) \bmod 2$ changes to $(p, q, m+1) \bmod 2$.

(d) There is $1 \bmod 2$ way that we can make $n+1$ into a $1$-cycle. In this case, $(p,q,m) \bmod 2$ changes to $(p+1, q,m) \bmod 2$.

Let $A_{000}(z,t)$ be the generating function for permutations with $(p,q,m) \equiv (0,0,0) \bmod 2$, weighted by $z^{\ell} t^n$, and counted modulo $2$. We define $A_{001}$, $A_{010}$, ..., $A_{111}$ similarly. Then we have eight linear equations: $$ \begin{array}{ccccccccccc} A_{000} &=& && zt A_{011} &+& t A_{001} &+& t A_{100} &+& 1 \\ A_{001} &=& && zt A_{010} && &+& t A_{101} && \\ A_{010} &=& t A_{100} && &+& t A_{011} &+& t A_{110} && \\ A_{011} &=& t A_{101} && &+& &+& t A_{111} && \\ A_{100} &=& t A_{010} &+& zt A_{111} &+& t A_{101} &+& t A_{000} && \\ A_{101} &=& t A_{011} &+& zt A_{110} && &+& t A_{001} && \\ A_{110} &=& && &+& t A_{111} &+& t A_{010} && \\ A_{111} &=& && &+& &+& t A_{011}. && \\ \end{array}$$

The first column describes the effect of case (a), the second column describes the effect of case (b), etcetera. For example, the $zt A_{011}$ term in the first row, second column, indicates that, if $(p,q,m) \equiv (0,1,1) \bmod 2$, then case (b) can occur in $q \equiv 1 \bmod 2$ ways, and the weight $z^{\ell} t^n$ will change to $z^{\ell+1} t^{n+1}$. The $+1$ in the first row is the empty permutation of the empty set.

Eight linear equations in eight variables is a mess for a human, but no problem for a computer. I was just double checking my work and preparing to enter it into Mathematica, when I discovered that I was treating $1$-cycles differently from the OEIS (better, IMNSHO). Fixing this issue gives $16$ linear equations in $16$ variables, so I decided to stop. (Also, you get negative powers of $z$, because inserting $n+1$ into a $1$-cycle changes $\ell$ to $\ell-1$, since the $1$-cycle is considered non-increasing but the resulting $2$-cycle is considered increasing.)

But, in any case, it is clear that each of the $A_{ijk}$'s, and therefore their sum, will be a rational function, in $\FF_2(z,t)$. And this gives us a lot of tools to use.


(2) There are good tools for understanding the fractal behavior of the coefficients of rational generating functions modulo $p$.

Suppose we have a rational function $F(x_1, x_2, \ldots, x_d) = \frac{P(x_1, \ldots, x_d)}{Q(x_1, \ldots, x_d)}$ with coefficients in $\FF_p$. Expand $F$ as a power series $\sum_{e_1, e_2, \ldots, e_d} F_{e_1, e_2, \ldots, e_d} x_1^{e_1} \cdots x_d^{e_d}$. How can we compute the coefficients $F_{e_1, e_2, \ldots, e_d} x_1^{e_1} \cdots x_d^{e_d}$ in terms of combinatorial properties of $(e_1, e_2, \ldots, e_d$)?

A basic example is $\tfrac{1}{1-y(1+x)} = \sum_{k,n} \binom{n}{k} x^k y^n$. Here we can use Lucas's theorem: Write $k$ and $n$ in base $p$ as $k = \sum k_i p^i$ and $n = \sum n_i p^i$, with $0 \leq k_i, n_i \leq p-1$. Lucas's theorem tells us that $\binom{n}{k} = \prod_i \binom{n_i}{k_i} \bmod p$. From this, it is easy to recover the standard Sierpinski gasket image of Pascal's triangle modulo $2$.

enter image description here

This example is pretty, but too simple to show the general case. On the other hand, the OP's example is way too big for my taste. I'm going to compromise and, as my running example, use the trinomial coefficients (OEIS A027907): $$\frac{1}{1-y(1+x+x^2)} = \sum \binom{n}{k}_3 x^k y^n.$$ Here is a picture of the trinomial coefficients modulo $2$. (Here $0 \leq n \leq 127$ and $0 \leq k \leq 255$.)

enter image description here

The key tool that we are going to use is the multivariate version of Christol's theorem. I couldn't find an online reference for the multivariate case. In print, see Chapter 14 of Allouche and Shallit, Automatic Sequences. I'll state everything for two variable functions, although the $d$-variable case is exactly the same with more indices.

Here is the usual statement of Christol's theorem (in multiple variables): Let $\sum F_{nk} x^k y^n$ be a formal power series with coeffcients in $\FF_p$. Write $k$ and $n$ in base $p$ as $k = \sum k_i p^i$ and $n = \sum n_i p^i$, with $0 \leq k_i, n_i \leq p-1$. Then the following are equivalent:

(1) There is a finite state automata (FSA) which, when fed the inputs $(n_0, k_0)$, $(n_1, k_1)$, ..., $(n_r, k_r)$, outputs $F_{nk}$.

(2) The power series $\sum F_{nk} x^k y^n$ is algebraic over $\FF_p(x,y)$.

I prefer a different formulation, which will not require you to learn what an FSA is. Condition (1) is equivalent to:

(1') There is a positive integer $D$ for which we can find $p^2$ many $D \times D$-matrices $C(i,j)$, for $0 \leq i,j \leq p-1$, and two vectors $\vec{u}$ and $\vec{v}$, such that $$F_{nk} = \vec{v}^T C(k_r, n_r) C(k_{r-1}, n_{r-1}) \cdots C(k_1, n_1) C(k_0, n_0) \vec{u}.$$ The entries of the matrices and the vectors are in $\FF_p$.

So, Lucas' theorem is the case where $D=1$, $C(i,j)$ is the $1 \times 1$ matrix ${\big[} \binom{j}{i} {\big]}$ and $\vec{u} = \vec{v} = [1]$.

To see the equivalence of (1) and (1'), given an FSA with $D$ states, we can make $D \times D$ matrices which encode the transitions of the FSA. We then use $\vec{u}$ to encode the initial state, and $\vec{v}$ to encode the final output. Conversely, given $D \times D$ matrices, we can make an FSA with $p^D$ states which keeps track of the matrix product.

I like (1') better because (a) I think most people know matrices better than FSA's (b) the matrices can be much smaller than the FSA and (c) condition (1') is what shows up naturally when you do computations.

Where do conditions (1) and (1') come from? Define $p^2$ many operators $c_{ij}$ from the vector space of formal power series to itself by the equation $$F(x,y) = \sum_{i,j=0}^{p-1} x^i y^j c_{ij}(F)(x^p, y^p).$$ Thus, the coefficient of $x^k y^n$ in $F(x,y)$ is the coefficient of $x^0 y^0$ in $$c(k_r, n_r) c(k_{r-1}, n_{r-1}) \cdots c(k_1, n_1) c(k_0, n_0) (F).$$ For any power series $F$, let $X$ be the set of all power series obtained by repeatedly applying the operators $c_{ij}$ to $F$ in any order, and let $V$ be the $\FF_p$-vector space spanned by $X$. Then (1) is equivalent to "$X$ is finite" and (1') is equivalent to "$V$ is finite dimensional". The states of the FSA are the elements of $X$, with the transition rules encoding how the $c_{ij}$ map $X$ to itself; the vectors in $\FF_p^D$ are the elements of $V$ (in some chosen basis) and the maps $C(i,j)$ are, again, the way that $c_{ij}$ maps $V$ to itself.

Thus, to make Christol's theorem concrete for some algebraic function $F(x,y)$, we need to find a finite dimensional vector space $V$, containing $F(x,y)$, which is mapped to itself by all the $c_{ij}$. In order to compute $c_{ij}(F)$, you extract the coefficients of $x^n y^k$ where $(n,k) \equiv (i,j) \bmod p$. If $F(x,y)$ is a rational function $\tfrac{P(x,y)}{Q(x,y)}$, then you can most easily do this by writing $F(x,y) = \tfrac{P(x,y) Q(x,y)^{p-1}}{Q(x^p, y^p)}$, expanding the numerator and grouping terms by the exponents modulo $p$. Then $c_{ij}(\tfrac{P}{Q})$ will be of the form $\tfrac{R}{Q}$, where $R$ is produced by the "expand and group" procedure.

This sounds very abstract, so let's go back to our trinomial example. I'll take $V$ to be the $2$-dimensional vector space of rational functions of the form $\tfrac{a+bx}{1+y+xy+xy^2}$. We compute $$\tfrac{a+bx}{1+y+xy+x^2y}=\tfrac{(a+bx)(1+y+xy+x^2y)}{(1+y+xy+x^2y)^2}=$$ $$\tfrac{a}{1+y^2+x^2y^2+x^4 y^2} + x \tfrac{b}{1+y^2+x^2y^2+x^4 y^2} + y \tfrac{a + a x^2 + b x^2}{1+y^2+x^2y^2+x^4 y^2} + xy \tfrac{a + b + b x^2}{1+y^2+x^2y^2+x^4 y^2}.$$ We compute that $$c_{00}\left( \tfrac{a+bx}{1+y+xy+x^2y} \right) = \tfrac{a}{1+y+xy+x^2y} \qquad c_{10}\left( \tfrac{a+bx}{1+y+xy+x^2y} \right) = \tfrac{b}{1+y+xy+x^2y}$$ $$c_{01}\left( \tfrac{a+bx}{1+y+xy+x^2y} \right) = \tfrac{a+(a+b)x}{1+y+xy+x^2y} \qquad c_{11}\left( \tfrac{a+bx}{1+y+xy+x^2y} \right) = \tfrac{(a+b)+bx}{1+y+xy+x^2y} . $$ In particular, we see that each of the $c_{ij}$ really does map the vector space of rational functions of the form $\tfrac{a+bx}{1+y+xy+xy^2}$ to itself.

In the case of a general rational function, if we want to study $\tfrac{P(x,y)}{Q(x,y)}$, we consider the vector space of rational functions of the form $\tfrac{N(x,y)}{Q(x,y)}$, where the exponents of $N(x,y)$ lie in some polytope. Presumably, one could write down the right polytope to use in terms of the Newton polytopes of $P$ and $Q$, but I found trial and error was easier than abstract thought here.

Equation $(\ast)$ says that $c_{00}$ transforms the numerator by $(a+bx) \mapsto a$. In the basis $(1, x)$ this means that $c_{00}$ acts by the matrix $\begin{bmatrix} 1&0 \\ 0&0 \\ \end{bmatrix}$. We read off each of the matrices similarly:

$$ C(0,0) = \begin{bmatrix} 1&0 \\ 0&0 \\ \end{bmatrix} \qquad C(1,0) = \begin{bmatrix} 0&1 \\ 0&0 \\ \end{bmatrix}$$ $$C(0,1) = \begin{bmatrix} 1&0 \\ 1&1 \\ \end{bmatrix} \qquad C(1,1) = \begin{bmatrix} 1&1 \\ 0&1 \\ \end{bmatrix} . $$

Now, we want to compute the coefficient of $x^0 y^0$ in $c(k_r, n_r) c(k_{r-1}, n_{r-1}) \cdots c(k_1, n_1) c(k_0, n_0) \tfrac{1}{1+y+xy + xy^2}$. The initial power series $\tfrac{1}{1+y+xy + xy^2}$ corresponds to the column vector $\begin{bmatrix} 1\\ 0 \end{bmatrix}$. Extracting the coefficient of $x^0 y^0$ in our basis is dot product with the row vector $\begin{bmatrix} 1&0 \end{bmatrix}$.

So we extract the "trinomial Lucas formula" $$\binom{n}{k}_3 \equiv \begin{bmatrix} 1 & 0 \end{bmatrix} C(k_r, n_r) C(k_{r-1}, n_{r-1}) \cdots C(k_1, n_1) C(k_0, n_0) \begin{bmatrix} 1\\0 \end{bmatrix} \bmod 2$$


(3) These tools could, in principle, extract the box counting dimension as $\log \lambda$ where $\lambda$ is an eigenvalue of a certain stochastic matrix.

And now, the final part! Look back at that plot of the trinomial coefficients. It has some big white spaces. For example, the box shown in blue below is the region $112 \leq k \leq 127$ and $64 \leq n \leq 89$.

enter image description here

Or, in base $2$, it is all the numbers where $k$ starts $(0111 \cdots)_2$ and $n$ starts $(0100 \cdots)_2$. The important thing is that this isn't just true at this particular $128 \times 256$ resolution. At any scale, for any $(k,n)$ with binary expansions starting $(0111\cdots,\ 0100\cdots)$, we will have $c(k,n)=0$. We can see this by computing the matrix product $$\begin{bmatrix} 1 & 0 \end{bmatrix} C(0,0) C(1,1) C(1,0) C(1,0) \cdots = \begin{bmatrix} 1 & 0 \end{bmatrix} \begin{bmatrix} 1&0 \\ 0&0 \\ \end{bmatrix} \begin{bmatrix} 1&1 \\ 0&1 \\ \end{bmatrix} \begin{bmatrix} 0&1 \\ 0&0 \\ \end{bmatrix} \begin{bmatrix} 0&1 \\ 0&0 \\ \end{bmatrix} \cdots = \begin{bmatrix} 0&0 \end{bmatrix}$$ regardless of what the future factors are. (There are examples that use fewer digits, but they land outside the triangle, and I thought a box in the interior would be more impressive.)

In general, if $\begin{bmatrix} 1 & 0 \end{bmatrix} C(k_r, n_r) C(k_{r-1}, n_{r-1}) \cdots C(k_q, n_q)$ is $\begin{bmatrix} 0&0 \end{bmatrix}$, we will get a white square corresponding to any $(k,n)$ whose binary expansions start $((k_r k_{r-1} \cdots k_q)_2, (n_r n_{r-1} \cdots n_q)_2)$, irrespective of how many factors come afterwards.

This suggests coloring the grid according to the value of the product $\begin{bmatrix} 1 & 0 \end{bmatrix} C(k_r, n_r) C(k_{r-1}, n_{r-1}) \cdots C(k_q, n_q)$. I'll use white for $\begin{bmatrix} 0&0 \end{bmatrix}$, green for $\begin{bmatrix} 0&1 \end{bmatrix}$, red for $\begin{bmatrix} 1&0 \end{bmatrix}$ and black for $\begin{bmatrix} 1&1 \end{bmatrix}$. Here are the results for $r=0$, $r=1$, $r=2$ and $r=6$.

enter image description here enter image description here enter image description here enter image description here

You can see the fractal form start to emerge.

In particular, note that any box which is white at a given stage remains white at all future stages. More generally, we have the following transformation rules: Each of the four tiles in the top row, transforms into the $2 \times 2$ grid in the bottom row.

enter image description here enter image description here

So, let's compute box counting dimension! We just need to count the number of colored (red, green or black) boxes. Letting $w(k)$, $g(k)$, $r(k)$ and $b(k)$ be the number of white, green, red and black boxes at level $k$, we have $$\begin{bmatrix} w(k+1) \\ g(k+1) \\ r(k+1) \\ b(k+1) \\ \end{bmatrix} = \begin{bmatrix} 4 & 2 & 0 & 0 \\ 0 & 1 & 1 & 2 \\ 0 & 0 & 2 & 2 \\ 0 & 1 & 1 & 0 \\ \end{bmatrix} \begin{bmatrix} w(k) \\ g(k) \\ r(k) \\ b(k) \\ \end{bmatrix}. $$ (Just count the boxes of each color in the transformation rule.) This matrix has eigenvalues $4$, $1 \pm \sqrt{5}$ and $1$. The eigenvector corresponding to $4$ is $[1 \ 0 \ 0 \ 0 ]^T$, indicating that, for large $k$, $w(k)$ is roughly $4^k$ (almost all the boxes are white) and the number of colored boxes are growing slower than $4^k$.

The next largest eigenvalue is $1+\sqrt{5} \approx 3.23$, with eigenvector $[(-3-\sqrt{5}) \ 2\ 2\ (-1+\sqrt{5})]^T$. So the numbers of red, green and black boxes grow like $(1+\sqrt{5})^k$ (and in the proportions $2:2:(-1+\sqrt{5})$).

The number of colored boxes is $\approx c(1+\sqrt{5})^k$ for some constant $c$, and so the box counting dimension is $$\lim_{k \to \infty} \frac{\log (c(1+\sqrt{5})^k)}{\log 2^k} = \frac{\log( (1+\sqrt{5})}{\log 2}\approx 1.69.$$


To review: The first big section shows how to get a rational generating function in $\mathbb{F}_2(x,y)$ for the triangle you are interested in. The second section shows how to derive a Lucas like rule for the coefficients. And the third section shows you how to compute a box counting dimension. Have fun!

  • 2
    Let me also say explicitly -- other users are very welcome to scoop me. If you know how to compute the box-counting dimension of sets like ${ (i,j) : F_{ij} \equiv 1 \bmod 2 }$ where $\sum F_{ij} z^i t^j$ is a rational generating function, then please write it up! My solution is a mess, and I bet this is something which is in the literature and I just didn't know where to search for it. – David E Speyer Nov 05 '22 at 16:57
  • 2
    Or if you want to work out the actual rational function! – David E Speyer Nov 05 '22 at 17:16
6

(Too long for a comment, just a numerical guess...)


I wrote an optimized implementation in C (using GMP) of the second Maple program from https://oeis.org/A186759 for coefficients modulo 2. I verified that the first 100 lines of output matched a separate Haskell implementation and another C implementation not using GMP.

Asymptotics for $O(n^2)$ output characters ($n$ lines): $O(n^3)$ time, $O(n^2)$ space.

Output is little-endian and left-aligned instead of big-endian and symmetric but this should not affect the dimension.

I generated a large image (65536x65536) and numerically estimated the fractal dimension by box counting as about $$D = 1.799\ldots$$

This is not a proof, of course.

Plot

log log plot of N(r) against r

Data

Output from box-counter in verbose mode:

$\log_2 r$ $N(r)$
0 215477756
1 100568193
2 28899802
3 8299116
4 2383433
5 684555
6 196657
7 56512
8 16246
9 4673
10 1347
11 389
12 113
13 33
14 10
15 3
16 1

Usage

./A186759mod2 |
./triangletopgm 65536 65536 |
pnmtopng -interlace > nano A186759mod2.png
./box-counter -i A186759mod2.png -m '#000'

A186759mod2.c

Compile with

gcc -o A186759mod2 A186759mod2.c -lgmp -std=c99 -Wall -Wextra -pedantic

Source code

#include <stdio.h>
#include <stdlib.h>

#include <gmp.h>

typedef signed long Z; #define N 65536

mpz_t b[N];

int main(int argc, char **argv) { (void) argc; (void) argv;

mpz_t binomial, t; mpz_init(binomial); mpz_set_ui(binomial, 1); mpz_init(t); mpz_init(b[0]); mpz_set_ui(b[0], 1); mpz_out_str(stdout, 2, b[0]); putchar('\n'); for (Z n = 1; n < N; ++n) { mpz_init(b[n]); for (Z i = 1; i <= n; ++i) { if (mpz_tstbit(binomial, i - 1)) { switch (i) { case 1: mpz_mul_2exp(t, b[n - i], 1); mpz_xor(b[n], b[n], t); break; case 2: mpz_xor(b[n], b[n], b[n - i]); break; default: mpz_mul_2exp(t, b[n - i], 1); mpz_xor(b[n], b[n], t); mpz_xor(b[n], b[n], b[n - i]); break; } } } mpz_out_str(stdout, 2, b[n]); putchar('\n'); mpz_set(t, binomial); mpz_mul_2exp(binomial, binomial, 1); mpz_xor(binomial, binomial, t); } return 0; }

triangletopgm.c

Compile with

gcc -o triangletopgm triangletopgm.c -std=c99 -Wall -Wextra -pedantic -O3

Source code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv) { if (argc != 3) return 1; int width = atoi(argv[1]); int height = atoi(argv[2]); unsigned char buffer = malloc(width); printf("P5\n%d %d\n255\n", width, height); for (int y = 0; y < height; ++y) { memset(buffer, 0, width); for (int x = 0; x < width; ++x) { int c = getchar(); if (c == '1') buffer[x] = 255; if (c == '\n') break; if (c == EOF) break; } fwrite(buffer, width, 1, stdout); } return 0; }

box-counter

https://mathr.co.uk/mandelbrot/box-counter-0.2.zip does the box counting (supports very large images, albeit limited by system memory).

Claude
  • 5,852