How To Find The Turning Point Of A Cubic Function
Discover the integral roots of a given Cubic equation
Given 5 integers say A, B, C, D, and Eastward which represents the cubic equation, the job is to observe 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: 3
Input: A = 1, B = 0, C = 0, D = 0, E = 16
Output: NA
Arroyo: The idea is to use binary search. Beneath are the steps:
- Initialise the first 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 non.
- If current mid satisfy the given equation the print the mid value.
- Else if the value of f(10) is less than E then update commencement as mid + one.
- Else Update end as mid – i.
- If we can't find any integral solution for the above equation and so print "-1".
Beneath is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using
namespace
std;
long
long
int
check(
int
A,
int
B,
int
C,
int
D,
long
long
int
10)
{
long
long
int
ans;
ans = (A * x * x * x
+ B * ten * x
+ C * x
+ D);
return
ans;
}
void
findSolution(
int
A,
int
B,
int
C,
int
D,
int
E)
{
int
start = 0, finish = 100000;
long
long
int
mid, ans;
while
(first <= end) {
mid = kickoff + (end - start) / ii;
ans = check(A, B, C, D, mid);
if
(ans == E) {
cout << mid << endl;
return
;
}
if
(ans < E)
start = mid + 1;
else
end = mid - 1;
}
cout <<
"NA"
;
}
int
main()
{
int
A = 1, B = 0, C = 0;
int
D = 0, E = 27;
findSolution(A, B, C, D, East);
}
Java
import
java.util.*;
class
GFG{
static
long
check(
int
A,
int
B,
int
C,
int
D,
long
x)
{
long
ans;
ans = (A * x * ten * x +
B * x * x + C * ten + D);
return
ans;
}
static
void
findSolution(
int
A,
int
B,
int
C,
int
D,
int
East)
{
long
commencement =
0
, end =
100000
;
long
mid, ans;
while
(commencement <= end)
{
mid = start + (terminate - start) /
2
;
ans = cheque(A, B, C, D, mid);
if
(ans == Eastward)
{
System.out.println(mid);
return
;
}
if
(ans < Due east)
offset = mid +
1
;
else
end = mid -
1
;
}
System.out.println(
"NA"
);
}
public
static
void
main(Cord args[])
{
int
A =
1
, B =
0
, C =
0
;
int
D =
0
, E =
27
;
findSolution(A, B, C, D, East);
}
}
Python3
def
bank check(A, B, C, D, x) :
ans
=
0
;
ans
=
(A
*
ten
*
x
*
x
+
B
*
x
*
x
+
C
*
x
+
D);
render
ans;
def
findSolution(A, B, C, D, E) :
start
=
0
; stop
=
100000
;
mid
=
0
;
ans
=
0
;
while
(beginning <
=
end) :
mid
=
start
+
(end
-
start)
/
/
2
;
ans
=
check(A, B, C, D, mid);
if
(ans
=
=
East) :
print
(mid);
return
;
if
(ans < E) :
starting time
=
mid
+
1
;
else
:
end
=
mid
-
1
;
print
(
"NA"
);
if
__name__
=
=
"__main__"
:
A
=
1
; B
=
0
; C
=
0
;
D
=
0
; E
=
27
;
findSolution(A, B, C, D, E);
C#
using
Arrangement;
class
GFG{
static
long
check(
int
A,
int
B,
int
C,
int
D,
long
10)
{
long
ans;
ans = (A * x * x * x +
B * x * x + C * x + D);
return
ans;
}
static
void
findSolution(
int
A,
int
B,
int
C,
int
D,
int
East)
{
long
offset = 0, end = 100000;
long
mid, ans;
while
(start <= end)
{
mid = start + (terminate - start) / 2;
ans = check(A, B, C, D, mid);
if
(ans == E)
{
Console.WriteLine(mid);
return
;
}
if
(ans < E)
outset = mid + ane;
else
stop = mid - 1;
}
Panel.Write(
"NA"
);
}
public
static
void
Main()
{
int
A = 1, B = 0, C = 0;
int
D = 0, E = 27;
findSolution(A, B, C, D, E);
}
}
Javascript
<script>
function
check(A, B, C, D, x)
{
var
ans;
ans = (A * x * 10 * ten +
B * x * x + C * x + D);
return
ans;
}
function
findSolution(A, B, C, D, E)
{
var
offset = 0, cease = 100000;
var
mid, ans;
while
(start <= terminate)
{
mid = parseInt(start + (end - start) / ii);
ans = check(A, B, C, D, mid);
if
(ans == E)
{
document.write(mid);
return
;
}
if
(ans < Due east)
kickoff = mid + one;
else
cease = mid - 1;
}
certificate.write(
"NA"
);
}
var
A = 1, B = 0, C = 0;
var
D = 0, E = 27;
findSolution(A, B, C, D, E);
</script>
Time Complexity: O(log Northward)
Source: https://www.geeksforgeeks.org/find-the-integral-roots-of-a-given-cubic-equation/
Posted by: ottmanpreal1956.blogspot.com
0 Response to "How To Find The Turning Point Of A Cubic Function"
Post a Comment