2
from fractions import Fraction

counter = 0;
a = int(raw_input())
b = int(raw_input())

if 1 <= a <= 10 ** 8:
    if a <= b <= 10 ** 8:
        for i in range(a, b+1):
            if float(i**Fraction(1,3)).is_integer() == True:
                counter += 1
                print(i)

print(str(counter))
print (str(float(64**Fraction(1,3)).is_integer()))

This code returns false which results in the if statement not being run at all. Furthermore, the cube root of 64 is 4, therefore, the result should be an integer. However, in the range between 1 and 100 inclusive, 1, 8, and 27 return true for this case. Any help would be appreciated as to why the cube root of 64 is not returning true.

Willie3838
  • 31
  • 2
  • 3
    You're running into the issue that floating point numbers are not very accurate - see https://docs.python.org/2/tutorial/floatingpoint.html – Peter Gibson May 24 '18 at 01:55
  • 1
    Possible duplicate of [Is cube root integer?](https://stackoverflow.com/questions/23621833/is-cube-root-integer) – James May 24 '18 at 02:00
  • 1
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Barmar May 24 '18 at 02:04

1 Answers1

0

You have used float for the cube root value. The following code gives you the value of float(i**Fraction(1,3)) = 3.9999999

Try rounding the value first before converting it to float.

if float(round(i**Fraction(1,3),2)).is_integer() == True:

Naveen
  • 1,190
  • 7
  • 20