Iterative python version (easier to convert in to verilog)