w3resource

Python Exercises: Iterated Cube Root

Python Basic - 1: Exercise-150 with Solution

Write a Python program that takes a positive integer and calculates the cube root of the number until the number is less than three. Count the number of steps to complete the task.

Sample Data:

(3) -> 1
(39) -> 2
(10000) -> 2

Sample Solution-1:

Python Code:

# Function to count the number of times a positive integer can be cubed until it is less than 3.
def test(n):
    # Counter variable initialized to 0.
    ctr = 0
    
    # Loop continues until n is greater than or equal to 3.
    while n >= 3:
        # Cube root of n is calculated using the expression n ** (1./3.).
        n = n ** (1./3.)
        
        # Increment the counter by 1 for each iteration.
        ctr = ctr + 1
    
    # Return 'Not a positive number!' if the final value of n is negative, otherwise, return the counter value.
    return 'Not a positive number!' if n < 0 else ctr

# Take user input for a positive integer.
n = int(input("Input a positive integer:"))

# Call the test function with the user-input value of n and print the result.
print(test(n))

Sample Output:

Input a positive integer: 3
1
Input a positive integer: 39
2
Input a positive integer: 10000
2
Input a positive integer: -4
Not a positive number!

Explanation:

Here is a breakdown of the above Python code:

  • Function definition:
    • def test(n):: Defines a function named "test()" that takes a positive integer 'n' as input.
  • Counter Initialization:
    • ctr = 0: Initializes a counter variable to 0.
  • While Loop:
    • while n >= 3:: A while loop that continues as long as the value of n is greater than or equal to 3.
  • Cube root calculation:
    • n = n (1./3.): Calculates the cube root of n using the expression n (1./3.).
  • Counter Increment:
    • ctr = ctr + 1: Increments the counter by 1 for each iteration of the loop.
  • Return statement:
    • return 'Not a positive number!' if n < 0 else ctr: Returns 'Not a positive number!' if the final value of 'n' is negative; otherwise, returns the counter value.
  • User input:
    • n = int(input("Input a positive integer:")): Takes user input for a positive integer.
  • Function call and print:
    • print(test(n)): Calls the "test()" function with the user-input value of 'n' and prints the result.

Flowchart:

Flowchart: Python - Iterated Cube Root.

Sample Solution-2:

Python Code:

# Recursive function to count the number of times a positive integer can be cubed until it is less than 3.
def test(n):
    # Return "Not a positive number!" if n is negative.
    if n < 0:
        return "Not a positive number!"
    
    # Return 0 if n is less than 3.
    elif n < 3:
        return 0
    
    # Recursive call to test function with the cube root of n, and add 1 to the result.
    else:
        return test(int(n ** (1./3.))) + 1

# Take user input for a positive integer.
n = int(input("Input a positive integer:"))

# Call the test function with the user-input value of n and print the result.
print(test(n))

Sample Output:

Input a positive integer: 14
1
Input a positive integer: -5
Not a positive number!

Explanation:

Here is a breakdown of the above Python code:

  • Function definition:
    • def test(n):: Defines a recursive function named "test()" that takes a positive integer n as input.
  • Conditional statements:
    • if n < 0:: Checks if 'n' is negative and returns "Not a positive number!" in that case.
    • elif n < 3:: Checks if 'n' is less than 3 and returns 0 in that case.
  • Recursive call:
    • return test(int(n ** (1./3.))) + 1: Recursively calls the "test()" function with the cube root of n and adds 1 to the result.
  • User input:
    • n = int(input("Input a positive integer:")): Takes user input for a positive integer.
  • Function Call and Print:
    • print(test(n)): Calls the "test()" function with the user-input value of 'n' and prints the result.

Flowchart:

Flowchart: Python - Iterated Cube Root.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: N x N square consisting only of the integer N
Next: Python String Exercise Home.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.