--- from: http://rosettacode.org/wiki/Damm_algorithm