Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the big O running time of a mod p, given a and p are n bit numbers?

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?

like image 291
Plastech Avatar asked Feb 01 '13 19:02

Plastech


People also ask

What is the time complexity of mod?

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) .

Is mod in O 1?

the modulo operation is on O(1) , if you use the hardware implementation.

How do you multiply with mods?

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.


1 Answers

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²).

like image 148
Andreas Grapentin Avatar answered Sep 19 '22 00:09

Andreas Grapentin