Find the integral roots of a given Cubic equation - GeeksforGeeks

22 Apr, 2021
Given 5 integers say A, B, C, D, and E which represents the cubic equation , the task is to find the integral solution for this equation. If there doesn’t exist any integral solution then print “NA”.Examples:  
Input: A = 1, B = 0, C = 0, D = 0, E = 27 Output: 3Input: A = 1, B = 0, C = 0, D = 0, E = 16 Output: NA  
 
Approach: The idea is to use binary search. Below are the steps:  
Initialise the start and end variable as 0 & 105 respectively.Find the middle(say mid) value of start and end check if it satisfy the given equation or not.If current mid satisfy the given equation the print the mid value.Else if the value of f(x) is less than E then update start as mid + 1.Else Update end as mid – 1.If we can’t find any integral solution for the above equation then print “-1”.
Initialise the start and end variable as 0 & 105 respectively.
Find the middle(say mid) value of start and end check if it satisfy the given equation or not.
If current mid satisfy the given equation the print the mid value.
Else if the value of f(x) is less than E then update start as mid + 1.
Else Update end as mid – 1.
If we can’t find any integral solution for the above equation then print “-1”.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the value at x of// the given equationlong long int check(int A, int B, int C,                    int D, long long int x){     long long int ans;     // Find the value equation at x    ans = (A * x * x * x           + B * x * x           + C * x           + D);     // Return the value of ans    return ans;} // Function to find the integral// solution of the given equationvoid findSolution(int A, int B, int C,                  int D, int E){     // Initialise start and end    int start = 0, end = 100000;     long long int mid, ans;     // Implement Binary Search    while (start <= end) {         // Find mid        mid = start + (end - start) / 2;         // Find the value of f(x) using        // current mid        ans = check(A, B, C, D, mid);         // Check if current mid satisfy        // the equation        if (ans == E) {             // Print mid and return            cout << mid << endl;            return;        }         if (ans < E)            start = mid + 1;        else            end = mid - 1;    }     // Print "NA" if not found    // any integral solution    cout << "NA";} // Driver Codeint main(){    int A = 1, B = 0, C = 0;    int D = 0, E = 27;     // Function Call    findSolution(A, B, C, D, E);}
// Java program for the above approachimport java.util.*;class GFG{ // Function to find the value at x of// the given equationstatic long check(int A, int B, int C,                         int D, long x){    long ans;     // Find the value equation at x    ans = (A * x * x * x +           B * x * x + C * x + D);     // Return the value of ans    return ans;} // Function to find the integral// solution of the given equationstatic void findSolution(int A, int B, int C,                         int D, int E){     // Initialise start and end    long start = 0, end = 100000;     long mid, ans;     // Implement Binary Search    while (start <= end)    {         // Find mid        mid = start + (end - start) / 2;         // Find the value of f(x) using        // current mid        ans = check(A, B, C, D, mid);         // Check if current mid satisfy        // the equation        if (ans == E)        {             // Print mid and return            System.out.println(mid);            return;        }         if (ans < E)            start = mid + 1;        else            end = mid - 1;    }     // Print "NA" if not found    // any integral solution    System.out.println("NA");} // Driver Codepublic static void main(String args[]){    int A = 1, B = 0, C = 0;    int D = 0, E = 27;     // Function Call    findSolution(A, B, C, D, E);}} // This code is contributed by Code_Mech
# Python3 program for the above approach # Function to find the value at x of# the given equationdef check(A, B, C, D, x) :     ans = 0;     # Find the value equation at x    ans = (A * x * x * x +           B * x * x + C * x + D);     # Return the value of ans    return ans; # Function to find the integral# solution of the given equationdef findSolution(A, B, C, D, E) :     # Initialise start and end    start = 0; end = 100000;     mid = 0;    ans = 0;     # Implement Binary Search    while (start <= end) :         # Find mid        mid = start + (end - start) // 2;         # Find the value of f(x) using        # current mid        ans = check(A, B, C, D, mid);         # Check if current mid satisfy        # the equation        if (ans == E) :             # Print mid and return            print(mid);            return;         if (ans < E) :            start = mid + 1;        else :            end = mid - 1;     # Print "NA" if not found    # any integral solution    print("NA"); # Driver Codeif __name__ == "__main__" :     A = 1; B = 0; C = 0;    D = 0; E = 27;     # Function Call    findSolution(A, B, C, D, E);     # This code is contributed by AnkitRai01
// C# program for the above approachusing System;class GFG{ // Function to find the value at x of// the given equationstatic long check(int A, int B, int C,                         int D, long x){    long ans;     // Find the value equation at x    ans = (A * x * x * x +           B * x * x + C * x + D);     // Return the value of ans    return ans;} // Function to find the integral// solution of the given equationstatic void findSolution(int A, int B, int C,                         int D, int E){     // Initialise start and end    long start = 0, end = 100000;     long mid, ans;     // Implement Binary Search    while (start <= end)    {         // Find mid        mid = start + (end - start) / 2;         // Find the value of f(x) using        // current mid        ans = check(A, B, C, D, mid);         // Check if current mid satisfy        // the equation        if (ans == E)        {             // Print mid and return            Console.WriteLine(mid);            return;        }         if (ans < E)            start = mid + 1;        else            end = mid - 1;    }     // Print "NA" if not found    // any integral solution    Console.Write("NA");} // Driver Codepublic static void Main(){    int A = 1, B = 0, C = 0;    int D = 0, E = 27;     // Function Call    findSolution(A, B, C, D, E);}} // This code is contributed by Code_Mech
<script> // Javascript program for the above approach // Function to find the value at x of// the given equationfunction check(A, B, C, D, x){    var ans;     // Find the value equation at x    ans = (A * x * x * x +           B * x * x + C * x + D);     // Return the value of ans    return ans;} // Function to find the integral// solution of the given equationfunction findSolution(A, B, C, D, E){     // Initialise start and end    var start = 0, end = 100000;     var mid, ans;     // Implement Binary Search    while (start <= end)    {         // Find mid        mid = parseInt(start + (end - start) / 2);         // Find the value of f(x) using        // current mid        ans = check(A, B, C, D, mid);         // Check if current mid satisfy        // the equation        if (ans == E)        {             // Print mid and return            document.write(mid);            return;        }         if (ans < E)            start = mid + 1;        else            end = mid - 1;    }     // Print "NA" if not found    // any integral solution    document.write("NA");} // Driver Codevar A = 1, B = 0, C = 0;var D = 0, E = 27; // Function CallfindSolution(A, B, C, D, E); // This code is contributed by Ankita saini </script>
3
 
Time Complexity: O(log N) 
ankthon
Code_Mech
ankita_saini
Algebra
Binary Search
maths-cube
root
Articles
Competitive Programming
Mathematical
Searching
Searching
Mathematical
Binary Search
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Time Complexity and Space Complexity
Mutex vs Semaphore
Understanding "extern" keyword in C
Analysis of Algorithms | Set 3 (Asymptotic Notations)
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Competitive Programming - A Complete Guide
Prefix Sum Array - Implementation and Applications in Competitive Programming
Modulo 10^9+7 (1000000007)