Generate an N | C ++ program for the above approach ; Utility function to find x ^ y in O ( log ( y ) ) ; Stores the result ; Update x if it is >= p ; If y is odd ; Multiply x with res ; y must be even now Set y = y / 2 ; Function to generate the N digit number satisfying the given conditions ; Find all possible integers upto 2 ^ N ; Generate binary representation of i ; Reduce the length of the string to N ; If current bit is '0' ; Convert string to equivalent integer ; If condition satisfies ; Print and break ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int power ( long long int x , unsigned long long int y ) { long long int res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) ; y = y >> 1 ; x = ( x * x ) ; } return res ; } void printNth ( int N ) { for ( long long int i = 1 ; i <= power ( 2 , N ) ; i ++ ) { string s = bitset < 64 > ( i ) . to_string ( ) ; string s1 = s . substr ( s . length ( ) - N , s . length ( ) ) ; for ( long long int j = 0 ; s1 [ j ] ; j ++ ) { if ( s1 [ j ] == '0' ) { s1 [ j ] = '2' ; } } long long int res = stoll ( s1 , nullptr , 10 ) ; if ( res % power ( 2 , N ) == 0 ) { cout << res << endl ; break ; } } } int main ( ) { int N = 15 ; printNth ( N ) ; }