Is cube root integer?

前端 未结 6 708
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 21:12

This seems to be simple but I cannot find a way to do it. I need to show whether the cube root of an integer is integer or not. I used is_integer() float method

6条回答
  •  余生分开走
    2020-12-01 22:16

    I think you should use the round function to get the answer. If I had to write a function then it will be as follows:

    def cube_integer(n):
        if round(n**(1.0/3.0))**3 == n:
            return True
        return False
    

    You can use something similar to int(n**(1.0/3.0)) == n**(1.0/3.0), but in python because of some issues with the computation of the value of cube root, it is not exactly computed. For example int(41063625**(1.0/3.0)) will give you 344, but the value should be 345.

提交回复
热议问题