DEV Community


Day 5 of 100DaysOfCode: Python Code to Find Perfect Cube in Range

Durga Pokharel
A mathematics student learning to code.
・1 min read

This is the 5th day of 100DaysOfCode and I am learning more from Coursera's Python Data Structure Course and did some assignments also.
But for my own exercise, I tried to write a code to find perfect cube numbers in a given range.
My solution is like below:

  • Create a list of numbers in a given range.
  • Loop through each number and find its cube root value.
  • Round the number came after making cube root of a number and find its cube.
  • If current number is equal to the number came after cubing the root value then the current number is perfect cube number and append it to our list.
numbers = list(range(1,202))
cube_root_numbers = []
for n in numbers:
    cube_root = (n)**(1/3)
    int_cube_root = round(cube_root)
    if int_cube_root**3 == n:
Enter fullscreen mode Exit fullscreen mode

Discussion (0)