Is cube root integer?

前端 未结 6 692
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 21:12

This seems to be simple but I cannot find a way to do it. I need to show whether the cube root of an integer is integer or not. I used is_integer() float method

6条回答
  •  旧巷少年郎
    2020-12-01 22:13

    This is another approach using the math module.

    import math
    num = int(input('Enter a number: '))
    root = int(input('Enter a root: '))
    nth_root = math.pow(num, (1/root))
    nth_root = round(nth_root, 10)
    print('\nThe {} root of {} is {}.'.format(root, num, nth_root))
    decimal, whole = math.modf(nth_root)
    print('The decimal portion of this cube root is {}.'.format(decimal))
    decimal == 0
    

    Line 1: Import math module.
    Line 2: Enter the number you would like to get the root of.
    Line 3: Enter the nth root you are looking for.
    Line 4: Use the power function.
    Line 5: Rounded to 10 significant figures to account for floating point approximations.
    Line 6: Print a preview of the nth root of the selected number.
    Line 7: Use the modf function to get the fractional and integer parts.
    Line 8: Print a preview of decimal part of the cube root value.
    Line 9: Return True if the cube root is an integer. Return False if the cube root value contains fractional numbers.

提交回复
热议问题