Saturday, April 16, 2016

Cause and effect

We went to Batman Vs Superman, we also watched Mystery Men a few weeks ago. Awesome conversation with my son Sean:

S: Why is he wearing glasses? Superman doesn't need glasses.
Me: When he's Clark Kent, he wears glasses so people don't think he's Superman.
S: ... Like Captain Amazing.
Me: Yes, just like Captain Amazing :)

Friday, January 08, 2016

(Another) Solution: Writting year as sums of runs of numbers

The solution of just grinding out of the algebra is fine but it's a bit dull and unintuitive. We can use the discussion at the end of the last post to produce a simple proof with almost no algebra.

What we're going to prove is that the number of sequences of consecutive integers that sum to \(Y\) is equal to twice the number of odd divisors of \(Y\).

First off note that if the sum is positive, then there must be more positive numbers in the sequence than negatives. I'm going to assume it's positive, if it's negative then the procedure works, just with the signs changed.

Next, note that the solutions to the problem always come in pairs. Every sequence of even length has a corresponding sequence of odd length. You can construct one from the other as follows.

If 0 appears in the sequence then you cancel every negative number with it's opposite positive number and remove the zero. Every time you cancel a \(-\) with a \(+\) you remove 2 elements from the sequence, so it stays odd or even. Finally removing the 0 flips from odd to even or vice versa. E.g. $$ -3 + -2 + -1 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 $$ becomes $$ 4 + 5 + 6 + 7 $$ 2 slightly non-obvious cases of this are sequences beginning with 0 or 1. They have no negative elements, so you just either add or remove 0 at the start.

Conversely if the sequence does not contain 0, prepend all the numbers from 0 up to the start of the sequence and also prepend their negatives. This does not change the sum but it flips the length from even to odd or vice versa. E.g. $$ 4 + 5 + 6 + 7 $$ becomes $$ -3 + -2 + -1 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 $$

So now we just consider the solutions of odd length. If we can show that there is exactly one of those for every odd divisor of \(Y\), we're done. That turns out to be fairly easy.

First, lets say you have an odd length sequence that sums to \(Y\). Since it's odd-length, it must have a middle number, let's say \(n\) and then there are \(k\) numbers before and \(k\) numbers after. The befores and afters go together in pairs. \(n-1\) with \(n+1\), \(n-2\) with \(n+2\) etc. Each of these pairs sums to \(2n\) and there are \(k\) pairs plus a lone \(n\) from the middle giving a total of \((2k+1)n\). So we have that \(Y=(2k+1)d\) and so \(2k+1\) (the length of the sequence) must be an odd divisor of \(Y\). So this shows that any sequence that sums to Y leads to a specific odd divisor of Y.

Conversely given an odd divisor of \(Y\), \(2k+1\) and we set \(n = Y/(2k+1)\) then the sequence of length \(2k+1\) centred on on \(n\) sums to \(Y\). So every odd divisor leads to a sequence of odd-length and we're done.

Sunday, January 03, 2016

Solution: Writting year as sums of runs of numbers

It turns out the answer is fairly well known and involves what are known as polite numbers but I hadn't heard of them before.

So in order to solve this, we consider sequences of positive consecutive integers. I've added "positive" in there because sequences that are all negative are just positive sequences with the sign flipped and so are not interesting. Sequences that start negative and go positive are also not interesting as you can a bunch of the numbers will cancel out and you'll be left with a sequence that's purely negative or a purely positive.

There's a well-known formula for the sequences starting at \(1\) and that the formula for triangular numbers: \(1 + 2 + ... + n = n(n+1)/2\). We can use this to get a formula for sequences that don't start at \(1\). The sum \((k+1) + (k+2) + ... + (k+n)\) is just the sum from 1 to n minus the sum from 1 to k. More formally that's \begin{equation}\label{2.1} \begin{split} &{} n(n+1)/2 - k(k+1)/2\\ =& 1/2(n^2+n -k^2 -k)\\ =& 1/2(n^2-k^2 + n -k)\\ =& 1/2((n+k)(n-k) +(n -k))\\ =& 1/2((n+k + 1)(n-k))\\ \end{split} \end{equation}

So now if we want to get a specific number \(Y\) out of that then $$ Y = 1/2((n+k + 1)(n-k)) $$ so $$ 2Y = (n+k + 1)(n-k) $$

