How do I get a whole number as a result for a cube root?

后端 未结 6 1866
梦谈多话
梦谈多话 2021-01-13 13:15

I am creating a problem which requires me to find the cube root of certain numbers, some of them have whole number roots, but a lot of them don\'t.

I have numbers li

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 13:54

    We first calculate a candidate integer of the cubit root by a very rough rounding (int(... + 0.1)) then verify if it is really the cubic root or not with exact integer arithmetic. (I assumed n is an int)

    cand = int(n ** (1.0/3.0) + 0.1)
    if cand**3 == n:
        print(cand, "is the cube root of ", n)
        processing = False
    else:
        n -= 1
    

提交回复
热议问题