Count bases which contains a set bit as the Most Significant Bit in the representation of N | C ++ program for the above approach ; Function to count bases having MSB of N as a set bit ; Store the required count ; Iterate over the range [ 2 , N ] ; Store the MSB of N ; If MSB is 1 , then increment the count by 1 ; Return the count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOfBase ( int N ) { int count = 0 ; for ( int i = 2 ; i <= N ; ++ i ) { int highestPower = ( int ) ( log ( N ) / log ( i ) ) ; int firstDigit = N / ( int ) pow ( i , highestPower ) ; if ( firstDigit == 1 ) { ++ count ; } } return count ; } int main ( ) { int N = 6 ; cout << countOfBase ( N ) ; return 0 ; }