Find an integral solution of the non-linear equation 2X + 5Y = N
Given an integer N representing a non-linear equation of the form 2X + 5Y = N, the task is to find an integral pair (X, Y) that satisfies the given equation. If multiple solutions exist, then print any one of them. Otherwise, print -1.
Examples:
Input: N = 29
Output: X = 2, Y = 2
Explanation:
Since, 22 + 52 = 29
Therefore, X = 2 and Y = 2 satisfy the given equation.
Input: N = 81
Output: -1
Approach: Follow the steps below to solve the problem:
- Initialize a variable, Say xMax to store the maximum possible value of X.
- Update xMax to log2N.
- Initialize a variable, say yMax to store the maximum possible of Y.
- Update yMax to log5N
- Iterate over all possible values of X and Y and for each value of X and Y and for each pair, check if it satisfies the given equation or not. If found to be true, then print the corresponding value of X and Y.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class GFG{
static int power( int x, int N)
{
int res = 1 ;
while (N > 0 )
{
if ((N & 1 ) != 0 )
{
res = (res * x);
}
x = (x * x);
N = N >> 1 ;
}
return res;
}
static void findValX_Y( int N)
{
if (N <= 1 )
{
System.out.println(- 1 );
return ;
}
int xMax;
xMax = ( int )Math.log(N);
int yMax;
yMax = ( int )(Math.log(N) / Math.log( 5.0 ));
for ( int i = 1 ; i <= xMax; i++)
{
for ( int j = 1 ; j <= yMax; j++)
{
int a = power( 2 , i);
int b = power( 5 , j);
if (a + b == N)
{
System.out.print(i + " " + j);
return ;
}
}
}
System.out.println( "-1" );
}
public static void main(String args[])
{
int N = 129 ;
findValX_Y(N);
}
}
|
Python3
from math import log2
def power(x, N):
res = 1
while (N > 0 ):
if (N & 1 ):
res = (res * x)
x = (x * x)
N = N >> 1
return res
def findValX_Y(N):
if (N < = 1 ):
print ( - 1 )
return
xMax = 0
xMax = int (log2(N))
yMax = 0
yMax = int (log2(N) / log2( 5.0 ))
for i in range ( 1 , xMax + 1 ):
for j in range ( 1 , yMax + 1 ):
a = power( 2 , i)
b = power( 5 , j)
if (a + b = = N):
print (i, j)
return
print ( - 1 )
if __name__ = = '__main__' :
N = 129
findValX_Y(N)
|
C#
using System;
class GFG{
static int power( int x,
int N)
{
int res = 1;
while (N > 0)
{
if ((N & 1) != 0)
{
res = (res * x);
}
x = (x * x);
N = N >> 1;
}
return res;
}
static void findValX_Y( int N)
{
if (N <= 1)
{
Console.WriteLine(-1);
return ;
}
int xMax;
xMax = ( int )Math.Log(N);
int yMax;
yMax = ( int )(Math.Log(N) /
Math.Log(5.0));
for ( int i = 1; i <= xMax; i++)
{
for ( int j = 1; j <= yMax; j++)
{
int a = power(2, i);
int b = power(5, j);
if (a + b == N)
{
Console.Write(i + " " + j);
return ;
}
}
}
Console.WriteLine( "-1" );
}
public static void Main()
{
int N = 129;
findValX_Y(N);
}
}
|
Javascript
<script>
function power(x, N)
{
let res = 1;
while (N > 0)
{
if ((N & 1) != 0)
{
res = (res * x);
}
x = (x * x);
N = N >> 1;
}
return res;
}
function findValX_Y(N)
{
if (N <= 1)
{
document.write(-1);
return ;
}
let xMax;
xMax = Math.log(N);
let yMax;
yMax = Math.log(N) / Math.log(5.0);
for (let i = 1; i <= xMax; i++)
{
for (let j = 1; j <= yMax; j++)
{
let a = power(2, i);
let b = power(5, j);
if (a + b == N)
{
document.write(i + " " + j);
return ;
}
}
}
document.write( "-1" );
}
let N = 129;
findValX_Y(N);
</script>
|
Output:
2 3
Time Complexity: O(log2N * log5N)
Auxiliary Space: O(1)
Last Updated :
19 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...