Saturday, February 16, 2019

Mathematics puzzle

This is an interesting maths puzzle. Find the sum of the following expression: \[ S = \sum_{k=0}^{\infty} \frac{k^{2}}{2^k}.\] Solution: Define \(x=log(2)\), so we can rewrite \(S\) as \[ S = \sum_{k=0}^{\infty} \frac{k^{2}}{e^{kx}}.\] We will then play fast and loose with the rules of calculus and calculate the 2nd antiderivative of the above expression, with respect to \(x\). \[D^{-2}S = \sum_{k=0}^{\infty} \frac{k^{2}}{(-k)(-k)}e^{-kx} = \sum_{k=0}^{\infty}e^{-kx}.\] This is in a much nicer form, as we can see it is a geometric sum, and so we can calculate the sum: \[D^{-2}S = \sum_{k=0}^{\infty}e^{-kx} = \frac{1}{1-e^{-x}}, \] which is valid for \(|e^{-x}| < 1 \implies x > 0\), which is good, since we defined \(x\) as \(\log(2)\). Now we have \(D^{-2}S\) in a closed form, we can simple differentiate (w.r.t. \(x\)) twice to find a closed form expression for our original expression, \(S\). Doing so gives, \[S = \frac{d^{2}}{dx^{2}}\frac{1}{1-e^{-x}}= e^{-x}(1-e^{-x})^{-2}(1 + 2e^{-x}(1-e^{-x})^{-1}).\] Since we know \(e^{-x} = 1/2\), we can subsitute it into the expression to get \[ S = \frac{1}{2}.(1-\frac{1}{2})^{-2}(1 + 2.\frac{1}{2}(1-\frac{1}{2})^{-1}) = 2\times3=6.\] So the answer to the original question, is \(6\). Let's just verify this with some quick calculation, in case we are not yet convinced. Using the J language we can calculate the sum of the first 1000 integers to approximate \(S\).

n=: i.1000
e=: *: % (2&^)
S=: +/@:e
S n


This gives (as we expected) 6, thus giving some kind of verification for our answer. Let's just extend this slightly... what if we have an expression \[T= \sum_{k=0}^{\infty} \frac{k}{2^k}?\] We can follow the same argument as above, except we only need to calculate the antiderivative once, and so the derivative of the closed form geometric sum once as well. This gives \[T=e^{-x}(1-e^{-x})^{-2},\] and we can substitute \(1/2\) in place of the exponential term to give, \[T=\frac{1}{2}\times(1-\frac{1}{2})^{-2} = 2.\] Let's do the calculation (for the first 1000 terms of \(T\)).

n=: i.1000
e2=: % (2&^)
T=: +/@:e
T n


This gives 2, as we expected.

ODBC with J to connect to MySQL Database

If you are using the J programming language and wish to connect to a DBMS (MySQL, PostgreSQL, etc), you can use J's ODBC interface. W...