Created
August 10, 2015 06:48
-
-
Save viveksyngh/d5770546b6dbcbc866ee to your computer and use it in GitHub Desktop.
Pow(m, n, d)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| __author__ = 'Vivek' | |
| #Implementation of (x^n)%d | |
| def pow(self, x, n, d): | |
| """ | |
| :param x: integer | |
| :param n: integer | |
| :param d: integer | |
| :return: (x^n)%d | |
| """ | |
| res = 1 | |
| sq = x | |
| if n == 0 and x == 0: | |
| return 0 | |
| if n == 1 : | |
| return x%d | |
| while n != 0 : | |
| if n%2 == 1 : | |
| res = res * sq | |
| sq = (sq * sq)%d | |
| n = n/2 | |
| if res > d : | |
| res = res%d | |
| return res | |
| print(pow(71045970, 41535484, 64735492)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment