#include <stdio.h>
#include <math.h>
/*
* Computes the value of the polynomial:
* f(x) = x + x^2 + x^4 + x^5 + x^8
*/
double evaluatePolynomial(double x) {
return x
+ pow(x, 2)
+ pow(x, 4)
+ pow(x, 5)
+ pow(x, 8);
}
/*
* Performs a binary search to find the value of x such that:
* f(x) ≈ targetValue
*
* The function assumes f(x) is strictly increasing for x > 0.
*/
double findRootBinarySearch(double targetValue, double left, double right) {
while (right - left > 1e-9) { // stop when precision is high enough
double mid = (left + right) / 2.0;
double value = evaluatePolynomial(mid);
if (value < targetValue)
left = mid;
else
right = mid;
}
return (left + right) / 2.0;
}
int main() {
double target = 6897.0;
// Search range chosen because the solution is known to be small and positive
double x = findRootBinarySearch(target, 0.0, 10.0);
printf("Approximate solution: x = %.10f\n", x);
return 0;
}
/*
run:
Approximate solution: x = 3.0000000002
*/