speeding up implementation by searching sqrt of num