Notice that the factors on the right hand side differ by \(2k + 1\) this means that one is always even and one odd and the odd one must be a divisor of \(Y\) (any odd divisor of \(2Y\) must divide \(Y\) also). So every odd divisor, \(d\) of \(Y\) gives us 2 possibilities. Either $$ \begin{split} &n+k+1& = d\\ &n-k& = 2Y/d\\ \end{split} $$ or $$ \begin{split} &{}n+k+1& = 2Y/d\\ &{}n-k& = d\\ \end{split} $$

Solving these for n and k we get either $$ \begin{split} n &= (d-1)/2 + Y/d\\ k &= Y/d - (d + 1)/2\\ \end{split} $$ or $$ \begin{split} n &= (d-1)/2 + Y/d\\ k &= (d - 1)/2 - Y/d\\ \end{split} $$

In both cases \(n\) is the same. The \(k\)s are different and in fact they are almost negatives of each other (add them together and you get \(-1\)). This means that for any odd divisor \(d\) you get exactly two solutions but exactly one of them will have a positive \(k\). \(n\) is positive if and only f \(d\) is positive.

So in fact for any \(Y\) there are exactly as many solutions as there are positive odd divisors of \(Y\). In the case of 2016 we have factors \(2016 =\) $$ \begin{split} d=1 &: 2016 = 2016\\ d=3 &: 2016 = 671+672+673\\ d=7 &: 2016 = 285+286+...+291\\ d=9 &: 2016 = 220+221+...+228\\ d=21 &: 2016 = 86+87+...+106\\ d=63 &: 2016 = 1+2+3+...+63\\ \end{split} $$ and for \(2015 =5*13*31\) we have $$ \begin{split} d=1 &: 2015 = 2015\\ d=5 &: 2015 = 401 + 402 + ... + 405\\ d=13 &: 2015 = 149 + 150 + ... + 161\\ d=31 &: 2015 = 50 + 51 + 80\\ d=65 &: 2015 = 2 + 3 + ... + 63\\ d=155 &: 2015 = 65 + 66 + ... + 90\\ d=403 &: 2015 = 197 + 198 + ... + 206\\ d=2015 &: 2015 = 1007 + 1008\\ \end{split} $$

In the 2015 example, the divisors 65, 155, 403 and 2015 are big enough that k becomes negative and we need to switch to the second solution. This doesn't happen at all for 2016 - the odd divisors never get very big because a lot of 2016 is kind of locked up inside the 32.

When the sequence comes from the first solution, it has exactly \(d\) elements and is centered around \(Y/d\). This makes sense intuitively, you can sum the sequence by adding the first and last, the second and second last, etc each contributes \(2Y/d\) and finally the middle one gives you one more for a total of \(dY/ = Y\).

When the sequence comes from the second solution, that doesn't work but instead of switching to solution 2, we can stick with solution 1 and accept a negative \(k\). Then we get a \(d\)-element sequence that sums to \(Y\) $$ \begin{split} d=65 &: 2015 = -1 + 0 + 1 + 2 + 3 + ... + 63\\ d=155 &: 2015 = -64 + -63 + ... + 90\\ d=403 &: 2015 = -196 + -195 + ... + 206\\ d=2015 &: 2015 = -1006 + -1005 + ... + 1008\\ \end{split} $$ each of these is actually the same as the solutions above (just cancel all the negatives with their positive counterpart)

Puzzle: Writting year as sums of runs of numbers

This year is 2016 and \[ 2016 = 1 + 2 + 3 + ... + 63 \] I didn't notice this myself, I saw it written somewhere but then I wondered if there were any other ways of writing 2016 as a sum of consecutive numbers and it turn out there are.

So, the puzzle is, how many ways are there of writing 2016 as a sum of consecutive numbers and what's the answer for the general case where you're trying to write any number \(Y\) as such a sum. The answer is quite simple but non-obvious.

I'll post the solution shortly My solution is here, please leave comments or solutions below.

Monday, August 31, 2015

Anti-war protest in Tokyo

This lady was at the anti-war protest in Tokyo this weekend. Police estimates are 30k people, organizer estimate were 120k people. I've no idea what the truth is but the pavement everywhere was jammed full of people with signs, chanting anti-war and anti-Abe slogans.

