📜  巴比伦方根法

📅  最后修改于: 2021-05-06 20:42:04             🧑  作者: Mango

算法:
该方法可以源自(但早于)牛顿-拉夫森法。

1 Start with an arbitrary positive start value x (the closer to the 
   root, the better).
2 Initialize y = 1.
3. Do following until desired approximation is achieved.
  a) Get the next approximation for root using average of x and y
  b) Set y = n/x

执行:

C++
#include 
using namespace std;
class gfg {
    /*Returns the square root of n. Note that the function */
public:
    float squareRoot(float n)
    {
        /*We are using n itself as initial approximation
          This can definitely be improved */
        float x = n;
        float y = 1;
        float e = 0.000001; /* e decides the accuracy level*/
        while (x - y > e) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }
};
 
/* Driver program to test above function*/
int main()
{
    gfg g;
    int n = 50;
    cout << "Square root of " << n << " is " << g.squareRoot(n);
    getchar();
}


C
#include 
 
/*Returns the square root of n. Note that the function */
float squareRoot(float n)
{
    /*We are using n itself as initial approximation
   This can definitely be improved */
    float x = n;
    float y = 1;
    float e = 0.000001; /* e decides the accuracy level*/
    while (x - y > e) {
        x = (x + y) / 2;
        y = n / x;
    }
    return x;
}
 
/* Driver program to test above function*/
int main()
{
    int n = 50;
    printf("Square root of %d is %f", n, squareRoot(n));
    getchar();
}


Java
class GFG {
 
    /*Returns the square root of n.
    Note that the function */
    static float squareRoot(float n)
    {
 
        /*We are using n itself as
        initial approximation This
        can definitely be improved */
        float x = n;
        float y = 1;
 
        // e decides the accuracy level
        double e = 0.000001;
        while (x - y > e) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }
 
    /* Driver program to test
    above function*/
    public static void main(String[] args)
    {
        int n = 50;
 
        System.out.printf("Square root of "
                          + n + " is " + squareRoot(n));
    }
}
 
// This code is contriubted by
// Smitha DInesh Semwal


Python 3
# Returns the square root of n.
# Note that the function
def squareRoot(n):
 
    # We are using n itself as
    # initial approximation This
    # can definitely be improved
        x = n
        y = 1
         
        # e decides the accuracy level
        e = 0.000001
        while(x - y > e):
     
            x = (x + y)/2
            y = n / x
     
        return x
 
# Driver program to test
# above function
n = 50
 
print("Square root of", n, "is",
              round(squareRoot(n), 6))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# Porgram for Babylonian
// method of square root
using System;
 
class GFG {
 
    // Returns the square root of n.
    // Note that the function
    static float squareRoot(float n)
    {
 
        // We are using n itself as
        // initial approximation This
        // can definitely be improved
        float x = n;
        float y = 1;
 
        // e decides the
        // accuracy level
        double e = 0.000001;
        while (x - y > e) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 50;
        Console.Write("Square root of "
                      + n + " is " + squareRoot(n));
    }
}
 
// This code is contriubted by nitin mittal.


PHP
 $e)
    {
        $x = ($x + $y)/2;
        $y = $n / $x;
    }
    return $x;
}
 
// Driver Code
{
    $n = 50;
    echo "Square root of $n is ", squareRoot($n);
}
 
// This code is contributed by nitin mittal.
?>


Javascript


C++
// C++ program for Babylonian
// method for square root
#include 
using namespace std;
 
class gfg {
 
    /* Returns the square root of
   n. Note that the function
   will not work for numbers
   which are not perfect
   squares*/
public:
    float squareRoot(float n)
    {
        /* We are using n itself as an initial
          approximation.  This can definitely be
          improved */
        float x = n;
        float y = 1;
 
        while (x > y) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }
};
 
/* Driver code*/
int main()
{
    gfg g;
    int n = 49;
    cout << "Square root of " << n << " is " << g.squareRoot(n);
    getchar();
}
 
// This code is edited by Dark_Dante_


C
// C program for Babylonian
// method for square root
#include 
 
/* Returns the square root of
   n. Note that the function
   will not work for numbers
   which are not perfect
   squares*/
unsigned int squareRoot(int n)
{
    int x = n;
    int y = 1;
    while (x > y) {
        x = (x + y) / 2;
        y = n / x;
    }
    return x;
}
 
// Driver Code
int main()
{
    int n = 49;
    printf("root of %d is %d", n, squareRoot(n));
    getchar();
}


Java
// Java program for Babylonian
// method for square root
import java.io.*;
 
public class GFG {
 
    /* Returns the square root of
    n. Note that the function
    will not work for numbers
    which are not perfect
    squares*/
    static long squareRoot(int n)
    {
        int x = n;
        int y = 1;
        while (x > y) {
            x = (x + y) / 2;
            y = n / x;
        }
        return (long)x;
    }
 
    // Driver Code
    static public void main(String[] args)
    {
        int n = 49;
        System.out.println("root of "
                           + n + " is " + squareRoot(n));
    }
}
 
