proconlib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: $\left\lfloor \sqrt{x} \right\rfloor$ (tools/floor_sqrt.hpp)

template <typename T>
T floor_sqrt(T x);

It returns $\left\lfloor \sqrt{x} \right\rfloor$.

Constraints

Time Complexity

License

Author

Required by

Verified with

Code

#ifndef TOOLS_FLOOR_SQRT_HPP
#define TOOLS_FLOOR_SQRT_HPP

#include <cassert>

namespace tools {

  template <typename T>
  T floor_sqrt(const T n) {
    assert(n >= 0);

    T ok = 0;
    T ng;
    for (ng = 1; ng <= n / ng; ng *= 2);

    while (ng - ok > 1) {
      const T mid = ok + (ng - ok) / 2;
      if (mid <= n / mid) {
        ok = mid;
      } else {
        ng = mid;
      }
    }

    return ok;
  }
}

#endif
#line 1 "tools/floor_sqrt.hpp"



#include <cassert>

namespace tools {

  template <typename T>
  T floor_sqrt(const T n) {
    assert(n >= 0);

    T ok = 0;
    T ng;
    for (ng = 1; ng <= n / ng; ng *= 2);

    while (ng - ok > 1) {
      const T mid = ok + (ng - ok) / 2;
      if (mid <= n / mid) {
        ok = mid;
      } else {
        ng = mid;
      }
    }

    return ok;
  }
}


Back to top page