3

We can compute $n^{th}$ Schröder number in $O(n^2)$ using the recurrence relation:

$S_{n+3} = 3 S_{n+2} + \sum_{k=0}^n S_{k+1} * S_{n-k+1}$.

My question is how to compute them faster?

  • 2
    The german Wikipedia article Schröder-Zahlen mentions the following recurrence relation: $(n+1)S_n=(6n-3)S_{n-1}-(n-2)S_{n-2}.$ – Reinhard Meier Apr 11 '18 at 18:50
  • Why can't we start at $S_1$ and go up while storing all the intermediate values of $S_i$ and $S_iS_j$? Alternatively, there is a closed form for $S_n$. – sku Apr 11 '18 at 22:28
  • @sku you can but there is not point in storing the products $S_i S_j$ since the $j$ associated with $i$ changes with $n$. so we will have to compute the summation anyway – Nilesh Hirani Apr 13 '18 at 18:06

2 Answers2

2

The Schroder numbers $S_n$ can be expressed in terms of Legendre polynomials evaluated at $3$ by the following formula \begin{eqnarray*} S_n= \frac{1}{2} (-P_{n-1}(3)+6P_n(3)-P_{n+1}(3)). \end{eqnarray*} Legendre polynomials satisfy the recurrence relation \begin{eqnarray*} (n+1)P_{n+1}(x)=(2n+1)xP_n(x)-nP_{n-1}(x). \end{eqnarray*} Substitute $x=3$ in this formaula and generate the sequence $P_n(3)$ (this gives the sequence $1,3,13,63,\cdots$ which are the central Delannoy numbers.) And now use the first equation to compute the Schroder numbers $1,2,6,22,90,\cdots$.

Donald Splutterwit
  • 36,613
  • 2
  • 26
  • 73
  • Can you add a proof or at least give a link to it for the first equation? – Nilesh Hirani Apr 11 '18 at 19:47
  • Equation $(6)$ here ... http://mathworld.wolfram.com/SchroederNumber.html. And in light of Rienhard Meier's comment ... the formula he states can be derived from the above. – Donald Splutterwit Apr 11 '18 at 20:01
  • Is there a simple formula allowing to compute $P_{3n},P_{3n+1},P_{3n+2}$ from $P_{n},P_{n+1},P_{n+2}$? That would allow to design a fast algorithm based on the ternary representation, similar to the repeat-squaring approach used in the computation of Fibonacci or Lucas numbers. – Jack D'Aurizio Apr 11 '18 at 20:33
  • I know you are aware of the formula \begin{eqnarray} \begin{bmatrix} F_{n-1} &F_n \F_n &F_{n+1} \ \end{bmatrix} =\begin{bmatrix} 0 &1 \1 &1 \ \end{bmatrix}^n. \end{eqnarray} Fibonacci numbers can be rapidly calculated by squaring methods. I do not think there is a similar method that will work for Legendre polynomials because the matrices will change at each stage. @JackD'Aurizio – Donald Splutterwit Apr 11 '18 at 20:49
  • @DonaldSplutterwit: on the other hand, there are reasons for believing that the computation of the Taylor coefficients of $$\frac{1}{\sqrt{1-6x+x^2}}$$ is not terribly harder than the computation of the Taylor coefficients of $$\frac{1}{\sqrt{1-x^2}}$$ (central binomial coefficients) – Jack D'Aurizio Apr 11 '18 at 20:59
  • where can i find proof for the relation with Delannoy numbers? I can understand the derivation of the generating function but don't know how to proceed further. Should I try to derive coefficient of $x^n$ in the generating function? – Nilesh Hirani Apr 12 '18 at 20:39
  • Wiki is always a good bet https://en.wikipedia.org/wiki/Delannoy_number ... & check out Jack's answer. – Donald Splutterwit Apr 12 '18 at 20:42
  • I meant this equation: $S_n = \frac{1}{2}\left(-D_{n-1}+6,D_n-D_{n+1}\right)$ – Nilesh Hirani Apr 13 '18 at 04:52
2

