# Representation of a number in powers of other

Given two numbers w and m, we need to determine whether it is possible to represent m in terms of powers of w. The powers of number w can be added or subtracted to obtain m and each powers of w can be used only once .

**Examples:**

Input : 3 7 Output : Yes As 7 = 9 - 3 + 1 (3^2 - 3^1 + 3^0 ) so it is possible . Input : 100 50 Output : No As 50 is less than 100 so we can never represent it in the powers of 100 .

Here we have to represent m in terms of powers of w used only once so it can be shown through the following equation .

**c0 + c1*w^1 + c2*w^2 + … = m —— (Equation 1)**

Where each c0, c1, c2 … are either **-1** (for subtracting that power of w ), **0** (not using that power of w ), **1** (for adding that power of w ) .

**=> c1*w^1 + c2*w^2 + … = m – c0
=> w(c1 + c2*w^1 + c3*w^2 + … ) = m – c0
=> c1 + c2*w^1 + c3*w^2 + … = (m – c0)/w —— (Equation 2)**

Now, notice equation 1 and equation 2 — we are trying to solve the same problem all over again. So we have to recurse till m > 0 . For such a solution to exist (m — ci) must be a multiple of w, where ci is the coefficient of the equation . The ci can be -1, 0, 1 . So we have to check for all three possibilities **( ( m – 1 ) % w == 0), ( ( m + 1 ) % w == 0) and ( m % w == 0) **. If it is not, then there will not be any solution.

## C++

`// CPP program to check if m can be represented ` `// as powers of w. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `bool` `asPowerSum(` `int` `w, ` `int` `m) ` `{ ` ` ` `while` `(m) { ` ` ` `if` `((m - 1) % w == 0) ` ` ` `m = (m - 1) / w; ` ` ` `else` `if` `((m + 1) % w == 0) ` ` ` `m = (m + 1) / w; ` ` ` ` ` `else` `if` `(m % w == 0) ` ` ` `m = m / w; ` ` ` ` ` `else` ` ` `break` `; ` `// None of 3 worked. ` ` ` `} ` ` ` ` ` `// If m is not zero means, it can't be ` ` ` `// represented in terms of powers of w. ` ` ` `return` `(m == 0); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `w = 3, m = 7; ` ` ` `if` `(asPowerSum(w, m)) ` ` ` `cout << ` `"Yes"` `<< endl; ` ` ` `else` ` ` `cout << ` `"No"` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check if m can ` `// be represented as powers of w. ` ` ` `class` `GFG ` `{ ` ` ` `static` `boolean` `asPowerSum(` `int` `w, ` `int` `m) ` ` ` `{ ` ` ` `while` `(m > ` `0` `) ` ` ` `{ ` ` ` `if` `((m - ` `1` `) % w == ` `0` `) ` ` ` `m = (m - ` `1` `) / w; ` ` ` ` ` `else` `if` `((m + ` `1` `) % w == ` `0` `) ` ` ` `m = (m + ` `1` `) / w; ` ` ` ` ` `else` `if` `(m % w == ` `0` `) ` ` ` `m = m / w; ` ` ` ` ` `else` ` ` `break` `; ` `// None of 3 worked. ` ` ` `} ` ` ` ` ` `// If m is not zero means, it can't be ` ` ` `// represented in terms of powers of w. ` ` ` `return` `(m == ` `0` `); ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `w = ` `3` `, m = ` `7` `; ` ` ` `if` `(asPowerSum(w, m)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to check if m can

# be represented as powers of w.

def asPowerSum(w, m):

while (m > 0):

if ((m – 1) % w == 0):

m = (m – 1) / w;

elif ((m + 1) % w == 0):

m = (m + 1) / w;

elif (m % w == 0):

m = m / w;

else:

break; # None of 3 worked.

# If m is not zero means, it can’t be

# represented in terms of powers of w.

return (m == 0);

# Driver code

w = 3;

m = 7;

if (asPowerSum(w, m)):

print(“Yes”);

else:

print(“No”);

# This code is contributed by mits

## C#

`// C# program to check if ` `// m can be represented ` `// as powers of w. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `bool` `asPowerSum(` `int` `w, ` ` ` `int` `m) ` ` ` `{ ` ` ` `while` `(m > 0) ` ` ` `{ ` ` ` `if` `((m - 1) % w == 0) ` ` ` `m = (m - 1) / w; ` ` ` ` ` `else` `if` `((m + 1) % w == 0) ` ` ` `m = (m + 1) / w; ` ` ` ` ` `else` `if` `(m % w == 0) ` ` ` `m = m / w; ` ` ` ` ` `else` ` ` `break` `; ` `// None of 3 worked. ` ` ` `} ` ` ` ` ` `// If m is not zero means, ` ` ` `// it can't be represented ` ` ` `// in terms of powers of w. ` ` ` `return` `(m == 0); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `w = 3, m = 7; ` ` ` `if` `(asPowerSum(w, m)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed ` `// by akt_mit. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to check if m can ` `// be represented as powers of w. ` ` ` `function` `asPowerSum(` `$w` `, ` `$m` `) ` `{ ` ` ` `while` `(` `$m` `) ` ` ` `{ ` ` ` `if` `((` `$m` `- 1) % ` `$w` `== 0) ` ` ` `$m` `= (` `$m` `- 1) / ` `$w` `; ` ` ` `else` `if` `((` `$m` `+ 1) % ` `$w` `== 0) ` ` ` `$m` `= (` `$m` `+ 1) / ` `$w` `; ` ` ` ` ` `else` `if` `(` `$m` `% ` `$w` `== 0) ` ` ` `$m` `= ` `$m` `/ ` `$w` `; ` ` ` ` ` `else` ` ` `break` `; ` `// None of 3 worked. ` ` ` `} ` ` ` ` ` `// If m is not zero means, it can't be ` ` ` `// represented in terms of powers of w. ` ` ` `return` `(` `$m` `== 0); ` `} ` ` ` `// Driver code ` `$w` `= 3; ` `$m` `= 7; ` `if` `(asPowerSum(` `$w` `, ` `$m` `)) ` ` ` `echo` `"Yes\n"` `; ` `else` ` ` `echo` `"No\n"` `; ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Yes

## Recommended Posts:

- Minimum number of given powers of 2 required to represent a number
- Check if a number can be represented as sum of non zero powers of 2
- Form a number using corner digits of powers
- Number of triangles possible with given lengths of sticks which are powers of 2
- Balance pans using given weights that are powers of a number
- Sum of largest divisible powers of p (a prime number) in a range
- Sum of first N natural numbers by taking powers of 2 as negative number
- Count number of trailing zeros in Binary representation of a number using Bitset
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Number of leading zeros in binary representation of a given number
- Next greater number than N with exactly one bit different in binary representation of N
- Number of trailing zeroes in base B representation of N!
- Prime Number of Set Bits in Binary Representation | Set 1
- Prime Number of Set Bits in Binary Representation | Set 2
- Check if binary representation of a number is palindrome

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.