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. Show # Use Python code to see if numbers are perfect cubesIn 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:
This custom Inside the function we first take the absolute value of the
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 We then round that value to an integer with Python’s # Example: see if a single value is a perfect cubeLet’s see how we use the above function. The mini-program below has The script’s code is:
Here we
first copy/paste the Then we make four variables with numerical values. We name them For that we call the The last bit of code has the
# Example: find perfect cubes in a list of valuesOf 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 Here’s an example:
This mini-program start with the code of the custom Then we make a list named For that we make a Inside the loop we first call the Then an if statement checks whether Here’s how that output looks when the loop is done:
ReferencesKelley, 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.
|