Pretty Printing a Tree in a Terminal

06/12/2016

More often than I’d like, simple tasks turn out to be not that simple. For example, displaying (beautifully) a binary tree for debugging purpose in a terminal. Of course, one could still use lots of parentheses, but that does not lend itself to a very fast assessment of the tree. We could, however, use box drawing characters, as in DOS’s goode olde tymes, since they’re now part of the Unicode standard.

tree-01

Read the rest of this entry »


The infinite radicals of Ramanujan

29/11/2016

What if I asked you to find the numerical value of

\displaystyle \sqrt{1+2\sqrt{1+3\sqrt{1+4\sqrt{1+5\sqrt{1+6\sqrt{\cdots}}}}}} ?

If you have difficulty figuring out what this thing is, don’t worry. You’re not the only one. This question is one the problems posed by Srinivasa Ramanujan, one of the most brilliant and, well, mysterious mathematicians of all time. In fact, it was head enough that Ramanujan had to give the answer a few months later.

Read the rest of this entry »


Tweet time!

22/11/2016

I’ve been using twitter for about five years, and I wondered if my use of it changed over time, and more precisely, linked to my wake/sleep cycle. That’s fortunately kind of simple to check because you can simply request your whole Twitter archive, delivered as a plain CSV File! Let’s see how we can juice it.

Read the rest of this entry »


Square Roots (Part V)

15/11/2016

Last week we examined the complexity of obtaining k in the decomposition n=2^k+b for some integer n. This week, we’ll have a look at how we can use it to speed-up square root extraction. Recall, we’re interested in k because

2^k \leqslant 2^k+b < 2^{k+1},

with 0 \leqslant b < 2^k, which allows us to get easy bounds on \sqrt{n}. Better, we also have that

\sqrt{2^k} \leqslant \sqrt{2^k+b} \leqslant \sqrt{2^{k+1}},

and we know how to compute \sqrt{2^k}=2\frac{k}{2} (somewhat efficiently! Let’s combine all this and see how it speeds up things.

Read the rest of this entry »


Square Roots (part IV)

08/11/2016

In a previous post, we noticed that

\sqrt{2^k} \leqslant \sqrt{n} = \sqrt{2^k+b} \leqslant \sqrt{2^{k+1}},

where 0 \leqslant b < 2^k, could be used to kick-start Newton's (or another) square root finding algorithm. But how fast can we find k and b in this decomposition?

Read the rest of this entry »


Regula Falsi

01/11/2016

The regular falsi, or method of false position, is a method to solve an equation in one unknown, from an initial “guess”. Guesses are progressively refined, by a rather simple method as we will see, until the exact answer is reached or until convergence is reached. The method is useful when you don’t really know how to divide by arbitrary values (as it was the case in Ancient Times) or when the equation is cumbersome.

Read the rest of this entry »


Horner’s Method

25/10/2016

It’s not like evaluating polynomial is something that comes up that frequently in the average programmer’s day. In fact, almost never, but it brings up a number of interesting questions, such as, how do we evaluate it very efficiently and how much of a simplification in the computation is actually a simplification?

The generic polynomial has the form

a_0+a_1x+a_2x^2+a_3x^3+\cdots+a_nx^n.

If we evaluate this naïvely, we end up doing O(n^2) multiply and O(n) additions. Even using the fast exponentiation algorithm, we still use O(n \lg n) multiplies. But we can, very easily, get down to O(n).

Read the rest of this entry »


Fractional Bits (Part III)

18/10/2016

A long time ago, we discussed the problem of using fractions of bits to encode, jointly, values without wasting much bits. But we considered then only special cases, but what if we need to shift precisely by, say, half a bit?

But what does shifting by half a bit even means?

Read the rest of this entry »


Count like an Egyptian (Part III)

11/10/2016

In previous posts, I’ve looked into unit fractions, also known as “Egyptian fractions”, fractions where the numerator is 1 and the denominator n, some integer. We have noticed that finding good representation for a fraction a/b as a sum of unit fraction is not as trivial as it may first seem. Indeed, we noticed that the greedy algorithm, the one that always pick the largest unitary fraction that does not exceed the rest as the next term, may lead to long series of unit fractions, sometimes with large denominators, even for simple fraction. For example,

\displaystyle \frac{412}{1000}=\frac{103}{250}=\frac{1}{3}+\frac{1}{13}+\frac{1}{574}+\frac{1}{699563}.

In a previous post, I suggested decomposing a/b in an “easy way”, which was often better than the greedy algorithm. Surely, we can do better, but how?

Read the rest of this entry »


Yet Another Square Root Algorithm (part III)

04/10/2016

Finding good, fast, and stable numerical algorithms is usually hard, even for simple problems like the square root, subject we’ve visited a number of times. One method we haven’t explored yet, is using the Taylor series. The Taylor series for \sqrt{n} around a is given by:

\displaystyle \sqrt{n}=\sqrt{a}+\frac{1}{2}\frac{n-a}{\sqrt{a}}-\frac{1}{8}\frac{(n-a)^2}{a^{3/2}}+\frac{1}{16}\frac{(n-a)^3}{a^{5/2}}+\cdots

Can we exploit that to compute efficiently, and with some precision, square roots?

Read the rest of this entry »