Updated algorithm for divisor function