modified sqrt end condition