Find multiple of x closest to or a ^ b (a raised to power b)
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
Examples :
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.
Observations :
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.
C++
// C++ Program to find closest // multiple of x to a^b #include <bits/stdc++.h> using namespace std; // function to find closest multiple // of x to a^b void multiple(int a, int b, int x) { if (b < 0) { if (a == 1 && x == 1) cout << "1"; else cout << "0"; } // calculate a ^ b / x int mul = pow(a, b); int ans = mul / x; // Answer is either (ans * x) or // (ans + 1) * x int ans1 = x * ans; int ans2 = x * (ans + 1); // Printing nearest answer cout << (((mul - ans1) <= (ans2 - mul)) ? ans1 : ans2); } // Driver Program int main() { int a = 349, b = 1, x = 4; multiple(a, b, x); return 0; } |
chevron_right
filter_none
Java
// java Program to find closest // multiple of x to a^b import java.io.*; public class GFG { // function to find closest // multiple of x to a^b static void multiple(int a, int b, int x) { if (b < 0) { if (a == 1 && x == 1) System.out.println("1"); else System.out.println("0"); } // calculate a ^ b / x int mul = (int)Math.pow(a, b); int ans = mul / x; // Answer is either (ans * x) or // (ans + 1) * x int ans1 = x * ans; int ans2 = x * (ans + 1); // Printing nearest answer System.out.println(((mul - ans1) <= (ans2 - mul)) ? ans1 : ans2); } // Driver Program static public void main (String[] args) { int a = 349, b = 1, x = 4; multiple(a, b, x); } } // This code is contributed by vt_m. |
chevron_right
filter_none
C#
// C# Program to find closest // multiple of x to a^b using System; public class GFG { // function to find closest multiple // of x to a^b static void multiple(int a, int b, int x) { if (b < 0) { if (a == 1 && x == 1) Console.WriteLine("1"); else Console.WriteLine("0"); } // calculate a ^ b / x int mul = (int)Math.Pow(a, b); int ans = mul / x; // Answer is either (ans * x) or // (ans + 1) * x int ans1 = x * ans; int ans2 = x * (ans + 1); // Printing nearest answer Console.WriteLine(((mul - ans1) <= (ans2 - mul)) ? ans1 : ans2); } // Driver Program static public void Main () { int a = 349, b = 1, x = 4; multiple(a, b, x); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP Program to find closest // multiple of x to a^b // function to find // closest multiple // of x to a^b function multiple($a, $b, $x) { if ($b < 0) { if ($a == 1 && $x == 1) echo "1"; else echo "0"; } // calculate a ^ b / x $mul = pow($a, $b); $ans = $mul / $x; // Answer is either // (ans * x) or // (ans + 1) * x $ans1 = $x * $ans; $ans2 = $x * ($ans + 1); $k = ((($mul - $ans1) <= ($ans2 - $mul)) ? $ans1 : $ans2); echo($k); } // Driver Code $a = 348; $b = 1; $x = 4; multiple($a, $b, $x); // This code is contributed by ajit ?> |
chevron_right
filter_none
Python3
# Python3 Program to # find closest multiple # of x to a^b import math # function to find closest # multiple of x to a^b def multiple(a, b, x): if (b < 0): if (a == 1 and x == 1): print("1"); else: print("0"); # calculate a ^ b / x mul = int(pow(a, b)); ans = int(mul / x); # Answer is either (ans * x) # or (ans + 1) * x ans1 = x * ans; ans2 = x * (ans + 1); # Printing nearest answer if ((mul - ans1) <= (ans2 - mul)): print(ans1); else: print(ans2); # Driver Code a = 349; b = 1; x = 4; multiple(a, b, x); # This code is contributed # by mits |
chevron_right
filter_none
Output:
348
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 contribute@geeksforgeeks.org. 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.
Recommended Posts:
- 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



