Newton Method Algorithm

In numerical analysis, Newton's method, also known as the Newton – Raphson method, named after Isaac Newton and Joseph Raphson, is a root-finding algorithm which produces successively better approximations to the beginnings (or zeroes) of a real-valued function. The most basic version begins with a individual-variable function f specify for a real variable X, the function's derivative F   ′, and an initial guess x0 for a root of f. Raphson again viewed Newton's method purely as an algebraic method and restricted its purpose to polynomials, but he describes the method in terms of the successive approximations xn instead of the more complicated sequence of polynomials used by Newton. The name" Newton's method" is derived from Isaac Newton's description of a special case of the method in De analysi per aequationesFinally, in 1740, Thomas Simpson described Newton's method as an iterative method for solve general nonlinear equations use calculus, essentially give the description above.
/*
 * Copyright (c) 2017 Kotlin Algorithm Club
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.algorithmexamples.math

fun sqrt(c: Int, e: Double = 1e-15): Double {
    return sqrt(c.toDouble(), e)
}

fun sqrt(c: Double, e: Double = 1e-15): Double {
    if (c < 0) return Double.NaN
    var t = c
    while (Math.abs(t - c / t) > e * t)
        t = (c / t + t) / 2.0
    return t
}

LANGUAGE:

DARK MODE: