solved for 2 digits...no testing
solved for 3 digits...fixed faulty code. Still needs tests.
final solution