0
import math

n, k = map(int, input().split())

print('NO' if (math.floor(n/k) % 2 == 0) else 'YES')

The above code takes 2 integers n and k as input and tries to tell if floor of n/k is odd or even.

Now the test cases :

  1. Input: n = 10 and k = 3 Output: YES Verdict: Correct.

  2. Input: n = 1000 and k = 3 Output: YES Verdict: Correct.

  3. Input: n = 10000000000000000 and k = 3 Output: YES Verdict: Correct.

  4. Input: n = 100000000000000000 and k = 3 Output: NO Verdict: Wrong.

  5. Input: n = 1000000000000000000 and k = 3 Output: NO Verdict: Wrong.

Now, what I found is that for n < 10e16 it works fine but for n = 10e17, 10e18, etc. it gives the wrong answer. I am not able to find an explanation for this behavior.

Thanks in advance.

Happy coding

Arpit-Gole
  • 365
  • 2
  • 6
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) (The linked answer explains things in terms of very small numbers, but the same problem applies to very large numbers.) – 0x5453 Aug 11 '20 at 04:06
  • Integers in Python have unlimited precision, but the division produces a `float` which does not. – Mark Ransom Aug 11 '20 at 04:10
  • Did you try to print `math.floor(n/k)`? That should answer your question immediately. – Mark Ransom Aug 11 '20 at 04:12
  • @0x5453 Now I know how floating points are implemented, but still doesn't solve my problem. Still, how do I obtain the correct output? – Arpit-Gole Aug 11 '20 at 04:20
  • @MarkRansom It doesn't make any difference. – Arpit-Gole Aug 11 '20 at 04:21
  • 1
    Use integer division `//` so you don't encounter floating point precision limits. I.e. `n//k` instead of `math.floor(n/k)`. – Mark Ransom Aug 11 '20 at 04:26

0 Answers0