The protest is because the prime minister (Abe Shinzo) is trying to get a few bills through parliament that would allow Japan to participate in collective self-defense. In particular, this would mean that they could have a pact with the US. The US is currently at war with several abstract concepts and some entities that are not nation states, so what does collective self-defense mean in this case? It also seems to be contrary to article 9 of the Japanese constitution. Even if the bills pass, they will be quickly challenged in the Supreme court, where hopefully they'll be trashed. Of course Abe's party has a proposal to fix the constitution too.

For extra fun, she was being interviewed by CCTV, the Chinese state TV station.

Thursday, February 26, 2015


For almost 2 years I've been having back pain. It took me about 9 months to figure out that it was from cycling (I blamed various things at first, like my mattress).

I went to a physio who gave me some exercises. I hated them because doing them correctly was really tricky. I noticed that one of them was like lying on the floor and pretending to swim, so I just started swimming for real instead. Swimming and better posture at work mostly fixed the upper back pain but I continued to have lower back pain. As a result, I could only cycle 2 or 3 days per week.

A few months ago, I went back to the physio to do a bike-fit, thinking that if I sat differently on the bike, it would fix my back. He said that the bike wasn't the problem. He told me that my lower back was lacking flexibility, that the vertebrae were locked and that I needed to stretch. He gave me different exercises for my lower back. These were not as hateful as the previous set but they were still annoying.

Then a friend told me about Sworkit, an app that makes it easy to do exercises casually, whenever you have some time. It comes with a bunch of set routines, with videos of how to do each exercise and you can do 5 minutes at a time, with 30 seconds for each exercise. Inside the Stretching secton I found "Back Stengthening". For the first while there was a bit of clicking and popping in the lower back as things freed up. After about a week of doing these 2-3 times per day, I was back cycling every day without pain!

That's a fantastic result for me. I had been worried that I would have to give up cycling. Taking the train is a little slower but reasonably comfortable but I fear what would happen to me with free lunches and no exercise. So splashed out the $1 for the Sworkit Pro to say thanks.

I learned two things from this.

  1. Stretching matters. I've cycled everywhere for almost 30 years but I'm still a fairly casual cyclist. I try not to break a sweat (not possible in Japanese summertime!) and I just go at a comfortable speed. I have never warmed up before getting on the bike and I still don't but at some point during the day it's good to stretch properly. Perhaps that's just getting old.
  2. The mediocre thing you do regularly is way more effective than the perfect thing you never do. The stretches in Sworkit were not tailored to my problem but the app makes it fun and easy enough that I do them 2-3 times per day. The tailored exercises were annoying enough that I was only doing them once every 2-3 days. I've had the same experience with Japanese. The manga that I enjoy reading is clearly inferior to the textbook that I never open from some abstract language learning point of view but in practice, you can only learn from the things you do, not the things you don't.

Wednesday, October 08, 2014

AsRock Q1900DC and laptop power adapters

I couldn't find any concrete recommendations but I now know that the DC in fits the adapter of a Toshiba Dynabook Satellite T210 but since that adapter is needed for the laptop, I had to buy another. In my local shop the Toshiba replacements were expensive so I bought an adapter for a Panasonic Toughbook (it came with 2 different size jacks). So they also work.

Sunday, July 06, 2014

Hotpot without the hot or the pot.

Back to the same place again (麻辣湯 - malatang). This time I remembered to take a photo before eating it all (harder than it looks).

It's awesome. It's basically Sichuan hotpot but without having to deal with a fire and a huge bowl of soup on your table. And ordering for one is easy. You choose your ingredients and 3 minutes later they bring you the cooked results. Unlimited rice included, all for 680JPY (4.90EUR)!

This time I went for 激辛, the highest on their spicy scale and it still wasn't that spicy. I might be getting the "foreigners don't like spicy" treatment, I don't know. I'll ask to go off the scale next time.

They also have awesome fried shaolongbao. These things are dangerous. They're dumplings, full of soup. Even when you think you've slurped it all out, there are still pockets remaining, hidden away, waiting to shoot a jet of boiling hot liquid up your nose when you bite down!

Sunday, February 16, 2014

Delivering mail from my home server to gmail

Mostly a note to myself

