DEV Community

Durga Pokharel
Durga Pokharel

Posted on

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

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:
        cube_root_numbers.append(n)
        print(n)
Enter fullscreen mode Exit fullscreen mode

Day 5 of #100DaysOfCode and #Python
* More about file handling
* More about List and Tuples
* Python code to find the perfect cube number in given range. pic.twitter.com/fTNQm4r5n4

— Durga Pokharel (@mathdurga) December 28, 2020

Top comments (0)