How to code the power digit sum algorithm