My ISP in Japan (OCN) blocks port 25 outgoing. It provides a relay host but it drops things if they have a dodgy looking from address. So I set up a postfix rewrite rule to make all of my mail from this server look like it was coming from my gmail account. This seems to make OCNs SMTP relay happy.

In /etc/postfix/, I added

smtp_generic_maps = regexp:/etc/postfix/generic
and that file looks like
/(.*?)@.*/ myaddress+antispamtoken+hostname-$

Gmail thinks this is spam, so I had to set up a filter to automatically catch emails from myaddress+antispamtoken and mark them as not spam.

There are probably several better ways of achieving the same goal but the less I know about SMTP and MTAs the happier I am.

Sunday, January 12, 2014

Fixing a Nexus 7

A couple of weeks ago, my Nexus 7 went into the sink while I was washing up. Despite my best efforts, I couldn't save it. About a year ago, a colleague dropped his Nexus 7 and smashed the screen. He was kind enough to give me that old tablet this week.

Thanks to, swapping the screens was fairly straightforward. I actually made it harder for myself because I read their full teardown and peeled back a lot of sticky copper shielding that I could have just left alone if I'd noticed they had an article for just replacing the display. Anyway, those guys are awesome. I was able to fix the speaker in another phone last year because of them, go buy some of their repair kits.

Here's what the insides of 2 Nexus 7s looks like :)

Also, well done Asus for making it fairly easy for me and my fat fingers to do this.

If anyone needs any help attempting the same thing, let me know.

Monday, December 23, 2013


