I am trying to find the running time of an algorithm that includes a computation of a%p. How long should this step take if a and p are n bit numbers?
Modulo/remainder is a O(1) operation (it's essentially just a variation on division, which takes constant time on fixed-sized numbers). Therefore, the inside of the loop is an O(1) operation, which makes the total complexity O(√n) .
the modulo operation is on O(1) , if you use the hardware implementation.
You just multiply the two numbers and then calculate the standard name. For example, say the modulus is 7. Let's look at some mod 15 examples. One thing to notice is that in modular arithmetic you can multiply two numbers that are both nonzero, and the result can be zero.
the modulo operation is on O(1)
, if you use the hardware implementation. (the reason being, that every operation on a limited set of input values into the same set is in O(1)
)
Otherwise, any software implementation for arbitrary length integers should have a similar complexity as the division of the same input numbers. I'm not sure what exactly that is.
EDIT: I've got a hunch, it's O(n²)
but I don't have a proof ready.
EDIT2: It's really hard to provide a complete and correct answer to this, as the complexity does not depend on the problem but on the implementation and thus, different implementations have different complexities, and the problem can only give a lower bound for the achievable performance of a solving algorithm.
Hoever, there is no such thing as a lower bound for the complexity of multiplication yet. and as the lower bound for modulo actually depends on the lower bound for multiplication (see the link posted in the comments. I've read it, it's good stuff!), there is also no lower bound for the complexity of modulo implementations, and so we can't give an exact estimate of the achievable performance of modulo.
But since we're talking about Big-O notation, I can safely say that any decent modulo implementation in in O(n²)
, and most are in smaller subsets of O(n²)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With