Find an integral solution of the non | C ++ program to implement the above approach ; Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the value of X and Y that satisfy the condition ; Base Case ; Stores maximum possible of X . ; Update xMax ; Stores maximum possible of Y . ; Update yMax ; Iterate over all possible values of X ; Iterate over all possible values of Y ; Stores value of 2 ^ i ; Stores value of 5 ^ j ; If the pair ( i , j ) satisfy the equation ; If no solution exists ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long power ( long long x , long long N ) { long long res = 1 ; while ( N > 0 ) { if ( N & 1 ) { res = ( res * x ) ; } x = ( x * x ) ; N = N >> 1 ; } return res ; } void findValX_Y ( long long N ) { if ( N <= 1 ) { cout << -1 << endl ; return ; } int xMax ; xMax = log2 ( N ) ; int yMax ; yMax = ( log2 ( N ) / log2 ( 5.0 ) ) ; for ( long long i = 1 ; i <= xMax ; i ++ ) { for ( long long j = 1 ; j <= yMax ; j ++ ) { long long a = power ( 2 , i ) ; long long b = power ( 5 , j ) ; if ( a + b == N ) { cout << i << " ▁ " << j << endl ; return ; } } } cout << -1 << endl ; } int main ( ) { long long N = 129 ; findValX_Y ( N ) ; return 0 ; }