Algorithm:
This method can be derived from (but predates) Newton–Raphson method.
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
Implementation:
C++
#include <iostream>
using namespace std;
class gfg {
public:
float squareRoot(float n)
{
float x = n;
float y = 1;
float e = 0.000001;
while (x - y > e) {
x = (x + y) / 2;
y = n / x;
}
return x;
}
};
int main()
{
gfg g;
int n = 50;
cout << "Square root of " << n << " is " << g.squareRoot(n);
getchar();
}
|
C
#include <stdio.h>
float squareRoot(float n)
{
float x = n;
float y = 1;
float e = 0.000001;
while (x - y > e) {
x = (x + y) / 2;
y = n / x;
}
return x;
}
int main()
{
int n = 50;
printf("Square root of %d is %f", n, squareRoot(n));
getchar();
}
|
Java
class GFG {
static float squareRoot(float n)
{
float x = n;
float y = 1;
double e = 0.000001;
while (x - y > e) {
x = (x + y) / 2;
y = n / x;
}
return x;
}
public static void main(String[] args)
{
int n = 50;
System.out.printf("Square root of "
+ n + " is " + squareRoot(n));
}
}
|
Python 3
def squareRoot(n):
x = n
y = 1
e = 0.000001
while(x - y > e):
x = (x + y)/2
y = n / x
return x
n = 50
print("Square root of", n, "is",
round(squareRoot(n), 6))
|
C#
using System;
class GFG {
static float squareRoot(float n)
{
float x = n;
float y = 1;
double e = 0.000001;
while (x - y > e) {
x = (x + y) / 2;
y = n / x;
}
return x;
}
public static void Main()
{
int n = 50;
Console.Write("Square root of "
+ n + " is " + squareRoot(n));
}
}
|
PHP
<?php
function squareRoot($n)
{
$x = $n;
$y = 1;
$e = 0.000001;
while($x - $y > $e)
{
$x = ($x + $y)/2;
$y = $n / $x;
}
return $x;
}
{
$n = 50;
echo "Square root of $n is ", squareRoot($n);
}
?>
|
Javascript
<script>
function squareRoot( n)
{
let x = n;
let y = 1;
let e = 0.000001;
while (x - y > e) {
x = (x + y) / 2;
y = n / x;
}
return x;
}
let n = 50;
document.write( "Square root of "+n+" is " + squareRoot(n).toFixed(6));
</script>
|
Output :
Square root of 50 is 7.071068
Time Complexity: O(n1/2)
Auxiliary Space: O(1)
Example:
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.
If we are sure that n is a perfect square, then we can use following method. The method can go in infinite loop for non-perfect-square numbers. For example, for 3 the below while loop will never terminate.
C++
#include <iostream>
using namespace std;
class gfg {
public:
float squareRoot(float n)
{
float x = n;
float y = 1;
while (x > y) {
x = (x + y) / 2;
y = n / x;
}
return x;
}
};
int main()
{
gfg g;
int n = 49;
cout << "Square root of " << n << " is " << g.squareRoot(n);
getchar();
}
|
C
#include <stdio.h>
unsigned int squareRoot(int n)
{
int x = n;
int y = 1;
while (x > y) {
x = (x + y) / 2;
y = n / x;
}
return x;
}
int main()
{
int n = 49;
printf("root of %d is %d", n, squareRoot(n));
getchar();
}
|
Java
import java.io.*;
public class GFG {
static long squareRoot(int n)
{
int x = n;
int y = 1;
while (x > y) {
x = (x + y) / 2;
y = n / x;
}
return (long)x;
}
static public void main(String[] args)
{
int n = 49;
System.out.println("root of "
+ n + " is " + squareRoot(n));
}
}
|
Python3
def squareRoot(n):
x = n;
y = 1;
while(x > y):
x = (x + y) / 2;
y = n / x;
return x;
n = 49;
print("root of", n, "is", squareRoot(n));
|
C#
using System;
public class GFG {
static uint squareRoot(int n)
{
int x = n;
int y = 1;
while (x > y) {
x = (x + y) / 2;
y = n / x;
}
return (uint)x;
}
static public void Main()
{
int n = 49;
Console.WriteLine("root of "
+ n + " is " + squareRoot(n));
}
}
|
PHP
<?php
function squareRoot( $n)
{
$x = $n;
$y = 1;
while($x > $y)
{
$x = ($x + $y) / 2;
$y =$n / $x;
}
return $x;
}
$n = 49;
echo " root of ", $n, " is ", squareRoot($n);
?>
|
Javascript
<script>
function squareRoot(n) {
var x = n;
var y = 1;
while (x > y) {
x = (x + y) / 2;
y = n / x;
}
return x;
}
var n = 49;
document.write("root of " + n + " is " + squareRoot(n));
</script>
|
Output :
root of 49 is 7
Time Complexity: O(n1/2)
Auxiliary Space: O(1)
References;
http://en.wikipedia.org/wiki/Square_root
http://en.wikipedia.org/wiki/Babylonian_method#Babylonian_method
Asked by Snehal
Please write comments if you find any bug in the above program/algorithm, or if you want to share more information about Babylonian method.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!