Algorithm was modified and division was changed to bit operations.