As much as the EU has its problems, it was an agreement among nations for nations, with nations retaining sovereignty (the Lisbon treaty introduced majority voting, so that's less true now). It has respect for human rights built in and its courts are, well, actual courts. It has a pretty good track record on delivering on human rights within the EU. I dread to think where Ireland and some others would be socially without it.

The TPP and TAFTA on the other hand, are agreements by nations to hand over some of their sovereignty to corporations and opaque tribunals. The WTO is already this to some extent but is actually about international trade. TPP and TAFTA go much further and impact the laws that can be passed inside countries based on the impact they would have on international investors operating inside those countries.

There are already treaties like this and the results are not good. The Australian govt is attempting to regulate tobacco packaging and is being sued by Philip Morris because it will hit their profits ( Tobacco companies making less money is exactly the point of the law. If a government decides to hit tobacco companies and the existing courts are OK with that then that should be the end of it.

The idea that someone has a right to continue to profit from a harmful industry or to be compensated for their reduced profits is nuts. It warps the investment market. Right now, investments like this are risky and capital is allocated elsewhere as a result. These treaties remove these risks by forcing governments to compensate investors who have chosen to invest in industries that end up being regulated. This makes antisocial investments more attractive.

TPP and TAFTA are more of the same, just bigger and stronger. They are being negotiated in secret with only a corporate interests represented. Some parts have been leaked, they are not nice.

You can read an American perspective on TPP here:

This article is about opposition in Japan:

If either of these passes, the other will become much more likely ("we have to follow the international standards, don't you know").

They both need to be shot down.

Saturday, July 06, 2013

Looking for a backup system (maybe)

I have many static files that I care about, e.g. photos, downloaded media, media that I've ripped, archives of old email, important documents. They are mostly mirrored on 2 disks but it's a bit of a mess. For some I do extra backing up, e.g. photos are supposed to end up in the cloud too. A few other things are backed up to a virtual server but that's even less systematic. Some things are over-backed up, they exist in a RAID and also on my media player disk and on a physical DVD. That's a waste of space. I don't want to have to think about any of this (at least not more than once - at the time I decide I want to be careful about a file).

I'm not trying to solve the general backup problem, this is pretty much for files that do not change once written (although their metadata or importance may change).

I want to be able to do something like

absorb --tag personal-documents --delete-when-safe tax-return-2013.pdf
and in a few seconds, there's a RAIDed copy on my home server and a copy in Google drive, another in Dropbox and that working copy is gone, not cluttering up my laptop anymore.

Does this exist already? It's not just backup, it's part "document management" and there's lots of stuff I'd like to be able to do with tagging, managing, presenting, sharing but I'd settle for a partial solution that I don't have write myself for now. I can't find anything suitable but thought I'd ask before putting any effort in.

Tuesday, June 25, 2013

Getting notification when a long-running command finishes.

My job often involves running a command to do something, realising that it's going to take longer than I expected and going doing something else in the meantime. Conversely I'll run something that should take a long time, go do something else and find that it stopped with an error after a very short time and I didn't notice.

For a long time I've wanted to set things up so that any command that takes more than 20s to run would send me an IM when it was finished. What I didn't want was to have to prefix all my commands with some notification because that's annoying and I would probably forget in exactly the cases I need it.

It sounds pretty simple but getting it work turned out to be quite fiddly.

Delivering an instant message.

There's a program call sendxmpp that will deliver an instant message over XMPP. It's written in Perl and definitely works on Linux but probably also Mac and Windows if you really want. Every few months I would try to make it work and when I finally got it working, it would stop working again the next day.. Apparently Google's XMPP service is particularly finicky. I appear to have made it work for real now. I honestly don't know what I did differently, it may even be something that changed when Google deprecated XMPP in favour of Hangouts. As I write this, I see that it no longer works for one of my accounts but it's working fine with another! If there's a more reliable option, I'd love to know.

To make this work you'll need a second Google account because sending a message from yourself to yourself doesn't seem to work. So I created an account that I'm going to use only for sending IMs. After creating that account, I invited it to chat and made sure that the accounts can chat with each other through the normal Google chat interface.

Next I created a config file for it with the following commands (you'll need to replace some parts of the last one)

Now to send a notification to myself I just do

echo some message | sendxmpp -t -u -o

and the message shows up. I also get a bunch of Perl warnings but such is life.

Finally I wrapped that up as a command called notify and I can just pipe a message into that.

Spotting that a command is taking a long time to finish

I hate unix shells, they're a horrible mishmash of special cases, obscure features and crap that's only that way for backwards compatibility. What I've done is a hack in bash and it's not perfect (it seems like it would be slightly less hacky in zsh but that wouldn't fix the oddities described below). The core is this

What that does is run bash_pre_cmd every time you hit enter to kick off a command and bash_post_command every time it prints the command prompt (which indicates that the command has finished, although there are various way for this not to be true, hence the oddities but in normal usage it's true). So all that remains is sensible definitions for those two commands

You'll probably want to customize the callback for your own preferences.

Put all that into your .bashrc and cross your fingers


If you suspend a process, that will cause the prompt to be printed and may cause a notification. If you unsuspend that process with fg then when it finishes, this code will see fg as the command that just completed. Also, we cannot notify for 0 seconds otherwise just hitting enter would cause notification and the IM would refer to the last command in the history. Interactive commands obviously take a long time and so cause useless notifications. I'll probably whitelist a few common ones like man and less. There are probably some problems too but for normal type-run-complete shell use it works just fine.

Basically shells are shitty and not very flexible. Doing this correctly would involve being able to stash away some state along with each command that's started then getting access to that state when the command finishes, it would require changes to shell.

Friday, June 14, 2013

Julian Assange's "secret" meeting with Eric Schmidt has a transcript and a 3 hour recording of a meeting between Julian Assange and Eric Schmidt (and some others) as part of the research for Eric's recent book.

It's long but it's quite interesting (nothing terribly secret though).

I like Assange's goal of using content-based addressing for everything. It's not an original idea of his but I his point about detecting newspaper articles that silently disappear was good. If he can help to popularise it, that's great.

I completely agree with his call for "scientific journalism". That is journalism where all claims are backed up with references. He says that anything else should be dismissed as not journalism at all. There used to be the excuse that there wasn't enough room on paper but that gone now. George Monbiot ( has been including full references in the web versions of his articles for years now but I haven't see anyone else doing it.

I thought he didn't give a convincing response to the question of what happens if the governments and corporations floods Wikileaks with thousands of computer-generated fake but plausible leaked documents. That whole conversation got a bit messed up and he seemed to miss the core of the question.

Possibly the most shocking thing was that Eric Schmidt didn't know what simulated annealing was :)

Anyway, I've linked the MP3 to this post so if you want to listen to it conveniently through a podcast player, you can subscribe to my feed to get it (there are no other podcasts in my feed).

Saturday, May 04, 2013


Quick note on this in case anyone runs into the same thing.

I got a one of these

When it checked for firmware upgrades it found "" but that would not connect to OCN no matter what I did. In the end I noticed that "" was available, installed that and OCN worked immediately.

No special settings needed, just username and password.