Open In App

Find an integral solution of the non-linear equation 2X + 5Y = N

Improve
Improve
Like Article
Like
Save
Share
Report

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++




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


Java




// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
    
// Function to find the value
// of power(X, N)
static int power(int x, int N)
{
     
    // Stores the value
    // of (X ^ N)
    int res = 1;
     
    // Calculate the value of
    // power(x, N)
    while (N > 0)
    {
         
        // If N is odd
        if ((N & 1) != 0)
        {
             
            // Update res
            res = (res * x);
        }
         
        // Update x
        x = (x * x);
         
        // Update N
        N = N >> 1;
    }
    return res;
}
 
// Function to find the value of
// X and Y that satisfy the condition
static void findValX_Y(int N)
{
     
    // Base Case
    if (N <= 1)
    {
        System.out.println(-1);
        return;
    }
 
    // Stores maximum possible
    // of X.
    int xMax;
     
    // Update xMax
    xMax = (int)Math.log(N);
     
    // Stores maximum possible
    // of Y.
    int yMax;
     
    // Update yMax
    yMax = (int)(Math.log(N) / Math.log(5.0));
     
    // Iterate over all possible
    // values of X
    for(int i = 1; i <= xMax; i++)
    {
         
        // Iterate over all possible
        // values of Y
        for(int j = 1; j <= yMax; j++)
        {
             
            // Stores value of 2^i
            int a = power(2, i);
             
            // Stores value of 5^j
            int b = power(5, j);
                 
            // If the pair (i, j)
            // satisfy the equation
            if (a + b == N)
            {
                System.out.print(i + " " + j);
                return;
            }
        }
    }
         
    // If no solution exists
    System.out.println("-1");
}
     
// Driver Code
public static void main(String args[])
{
    int N = 129;
     
    findValX_Y(N);
}
}
 
// This code is contributed by bgangwar59


Python3




# Python3 program to implement
# the above approach
from math import log2
       
# Function to find the value
# of power(X, N)
def power(x, N):
     
    # Stores the value
    # of (X ^ N)
    res = 1
  
    # Calculate the value of
    # power(x, N)
    while (N > 0):
         
        # If N is odd
        if (N & 1):
             
            # Update res
            res = (res * x)
             
        # Update x
        x = (x * x)
         
        # Update N
        N = N >> 1
         
    return res
  
# Function to find the value of
# X and Y that satisfy the condition
def findValX_Y(N):
     
    # Base Case
    if (N <= 1):
       print(-1)
       return
    
    # Stores maximum possible
    # of X
    xMax = 0
     
    # Update xMax
    xMax = int(log2(N))
     
    # Stores maximum possible
    # of Y
    yMax = 0
     
    # Update yMax
    yMax = int(log2(N) / log2(5.0))
     
    # Iterate over all possible
    # values of X
    for i in range(1, xMax + 1):
         
        # Iterate over all possible
        # values of Y
        for j in range(1, yMax + 1):
             
            # Stores value of 2^i
            a = power(2, i)
             
            # Stores value of 5^j
            b = power(5, j)
             
            # If the pair (i, j)
            # satisfy the equation
            if (a + b == N):
                print(i, j)
                return
  
    # If no solution exists
    print(-1)
  
# Driver Code
if __name__ == '__main__':
     
    N = 129
  
    findValX_Y(N)
 
# This code is contributed by mohit kumar 29


C#




// C# program to implement
// the above approach
using System;
class GFG{
    
// Function to find the
// value of power(X, N)
static int power(int x,
                 int N)
{   
  // Stores the value
  // of (X ^ N)
  int res = 1;
 
  // Calculate the value
  // of power(x, N)
  while (N > 0)
  {        
    // If N is odd
    if ((N & 1) != 0)
    {
 
      // Update res
      res = (res * x);
    }
 
    // Update x
    x = (x * x);
 
    // Update N
    N = N >> 1;
  }
  return res;
}
 
// Function to find the
// value of X and Y that
// satisfy the condition
static void findValX_Y(int N)
{    
  // Base Case
  if (N <= 1)
  {
    Console.WriteLine(-1);
    return;
  }
 
  // Stores maximum
  // possible of X.
  int xMax;
 
  // Update xMax
  xMax = (int)Math.Log(N);
 
  // Stores maximum possible
  // of Y.
  int yMax;
 
  // Update yMax
  yMax = (int)(Math.Log(N) /
               Math.Log(5.0));
 
  // Iterate over all possible
  // values of X
  for(int i = 1; i <= xMax; i++)
  {
    // Iterate over all possible
    // values of Y
    for(int j = 1; j <= yMax; j++)
    {
      // Stores value of 2^i
      int a = power(2, i);
 
      // Stores value of 5^j
      int b = power(5, j);
 
      // If the pair (i, j)
      // satisfy the equation
      if (a + b == N)
      {
        Console.Write(i + " " + j);
        return;
      }
    }
  }
 
  // If no solution exists
  Console.WriteLine("-1");
}
 
// Driver Code
public static void Main()
{
  int N = 129;
  findValX_Y(N);
}
}
 
// This code is contributed by surendra_gangwar


Javascript




<script>
 
// JavaScript program to implement the above approach
 
// Function to find the value
// of power(X, N)
function power(x, N)
{
     
    // Stores the value
    // of (X ^ N)
    let res = 1;
     
    // Calculate the value of
    // power(x, N)
    while (N > 0)
    {
         
        // If N is odd
        if ((N & 1) != 0)
        {
             
            // Update res
            res = (res * x);
        }
         
        // Update x
        x = (x * x);
         
        // Update N
        N = N >> 1;
    }
    return res;
}
 
// Function to find the value of
// X and Y that satisfy the condition
function findValX_Y(N)
{
     
    // Base Case
    if (N <= 1)
    {
        document.write(-1);
        return;
    }
 
    // Stores maximum possible
    // of X.
    let xMax;
     
    // Update xMax
    xMax = Math.log(N);
     
    // Stores maximum possible
    // of Y.
    let yMax;
     
    // Update yMax
    yMax = Math.log(N) / Math.log(5.0);
     
    // Iterate over all possible
    // values of X
    for(let i = 1; i <= xMax; i++)
    {
         
        // Iterate over all possible
        // values of Y
        for(let j = 1; j <= yMax; j++)
        {
             
            // Stores value of 2^i
            let a = power(2, i);
             
            // Stores value of 5^j
            let b = power(5, j);
                 
            // If the pair (i, j)
            // satisfy the equation
            if (a + b == N)
            {
                document.write(i + " " + j);
                return;
            }
        }
    }
         
    // If no solution exists
    document.write("-1");
}
 
// Driver Code
 
    let N = 129;
    findValX_Y(N);
     
    // This code is contributed by susmitakundugoaldanga.
</script>


Output:

2 3

Time Complexity: O(log2N * log5N)
Auxiliary Space: O(1)



Last Updated : 19 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads