#include <iostream>
#include <cmath>

double newtonRaphson(double (*f)(double), double (*f_prime)(double), double x0, double tol) {
    double x1;
    while (true) {
        x1 = x0 - f(x0) / f_prime(x0);
        if (fabs(x1 - x0) < tol) break;
        x0 = x1;
    }
    return x1;
}

double f(double x) {
    return x * x - 2;  // معادله x^2 - 2 = 0
}

double f_prime(double x) {
    return 2 * x;  // مشتق معادله
}

int main() {
    double initial_guess = 1.0;
    double tolerance = 0.0001;

    double root = newtonRaphson(f, f_prime, initial_guess, tolerance);

    std::cout << "ریشه معادله برابر است با: " << root << std::endl;

    return 0;
}