Power – Mod Power in Python | HackerRank Solution

Hello coders, today we are going to solve Power – Mod Power HackerRank Solution in Python.

Power - Mod Power in Python

Objective

So far, we have only heard of Python’s powers. Now, we will witness them!

Powers or exponents in Python can be calculated using the built-in power function. Call the power function ab as shown below:

>>> pow(a,b) 

or

>>> a**b

It’s also possible to calculate ab mod m.

>>> pow(a,b,m)  

This is very helpful in computations where you have to print the resultant % mod.

Note: Here, a and b can be floats or negatives, but, if a third argument is present, b cannot be negative.

Note: Python has a math module that has its own pow(). It takes two arguments and returns a float. It is uncommon to use math.pow().

Task

You are given three integers: ab, and m. Print two lines.
On the first line, print the result of pow(a,b). On the second line, print the result of pow(a,b,m).

Input Format

The first line contains a, the second line contains b, and the third line contains m.

Constraints

  • 1 <= a <= 10
  • 1 <= b <= 10
  • 2 <= m <= 1000

Sample Input

3
4
5

Sample output

81
1

Solution – Power – Mod Power in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT
import math

a = int(input())
b = int(input())
m = int(input())

c = math.pow(a, b)
d = c%m

print(int(c))
print(int(d))

Disclaimer: The above Problem (Power – Mod Power) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *