Is an integer number of cubes?

It seems simple, but I cannot find a way to do this. I need to show whether the root of the cube of the whole is integer or not. I used the is_integer() float method in Python 3.4, but it failed. how

 x = (3**3)**(1/3.0) is_integer(x) True 

but

 x = (4**3)**(1/3.0) is_integer(x) False 

I tried x%1 == 0 , x == int(x) and isinstance(x,int) with no success.

I would appreciate any comments.

+10
python
source share
6 answers

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

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

This truncates the floating point cubing, then checks for the two closest integers.

For large numbers, one way to do this is to perform a binary search for the true root of the cube using integers only to maintain accuracy:

 def find_cube_root(n): lo = 0 hi = n while lo < hi: mid = (lo+hi)//2 if mid**3 < n: lo = mid+1 else: hi = mid return lo def is_perfect_cube(n): return find_cube_root(n)**3 == n 
+23
source share

SymPy also has an integer_nthroot function that quickly finds the integer n-th root of a number and tells you whether it was also that:

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

So your function could be

 def is_perfect_cube(x): return integer_nthroot(x, 3)[1] 

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

+3
source share

If your numbers are small, I would do:

 def is_perfect_cube(number): return number in [x**3 for x in range(15)] 

Of course, 15 can be replaced with something more suitable.

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

 from sympy import S, Rational def is_perfect_cube(number): # change the number into a sympy object num = S(number) return (num**Rational(1,3)).is_Integer 
+1
source share

I think you should use the round function to get the answer. If I had to write a function, it would look like this:

 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, due to some problems with calculating the value of the root of the cube, this is not exactly calculated, For example , int(41063625**(1.0/3.0)) will give you 344, but the value should be 345.

0
source share

To answer @nneonneo in more detail, you could write a more general kth-root function to use instead of cube_root,

 def kth_root(n,k): lb,ub = 0,n #lower bound, upper bound while lb < ub: guess = (lb+ub)//2 if pow(guess,k) < n: lb = guess+1 else: ub = guess return lb def is_perfect_cube(n): return kth_root(n,3) == n 
0
source share

This is another approach using a 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 a math module.
Line 2: enter the number from which you want to get the root.
Line 3: enter the nth root you are looking for.
Line 4: Use the power function.
Line 5: rounded to 10 significant digits 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 the decimal portion of the root value of the cube.
Line 9: return True if the root of the cube is an integer. Return False if the value of the root of the cube contains fractional numbers.

0
source share

All Articles