1. 2

Reference implementation in Python: https://github.com/google/hpolyc

  1.