Perfect square and perfect cube

前端 未结 8 2149
离开以前
离开以前 2020-12-09 12:27

Is there any predefined function in c++ to check whether the number is square of any number and same for the cube..

8条回答
  •  佛祖请我去吃肉
    2020-12-09 12:52

    No, there are no standard c or c++ functions to check whether an integer is a perfect square or a perfect cube.

    If you want it to be fast and avoid using the float/double routines mentioned in most of the answers, then code a binary search using only integers. If you can find an n with n^2 < m < (n+1)^2, then m is not a perfect square. If m is a perfect square, then you'll find an n with n^2=m. The problem is discussed here

提交回复
热议问题