Number of digits in the nth number made of given four digits | C ++ program to count number of digits in n - th number made of given four digits . ; Efficient function to calculate number of digits in the nth number constructed by using 6 , 1 , 4 and 9 as digits in the ascending order . ; Number of digits increase after every i - th number where i increases in powers of 4. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int number_of_digits ( int n ) { int i , res , sum = 0 ; for ( i = 4 , res = 1 ; ; i *= 4 , res ++ ) { sum += i ; if ( sum >= n ) break ; } return res ; } int main ( ) { int n = 21 ; cout << number_of_digits ( n ) << endl ; return 0 ; }