-3

I am trying to calculate value of this expression:

int((1770246808796936040 * 1103515245 + 12345 / 65536) % 32768)

And it gives 0. Without int cast, it is 0 too.

Does anyone know why it so? Are these numbers too big for Python? Is any way to handle the issue? Appreciate any help.

Caconde
  • 4,177
  • 7
  • 35
  • 32
Bakerelly
  • 5
  • 1
  • I guess the issue is resolved, mark any of the answers as accepted to close it. – Jarvis Dec 26 '20 at 14:28
  • 2
    What do you think the answer should be, and why? It looks like one step of an [LCG](https://en.wikipedia.org/wiki/Linear_congruential_generator), in particular see the 4th line of [this table](https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use), but you have misunderstood the order of operations and incorrectly parenthesized it. In python a correct expression would be `((1770246808796936040 * 1103515245 + 12345) // 65536) % 32768` – President James K. Polk Dec 26 '20 at 15:56

3 Answers3

2

The number calculated from the expression

1770246808796936040 * 1103515245 + 12345 / 65536

is divisible by 32768, that's why you get 0.

To give you an idea of Python's capacity to handle huge numbers, you can see this:

>>> 10**1000

10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Jarvis
  • 8,494
  • 3
  • 27
  • 58
2

The % operator gives the remainder of a division. Since

(1770246808796936040 * 1103515245 + 12345 / 65536)

is divisible by 32768, the result is 0. To obtain the quotient, you should use the operator / instead of %.

aargun
  • 126
  • 4
1

Because the numbers' combination is such that the result is 0. Increase or decrease any number by even 1 and you will see that it is not 0. For example see below. I changed 32768 to 32769.

print(int((1770246808796936040 * 1103515245 + 12345 / 65536) % 32769))

It gives.

21725
Amit
  • 2,018
  • 1
  • 8
  • 12