<commit_before>/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function(x) {
  var low = 0;
  var high = x;
  var sq;
  var pre;
  if (x === 0) return 0;

  while (low <= high) {
    var i = Math.floor((high + low)/2);
    sq = i * i;
    if (sq === x)  return i;

    if (sq > x) {
      high = i - 1;
    } else {
      pre = i;
      low = i + 1;
    }
  }

  return pre;
};


var eq = require('assert').equal;
eq(mySqrt(5), 2);
eq(mySqrt(4), 2);
eq(mySqrt(11), 3);<commit_msg>sqrt(x): refacotor potentially overflow mid * mid<commit_after>/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function(x) {
  var low = 0;
  var high = x;
  var pre;
  if (x === 0) return 0;

  while (low <= high) {
    var mid = low + ((high - low) >> 1);

    // in case of overflow, use below instead of mid * mid
    if (mid === x / mid)  return mid;

    if (mid > x / mid) {
      high = mid - 1;
    } else {
      pre = mid;
      low = mid + 1;
    }
  }

  return pre;
};


var eq = require('assert').equal;
eq(mySqrt(1), 1);
eq(mySqrt(5), 2);
eq(mySqrt(4), 2);
eq(mySqrt(11), 3);
{'fraction_non_alphanumeric': 0.19199272065514103, 'fraction_numerical': 0.025477707006369428, 'mean_word_length': 2.160919540229885, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '579524', 'n_tokens_mistral': 486, 'n_tokens_neox': 446, 'n_words': 139}