Is there any cube function in python?

A perfect cube is the result of multiplying an integer with itself twice. While it’s easy in Python to raise a value to the power of 3, evaluating if a number is a perfect cube requires a bit more code. Let’s take a look.

# Use Python code to see if numbers are perfect cubes

In algebra, a cube of a number is its third power: the result we get by multiplying that number with itself twice (Wikipedia, 2019). A so-called perfect cube is the result of multiplying a positive integer with itself two times (Kelley, 2007; Wikipedia, 2019).

For example, 8 is a perfect cube because 2 x 2 x 2 = 8. Other perfect cube values are 125 (the result of 53), 343 (73), and 512 (83). Values that aren’t a perfect cube include 25 (2.92403 ≈ 25) and 100 (4.64163 ≈ 100).

There are several ways to see if a number is a perfect cube. One approach is the following. First take the cube root from a value. Then round that outcome to get its integer value. Next raise that rounded value to the third power. When that outcome matches the original number, that number is a perfect cube.

Here’s how we can program that process in Python:

def is_perfect_cube(number) -> bool:
    """
    Indicates (with True/False) if the provided number is a perfect cube.
    """
    number = abs(number)  # Prevents errors due to negative numbers
    return round(number ** (1 / 3)) ** 3 == number

This custom is_perfect_cube() function has one parameter. It returns a binary true/false value. That tells if the parameter is a perfect cube (True) or not (False).

Inside the function we first take the absolute value of the number parameter with Python’s abs() function. Else our other function code creates a complex number by raising the negative parameter value to a fractional power (1 / 3). We don’t want that since it triggers a Python error with the other code.

The second line of code checks if the function’s parameter is a perfect cube. Several things happen here. Let’s start with the innermost parentheses. First we divide 1 by 3 to get one third. Then we raise the number parameter to that third. That gives us the cube root.

We then round that value to an integer with Python’s round() function. Next we raise that integer to the power of 3. Then we look if that outcome matches (==) the original parameter value. When it does, the function’s parameter is a perfect cube. In that case the function returns True. Should that comparison test False, they are unequal and the function’s parameter is not a perfect cube.

# Example: see if a single value is a perfect cube

Let’s see how we use the above function. The mini-program below has is_perfect_cube() check if certain individual values are perfect cubes.

The script’s code is:

def is_perfect_cube(number) -> bool:
    """
    Indicates (with True/False) if the provided number is a perfect cube.
    """
    number = abs(number)  # Prevents errors due to negative numbers
    return round(number ** (1 / 3)) ** 3 == number


# Some values to check
value_a = 0.25
value_b = 8
value_c = 343
value_d = 117649

# See if the numbers are perfect cubes
pcube_a = is_perfect_cube(value_a)
pcube_b = is_perfect_cube(value_b)
pcube_c = is_perfect_cube(value_c)
pcube_d = is_perfect_cube(value_d)

# Output the results
print("Is", value_a, "a perfect cube?", pcube_a)
print("Is", value_b, "a perfect cube?", pcube_b)
print("Is", value_c, "a perfect cube?", pcube_c)
print("Is", value_d, "a perfect cube?", pcube_d)

Here we first copy/paste the is_perfect_cube() function.

Then we make four variables with numerical values. We name them value_a to value_d. Next we look if they are perfect cubes or not.

For that we call the is_perfect_cube() function on each variable. We store the True or False outcome from that function in new variables, which we name pcube_a through pcube_d.

The last bit of code has the print() function display the original value and the is_perfect_cube() outcome. Here’s what that outputs:

Is 0.25 a perfect cube? False
Is 8 a perfect cube? True
Is 343 a perfect cube? True
Is 117649 a perfect cube? True

# Example: find perfect cubes in a list of values

Of course we don’t always have individual values. Sometimes we have a list (or array) and want to find perfect cubes there. Luckily, the process for that is much the same: go through the values, and call is_perfect_cube() to find perfect cubes.

Here’s an example:

def is_perfect_cube(number) -> bool:
    """
    Indicates (with True/False) if the provided number is a perfect cube.
    """
    number = abs(number)  # Prevents errors due to negative numbers
    return round(number ** (1 / 3)) ** 3 == number


# Some random integer values
values = [
    3, 27, 81, 100, 1728,
    4539, 59319, 216100,
]

# See if each number is a perfect cube
for value in values:
    is_cube = is_perfect_cube(value)

    if is_cube:
        print(value, "is a perfect cube!")

This mini-program start with the code of the custom is_perfect_cube() function that we discussed above.

Then we make a list named values. Its contents are 8 different integer numbers. Now we want to see which of those are perfect cubes.

For that we make a for loop. This loop goes through the entire values list. During each loop cycle, we get the current list element with the value variable.

Inside the loop we first call the is_perfect_cube() function on that variable. That gives us a True or False outcome, which we put in another variable (is_cube).

Then an if statement checks whether is_cube is True. When it is, the print() function says that the current list value is a perfect cube.

Here’s how that output looks when the loop is done:

27 is a perfect cube!
1728 is a perfect cube!
59319 is a perfect cube!

References

Kelley, M.W. (2007). The Complete Idiot’s Guide to Algebra (2nd Edition). Alpha Books, NY: New York.

Wikipedia (2019, November 30). Cube (algebra). Retrieved on December 13, 2019, from https://en.wikipedia.org/wiki/Cube_(algebra)

Published December 20, 2019.

« All Python math articles

How do you write a cube function in Python?

Python Program to Find Cube of a Number.
def cube(x):.
return x * x * x..
n = int(input(" Enter the number : ")).
cube1 = cube(n).
print("The Cube of {0} = {1}". format(n, cube1)).

How do you square or cube in Python?

Python Write functions to find the square and cube of a given....
Example: Input: Enter an integer number: 6 Output: Square of 6 is 36 Cube of 6 is 216..
Function to get square: def square (num): return (num*num).
Function to get cube: def cube (num): return (num*num*num).

What is the function for cube root in Python?

cbrt() in Python. This mathematical function helps user to calculate cube root of x for all x being the array elements.