Delannoy numbers can be defined as $$ D_n = [x^n y^n]\frac{1}{1-x-y-xy}=[x^n y^n]\frac{1}{2-(1+x)(1+y)}=\sum_{k\geq 0}\frac{1}{2^{k+1}}\binom{k}{n}^2 = P_n(3)\tag{1} $$ and due to the orthogonality relations in $L^2(0,2\pi)$ the RHS of $(1)$ can be represented as $$ D_n = P_n(3) = \frac{1}{2\pi}\int_{0}^{2\pi}\frac{d\theta}{(3-2\sqrt{2}\cos\theta)^{n+1}} \tag{2} $$ giving both the generating function and the asymptotic behaviour of Delannoy numbers: $$ \sum_{n\geq 0}D_n x^n = \frac{1}{\sqrt{1-6x+x^2}},\qquad D_n\sim\frac{C}{\sqrt{n}}(1+\sqrt{2})^{2n}. \tag{3} $$ Schroeder numbers are related to Delannoy numbers via $S_n = \frac{1}{2}\left(-D_{n-1}+6\,D_n-D_{n+1}\right)$, hence they have a similar generating function, a similar asymptotic behaviour and they can be computed by tackling the integral $\frac{1}{2\pi}\int_{0}^{2\pi}\frac{d\theta}{(3-2\sqrt{2}\cos\theta)^{n}}$ through Gaussian quadrature and rounding to the closest integer.

The existence of an algorithm by repeat squaring (similar to the one allowing a fast computation of Fibonacci or Lucas numbers) does not seem to be investigated in the literature (or, at least, I have not been able to find it) but it is for sure an interesting question, which deserves further thoughts.

Here it is one: for any $R\in(-1,1)$ (in particular for $R=\frac{2\sqrt{2}}{3}$) we have the following identity: $$ \frac{1}{1-R\cos\theta} = \frac{1}{\sqrt{1-R^2}}\sum_{h\geq 0}\left(\frac{R}{1+\sqrt{1-R^2}}\right)^h \cos(h\theta)\tag{4} $$ i.e. we have a Fourier (cosine) series whose coefficients have an exponential decay. A similar identity holds for $\frac{1}{(1-R\cos\theta)^2}$, and given the Fourier cosine series of $\frac{1}{(1-R\cos\theta)^n}$ and $\frac{1}{(1-R\cos\theta)^{n+1}}$, to compute the Fourier cosine series of $\frac{1}{(1-R\cos\theta)^{2n}}$ and $\frac{1}{(1-R\cos\theta)^{2n+1}}$ is simple by convolution. In particular the RHS of $(2)$ can be computed through a repeat-squaring approach: the hardest part of this approach is to understand where the exponential decay allows to truncate the involved Fourier cosine series.

This is also essentially equivalent to understanding how squaring works in the binomial base.
A concrete example is given by the computation of $D_5$:

$$ D_5=\sum_{k\geq 0}\frac{\binom{k}{5}^2}{2^{k+1}}=\sum_{k\geq 0}\frac{a_{10}\binom{k}{10}+a_9\binom{k}{9}+\ldots+a_1\binom{k}{1}+a_0}{2^{k+1}}=a_{10}+a_9+\ldots+a_1+a_0 $$ where we also know that $D_5=\sum_{k=0}^{5}\binom{5}{k}^2 2^{5-k}$.

Jack D'Aurizio
  • 353,855
  • Where can I find derivation of the equation $S_n = \frac{1}{2}\left(-D_{n-1}+6,D_n-D_{n+1}\right)$? – Nilesh Hirani Apr 13 '18 at 05:08
  • Your approach looks nice. But as the number gets quite large with increasing value of $n$, I wanted to compute them modulo a prime and thus don't want to work with floating point numbers. I am sorry I didn't include this in the question. But I guess this will still be useful for future readers – Nilesh Hirani Apr 13 '18 at 05:09
  • @NileshHirani: the computation of $D_n\pmod{p}$ is way easier, since $$ D_n = \sum_{k=0}^{n}\binom{n}{k}^2 2^k$$ and we know, from Lucas theorem, $\binom{n}{k}\pmod{p}$. It is an entirely different problem, and a much simpler one. We also have $D_{p-1}\equiv 1\pmod{p}$, a sort of Fermat's little theorem. – Jack D'Aurizio Apr 13 '18 at 20:23
  • The relation between $S_n$ and $D_{n+{-1,0,1}}$ is a straightforward consequence of the generating functions for $D_n$ and $S_n$; the previous comment also provides an interesting way for computing $D_n$ from the Chinese remainder theorem: it is enough to compute $D_n\pmod{p}$ for a sufficiently large set of primes $p$ ($\leq 2n$ primes). – Jack D'Aurizio Apr 13 '18 at 20:27