// This code is contributed by anuj_67.


Python3
# python3 program for Babylonian
# method for square root
 
# Returns the square root of n.
# Note that the function
# will not work for numbers
# which are not perfect squares
 
def squareRoot(n):
    x = n;
    y = 1;
    while(x > y):
        x = (x + y) / 2;
        y = n / x;
    return x;
 
# Driver Code
n = 49;
print("root of", n, "is", squareRoot(n));
 
# This code is contributed by mits.


C#
// C# program for Babylonian
// method for square root
 
using System;
 
public class GFG {
 
    /* Returns the square root of
    n. Note that the function
    will not work for numbers
    which are not perfect
    squares*/
    static uint squareRoot(int n)
    {
        int x = n;
        int y = 1;
        while (x > y) {
            x = (x + y) / 2;
            y = n / x;
        }
        return (uint)x;
    }
 
    // Driver Code
    static public void Main()
    {
        int n = 49;
        Console.WriteLine("root of "
                          + n + " is " + squareRoot(n));
    }
}
 
// This code is contributed by anuj_67.


PHP
 $y)
    {
        $x = ($x + $y) / 2;
        $y =$n / $x;
    }
    return $x;
}
 
// Driver Code
$n = 49;
echo " root of ", $n, " is ", squareRoot($n);
           
// This code is contributed by anuj_67.
?>


Javascript


输出 :

Square root of 50 is 7.071068

例子:

n = 4 /*n itself is used for initial approximation*/
Initialize x = 4, y = 1
Next Approximation x = (x + y)/2 (= 2.500000), 
y = n/x  (=1.600000)
Next Approximation x = 2.050000,
y = 1.951220
Next Approximation x = 2.000610,
y = 1.999390
Next Approximation x = 2.000000, 
y = 2.000000
Terminate as (x - y) > e now.

如果确定n是一个完美的平方,则可以使用以下方法。对于非完美平方数,该方法可以无限循环。例如,对于3,下面的while循环将永远不会终止。

C++

// C++ program for Babylonian
// method for square root
#include 
using namespace std;
 
class gfg {
 
    /* Returns the square root of
   n. Note that the function
   will not work for numbers
   which are not perfect
   squares*/
public:
    float squareRoot(float n)
    {
        /* We are using n itself as an initial
          approximation.  This can definitely be
          improved */
        float x = n;
        float y = 1;
 
        while (x > y) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }
};
 
/* Driver code*/
int main()
{
    gfg g;
    int n = 49;
    cout << "Square root of " << n << " is " << g.squareRoot(n);
    getchar();
}
 
// This code is edited by Dark_Dante_

C

// C program for Babylonian
// method for square root
#include 
 
/* Returns the square root of
   n. Note that the function
   will not work for numbers
   which are not perfect
   squares*/
unsigned int squareRoot(int n)
{
    int x = n;
    int y = 1;
    while (x > y) {
        x = (x + y) / 2;
        y = n / x;
    }
    return x;
}
 
// Driver Code
int main()
{
    int n = 49;
    printf("root of %d is %d", n, squareRoot(n));
    getchar();
}

Java

// Java program for Babylonian
// method for square root
import java.io.*;
 
public class GFG {
 
    /* Returns the square root of
    n. Note that the function
    will not work for numbers
    which are not perfect
    squares*/
    static long squareRoot(int n)
    {
        int x = n;
        int y = 1;
        while (x > y) {
            x = (x + y) / 2;
            y = n / x;
        }
        return (long)x;
    }
 
    // Driver Code
    static public void main(String[] args)
    {
        int n = 49;
        System.out.println("root of "
                           + n + " is " + squareRoot(n));
    }
}
 
// This code is contributed by anuj_67.

Python3

# python3 program for Babylonian
# method for square root
 
# Returns the square root of n.
# Note that the function
# will not work for numbers
# which are not perfect squares
 
def squareRoot(n):
    x = n;
    y = 1;
    while(x > y):
        x = (x + y) / 2;
        y = n / x;
    return x;
 
# Driver Code
n = 49;
print("root of", n, "is", squareRoot(n));
 
# This code is contributed by mits.

C#

// C# program for Babylonian
// method for square root
 
using System;
 
public class GFG {
 
    /* Returns the square root of
    n. Note that the function
    will not work for numbers
    which are not perfect
    squares*/
    static uint squareRoot(int n)
    {
        int x = n;
        int y = 1;
        while (x > y) {
            x = (x + y) / 2;
            y = n / x;
        }
        return (uint)x;
    }
 
    // Driver Code
    static public void Main()
    {
        int n = 49;
        Console.WriteLine("root of "
                          + n + " is " + squareRoot(n));
    }
}
 
// This code is contributed by anuj_67.

的PHP

 $y)
    {
        $x = ($x + $y) / 2;
        $y =$n / $x;
    }
    return $x;
}
 
// Driver Code
$n = 49;
echo " root of ", $n, " is ", squareRoot($n);
           
// This code is contributed by anuj_67.
?>

Java脚本


输出 :

root of 49 is 7