Skip to content Skip to sidebar Skip to footer

Is Cube Root Integer?

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 in Python 3.4 but t

Solution 1:

For small numbers (<~10 or so), you can use the following approach:

defis_perfect_cube(n):
    c = int(n**(1/3.))
    return (c**3 == n) or ((c+1)**3 == n)

This truncates the floating-point cuberoot, then tests the two nearest integers.

For larger numbers, one way to do it is to do a binary search for the true cube root using integers only to preserve precision:

def find_cube_root(n):
    lo =0
    hi = 1 << ((n.bit_length() + 2) // 3)while lo < hi:
        mid = (lo+hi)//2if mid**3 < n:
            lo = mid+1else:
            hi = mid
    return lo

def is_perfect_cube(n):
    returnfind_cube_root(n)**3 == n

Solution 2:

In SymPy there is also the integer_nthroot function which will quickly find the integer nth root of a number and tell you whether it was exact, too:

>>> integer_nthroot(primorial(12)+1,3)
(19505, False)

So your function could be

defis_perfect_cube(x): return integer_nthroot(x, 3)[1]

(And because SymPy is open source, you can look at the routine to see how integer_nthroot works.)

Solution 3:

If your numbers aren't big, I would do:

defis_perfect_cube(number):
    return number in [x**3for x inrange(15)]

Of course, 15 could be replaced with something more appropriate.

If you do need to deal with big numbers, I would use the sympy library to get more accurate results.

from sympy import S, Rational

defis_perfect_cube(number):
    # change the number into a sympy object
    num = S(number)
    return (num**Rational(1,3)).is_Integer

Solution 4:

To elaborate on the answer by @nneonneo, one could write a more general kth-root function to use instead of cube_root,

defkth_root(n,k):
    lb,ub = 0,n #lower bound, upper boundwhile lb < ub:
        guess = (lb+ub)//2ifpow(guess,k) < n: lb = guess+1else: ub = guess
    return lb

defis_perfect_cube(n):
    return kth_root(n,3) == n

Solution 5:

This is another approach using the math module.

import math
num = int(input('Enter a number: '))
root = int(input('Enter a root: '))
nth_root = math.pow(num, (1/root))
nth_root = round(nth_root, 10)
print('\nThe {} root of {} is {}.'.format(root, num, nth_root))
decimal, whole = math.modf(nth_root)
print('The decimal portion of this cube root is {}.'.format(decimal))
decimal == 0

Line 1: Import math module. Line 2: Enter the number you would like to get the root of. Line 3: Enter the nth root you are looking for. Line 4: Use the power function. Line 5: Rounded to 10 significant figures to account for floating point approximations. Line 6: Print a preview of the nth root of the selected number. Line 7: Use the modf function to get the fractional and integer parts. Line 8: Print a preview of decimal part of the cube root value. Line 9: Return True if the cube root is an integer. Return False if the cube root value contains fractional numbers.

Post a Comment for "Is Cube Root Integer?"