#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

double f(double x) {
    return x*x*x - x - 2;
}

int main() {
    double x1, x2, x3, f1, f2, tolerance;
    int iteration = 0, max_iterations = 1000;

    cout << "Enter the value of x1: ";
    cin >> x1;
    cout << "Enter the value of x2: ";
    cin >> x2;
    cout << "Enter tolerance value: ";
    cin >> tolerance;

    cout << "\nIteration\t x1\t\t x2\t\t x3\t\t f(x1)\t\t f(x2)\n";
    cout << fixed << setprecision(6);

    do {
        f1 = f(x1);
        f2 = f(x2);

        if (fabs(f2 - f1) < 1e-12) {
            cout << "ভাগফল শূন্যের কাছাকাছি, গণনা করা যায় না।" << endl;
            break;
        }

        x3 = (f2 * x1 - f1 * x2) / (f2 - f1);
        double fx3 = f(x3);

        cout << iteration + 1 << "\t\t " << x1 << "\t " << x2 << "\t " << x3
             << "\t " << f1 << "\t " << f2 << endl;

        x1 = x2;
        x2 = x3;
        f1 = f2;
        f2 = fx3;

        iteration++;

        if (iteration >= max_iterations) {
            cout << "সমাধান নির্ধারিত সংখ্যক ধাপে সন্নিকটে পৌঁছায়নি।" << endl;
            break;
        }

    } while (fabs(f(x3)) > tolerance);

    if (fabs(f(x3)) <= tolerance) {
        cout << "\nApproximate root = " << x3 << endl;
    }

    return 0;
}
