DEV Community

Durga Pokharel
Durga Pokharel

Posted on

1

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

Image of Stellar post

🚀 Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Sentry image

Make it make sense

Only the context you need to fix your broken code with Sentry.

Start debugging →

Retry later