See http://ambur.net/counting.pdf