Python Program to find out the price of a product after a number of days

PythonServer Side ProgrammingProgramming

Suppose, a person wants to buy a product of price x. But each passing day, the price of the product increases x times the price of the previous day. We have to find out the price of the product after y days since the person has made up his mind to purchase the product. If the price of the product is too much, then the answer is given as price modulo 10^9 + 7. The input is given in a list of pairs; the first value of the pair is the initial price x and the second value is y, the count of the days that have passed.

So, if the input is like nums = [(5, 2), (6, 8), (2, 12), (2722764242812953792238894584, 3486705296791319646759756475), (1505449742164712795427942455727527, 61649494321438487460747056421546274264)], then the output will be 25, 1679616, 4096, 754504594, 32955023

Here the output is 5^2 = 25, 6^8 = 1679616, 2^12 =4096, 2722764242812953792238894584^3486705296791319646759756475 = 754504594 (The value is given as value modulo 10^9 + 7), and so on.

To solve this, we will follow these steps −

  • for i in range 0 to size of nums, do
    • x,y := nums[i, 0], nums[i, 1]
    • return the value x to the power y modulo 10^9 + 7

Example

Let us see the following implementation to get better understanding −

 Live Demo

def solve(nums):
for i in range(len(nums)) :
   x,y = nums[i][0], nums[i][1]
   print(pow(x,y,1000000007))
solve([(5, 2),(6, 8),(2, 12)
,(2722764242812953792238894584, 3486705296791319646759756475)
,(1505449742164712795427942455727527, 61649494321438487460747056421546274264)])

Input

[(5, 2),(6, 8),(2, 12)
,(2722764242812953792238894584, 3486705296791319646759756475)
,(1505449742164712795427942455727527, 61649494321438487460747056421546274264)]

Output

25
1679616
4096
754504594
32955023
raja
Published on 18-May-2021 05:52:50
Advertisements