refactor the Miller-Rabin test code

also use pow(b, 2, n) rather than b**2 % n which is a bit more efficient

This is only a minor change that doesn't change any test. It arose out of dicussions at [this issue](https://code.google.com/p/sympy/issues/detail?id=3809)