improved code for special cases and limited testing to the square root of the lower number