Given two positive integers **N** and **K**, the task is to find the minimum number to be added to N to make it a power of K.

**Examples:**

Input:N = 9, K = 10

Output:1

Explanation:

9 + 1 = 10 = 10^{1}

Input:N = 20, K = 5

Output:5

Explanation:

20 + 5 = 25 = 5^{2}

**Approach:** The idea to solve this problem is to observe that the minimum power of K which can be formed from N is the next greater power of K. So, the idea is to find the next greater power of K and find the difference between N and this number. The next greater power of K can be found by the formula,

K

^{int(log(N)/log(K)) + 1}

Therefore, the minimum number to be added can be computed by:

Minimum Number = K

^{int(log(N)/log(K)) + 1}– N

Below is the implementation of the above approach:

## C++

`// C++ program to find the minimum number ` `// to be added to N to make it a power of K ` ` ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Function to return the minimum number ` `// to be added to N to make it a power of K. ` `int` `minNum(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `x = (` `int` `)(` `log` `(n) / ` `log` `(k)) + 1; ` ` ` ` ` `// Computing the difference between ` ` ` `// then next greater power of K ` ` ` `// and N ` ` ` `int` `mn = ` `pow` `(k, x) - n; ` ` ` `return` `mn; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 20, k = 5; ` ` ` `cout << minNum(n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the minimum number ` `// to be added to N to make it a power of K ` ` ` `class` `GFG{ ` ` ` `// Function to return the minimum number ` `// to be added to N to make it a power of K. ` `static` `int` `minNum(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `x = (` `int` `)(Math.log(n) / Math.log(k)) + ` `1` `; ` ` ` ` ` `// Computing the difference between ` ` ` `// then next greater power of K ` ` ` `// and N ` ` ` `int` `mn = (` `int` `) (Math.pow(k, x) - n); ` ` ` `return` `mn; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `20` `, k = ` `5` `; ` ` ` `System.out.print(minNum(n, k)); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the minimum number ` `# to be added to N to make it a power of K ` `import` `math ` ` ` `# Function to return the minimum number ` `# to be added to N to make it a power of K. ` `def` `minNum(n, k): ` ` ` ` ` `x ` `=` `int` `((math.log(n) ` `/` `/` `math.log(k))) ` `+` `1` ` ` ` ` `# Computing the difference between ` ` ` `# then next greater power of K ` ` ` `# and N ` ` ` `mn ` `=` `pow` `(k, x) ` `-` `n ` ` ` `return` `mn ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` ` ` `n ` `=` `20` ` ` `k ` `=` `5` ` ` `print` `(minNum(n, k)) ` ` ` `# This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

**Output:**

5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the minimum number to be added to N to make it a prime number
- Minimum number to be added to all digits of X to make X > Y
- Minimum number of Parentheses to be added to make it valid
- Find minimum number to be divided to make a number a perfect square
- Find minimum number of coins that make a given value
- Least number to be added to or subtracted from N to make it a Perfect Square
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Find the minimum number of elements that should be removed to make an array good
- Find minimum number of merge operations to make an array palindrome
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
- Find the minimum number of operations required to make all array elements equal
- Minimum number of power terms with sum equal to n
- Minimum removals in a number to be divisible by 10 power raised to K
- Minimum divisor of a number to make the number perfect cube
- Minimum number of swaps required to make a number divisible by 60
- Find whether a given number is a power of 4 or not
- Find the super power of a given Number
- Find minimum operations needed to make an Array beautiful
- Minimum number of changes required to make the given array an AP
- Minimum number of letters needed to make a total of n

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.