Power and Mod Power in Python HackerRank Solution

Hello Programmers, In this post, you will know how to solve the Power and Mod Power in Python HackerRank Solution. This problem is a part of the HackerRank Python Programming Series.

Power and Mod Power in Python HackerRank Solution
Power and Mod Power in Python HackerRank Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Power and Mod Power in Python HackerRank Solution

problem

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 as a^b shown below:

>>> pow(a,b)

or

>>> a**b

It’s also possible to calculate a^b 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. Frankly speaking, we will never use math.pow().

Task :

You are given three integers: a, b, and m, respectively. Print two lines.
The first line should print the result of pow(a,b). The second line should 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

Power and Mod Power in Python HackerRank Solutions

# Enter your code here. Read input from STDIN. Print output to STDOUT
a, b, m = [int(input()) for _ in range(3)]
print(pow(a, b), pow(a, b, m), sep='\n')

Disclaimer: The above Problem (Power and Mod Power in Python) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Integers Come In All Sizes in Python HackerRank Solution

Leave a Reply

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