Perfect square and perfect cube

前端 未结 8 2123
离开以前
离开以前 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:57

    No, but it's easy to write one:

    bool is_perfect_square(int n) {
        if (n < 0)
            return false;
        int root(round(sqrt(n)));
        return n == root * root;
    }
    
    bool is_perfect_cube(int n) {
        int root(round(cbrt(n)));
        return n == root * root * root;
    }
    

提交回复
热议问题