We are given 3 numbers a, b and x. We need to output the multiple of x which is closest to a^b.
Note : b can be a negative number
Input : x = 2, a = 4, b = -2 Output : 0 Explanation : a^b = 1/16. Closest multiple of 2 to 1/16 is 0. Input : x = 4, a = 349, b = 1 Output : 348 Explanation :a^b = 349 The closest multiple of 4 to 349 is 348.
1. When b is negative and a is 1, then a ^ b turns out to be 1 and hence, closest multiple of x becomes either x * 0 or x * 1. 2. When b is negative and a is more than 1, then a ^ b turns out to be less than 1 and hence closest multiple of x becomes 0. 3. When b is positive, calculate a ^ b, then let mul = Integer (a^b / x), then closest multiple of x is mul * x or (mul + 1) * x.
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Find value of y mod (2 raised to power x)
- Find unit digit of x raised to power y
- Find last five digits of a given five digit number raised to power five
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the multiple of x which is closest to a^b
- K-th digit in 'a' raised to power 'b'
- Number of digits in 2 raised to power n
- Print last k digits of a^b (a raised to power b)
- Check if a number can be expressed as x^y (x raised to power y)
- Minimum removals in a number to be divisible by 10 power raised to K
- Multiple of x closest to n
- GCD of a number raised to some power and another number
- Find power of power under mod of a prime
- Find the number closest to n and divisible by m
- Find K Closest Points to the Origin