0

So I am implementing a custom hash function and I am not sure what kind of number and how big a number to choose. I have tried various prime numbers but I am not sure if % operator works faster for prime numbers. Actually, I am not sure if there are cases when modulo operator works faster or if there are any cases.

Maybe the answer lies within cython or some other low level implementation details?

eugen
  • 1,249
  • 9
  • 15
  • why do you think it would make a difference in case of a prime number? – pvpkiran Sep 30 '19 at 13:55
  • I am not sure but I thought if this it is optimized for a prime number? I am not sure though... – eugen Sep 30 '19 at 13:56
  • `%` is just the modulo operator, it doesnt know if a number is prime or not it just calculates the remainder of one number divided by another. – Chris Doyle Sep 30 '19 at 13:57
  • 3
    Previous Answer: [How does Python Implement the Modulo Operation](https://stackoverflow.com/questions/18198308/how-does-python-implement-the-modulo-operation) – DarrylG Sep 30 '19 at 14:00
  • Ok, I have edited the question, so maybe it is not that old question – eugen Sep 30 '19 at 14:11

1 Answers1

2

From the Python online documentation:

The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand 1.

6. Expressions - Binary arithmetic operations - Modulo operator

A prime number ads no necessary improvement of degradation, the operator behaviour prevails.

If your looking to get the division and remainder in one go, you're looking for the divmod() function.

Pedro Rodrigues
  • 2,520
  • 2
  • 27
  • 26