Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate mod using pow function python

Tags:

python

pow

So, If i would like to calculate the value of 6^8 mod 5 using the pow function, what should I put in a line??

In the assumption that You don't need to import it first

I know that pow is used like pow (x, y) = pow (6, 8) = 6^8 and

My guess is

mod.pow(6,8)

Thank you!

like image 480
mark T Avatar asked Sep 23 '15 11:09

mark T


People also ask

Can we use math POW in Python?

The math. pow() method returns the value of x raised to power y. If x is negative and y is not an integer, it returns a ValueError. This method converts both arguments into a float.

Can pow () take 3 arguments?

The pow() method takes three parameters: number - the base value that is raised to a certain power. power - the exponent value that raises number. modulus - (optional) divides the result of number paused to a power and finds the remainder: number^power % modulus.


1 Answers

It's simple: pow takes an optional 3rd argument for the modulus.

From the docs:

pow(x, y[, z])

Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z). The two-argument form pow(x, y) is equivalent to using the power operator: x**y.

So you want:

pow(6, 8, 5)

Not only is pow(x, y, z) faster & more efficient than (x ** y) % z it can easily handle large values of y without using arbitrary precision arithmetic, assuming z is a simple machine integer.

like image 181
PM 2Ring Avatar answered Sep 23 '22 11:09

PM 2Ring