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

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay