Check if a number N can be expressed as the sum of powers of X or not | C ++ program for the above approach ; Function to check if the number N can be expressed as the sum of different powers of X or not ; While n is a positive number ; Find the remainder ; If rem is at least 2 , then representation is impossible ; Divide the value of N by x ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ToCheckPowerofX ( int n , int x ) { while ( n > 0 ) { int rem = n % x ; if ( rem >= 2 ) { return false ; } n = n / x ; } return true ; } int main ( ) { int N = 10 , X = 3 ; if ( ToCheckPowerofX ( N , X ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }