Check if a given number can be represented in given a no . of digits in any base | C ++ program to check if a given number can be represented in given number of digits in any base ; Returns true if ' num ' can be represented usind ' dig ' digits in ' base ' ; Base case ; If there are more than 1 digits left and number is more than base , then remove last digit by doing num / base , reduce the number of digits and recur ; return true of num can be represented in ' dig ' digits in any base from 2 to 32 ; Check for all bases one by one ; Driver program
#include <iostream> NEW_LINE using namespace std ; bool checkUtil ( int num , int dig , int base ) { if ( dig == 1 && num < base ) return true ; if ( dig > 1 && num >= base ) return checkUtil ( num / base , -- dig , base ) ; return false ; } bool check ( int num , int dig ) { for ( int base = 2 ; base <= 32 ; base ++ ) if ( checkUtil ( num , dig , base ) ) return true ; return false ; } int main ( ) { int num = 8 ; int dig = 3 ; ( check ( num , dig ) ) ? cout << " Yes " : cout << " No " ; return 0 ; }