r/PythonLearning • • 14d ago

Help Request Python Quiz of the day.

What will be the output of the following Python Code?

print(pow(2, 5, 7))

0 Upvotes

9 comments sorted by

2

u/RNG_HatesMe 14d ago edited 14d ago

4

2**5 mod 7 = 32 mod 7 = 32-28 = 4

Interesting, I didn't know the pow() function had a parameter for mod? Apparently that's helpful in cryptography?

1

u/TheKnottyOne 14d ago

In my cryptography (entry level) class we used pow() for RSA encryption. While my skill and understanding is strictly from an educational standpoint it’s the only place I’ve seen pow() use the mod parameter heavily 😅

Do I remember the intricacies of its utilization? Nope 😂

1

u/RNG_HatesMe 14d ago

Apparently it's much faster with very large numbers because it distributes the mod operation and can keep the magnitudes lower, and therefore uses less memory and processing time.

1

u/TheKnottyOne 14d ago

Yeah that’s what I learned as well. I think there’s a caveat to timing though - the time it takes to complete seems to depend on the value of the exponent

1

u/TheKnottyOne 14d ago edited 14d ago

4 - pow() takes the parameters of: base, power, mod. So it’ll do 2 to the power of 5 then % 7

2

u/Buttleston 14d ago

Been programming in python for like 15 years, and I had no idea

1

u/atreidesardaukar 14d ago

I didn't do the math, but here's the documentation for the built-in function: 

https://docs.python.org/3/builtins/functions.html#pow

1

u/Fluffy_white_fox 8d ago

That's 2**5 then mod 7 So 32%7 that's 4