notes blog about

Implementation of guess and check algorithm with approximation:

# Find cube root of a given number.

cube = 27

step = 0.01 # decreasing step -> slower program
diff = 0.1  # increasing diff -> less accurate answer

count = 0
guess = 0

# Look for close enough answer and make sure you don't
# accindentally skip the close enough boundary.
while abs(guess**3-cube) >= diff and guess <= cube:
    guess += step
    count += 1

print("numbers of guesses: {}".format(count))

if abs(guess**3-cube) < diff:
    print("{} is close to the cube root of {}".format(guess, cube))
else:
    print("failed to find cube root of {}".format(cube))

More