Finding Square Root Without Using Sqrt Function

Finding square root without using sqrt function?

There is a better algorithm, which needs at most 6 iterations to converge to maximum precision for double numbers:

#include <math.h>

double sqrt(double x) {
if (x <= 0)
return 0; // if negative number throw an exception?
int exp = 0;
x = frexp(x, &exp); // extract binary exponent from x
if (exp & 1) { // we want exponent to be even
exp--;
x *= 2;
}
double y = (1+x)/2; // first approximation
double z = 0;
while (y != z) { // yes, we CAN compare doubles here!
z = y;
y = (y + x/y) / 2;
}
return ldexp(y, exp/2); // multiply answer by 2^(exp/2)
}

Algorithm starts with 1 as first approximation for square root value.
Then, on each step, it improves next approximation by taking average between current value y and x/y. If y = sqrt(x), it will be the same. If y > sqrt(x), then x/y < sqrt(x) by about the same amount. In other words, it will converge very fast.

UPDATE: To speed up convergence on very large or very small numbers, changed sqrt() function to extract binary exponent and compute square root from number in [1, 4) range. It now needs frexp() from <math.h> to get binary exponent, but it is possible to get this exponent by extracting bits from IEEE-754 number format without using frexp().

Rooting without using sqrt in Swift

OK, so here's your code after I aligned it.

import Foundation

func rooting(number: Int) -> Int {
for i in 1...100 {
if i * i == number {
var Root = i
break
}
return Root
}
return Root
}

print(rooting(number: 9))

The problem is that you are creating the Root variable in a scope that ends before you try to use the variable. Essentially, the Root variable only exists in the following segment:

import Foundation

func rooting(number: Int) -> Int {
for i in 1...100 {
if i * i == number {
var Root = I //Root variable starts existing
break
} //Root variable no longer exists
return Root
}
return Root
}

print(rooting(number: 9))

This is because it is scoped to the if statement. To solve this specific problem, you could change your code to this:

import Foundation

func rooting(number: Int) -> Int {
for i in 1...100 {
if i * i == number {
return i
}
}
}

print(rooting(number: 9))

since you don't do anything with Root other than return it. Of course, this still won't work, as you aren't guaranteed to return anything (what if number is 8?), and you said that the function will always return an Int.


Here's a quick example of an implementation of a rooting function:

import Foundation

func rooting(number: Int) -> Int? {
guard number > 0 else { return nil }
for i in 0...(number/2) {
if i * i == number {
return i
}
}
return nil
}

print(rooting(number: 9))

If the number has a square root, it will be returned; otherwise, the function returns nil.

How to calculate the square root without using library and built-in methods in Javascript?

You could take xi (x) and the new value of xi + 1 (x1) and check if the values are equal. Then end the series and return that value.

For starting, you need an apporopriate value like the half of the given value.

function sqrt(a) {    var x,        x1 = a / 2;            do {        x = x1;        x1 = (x + (a / x)) / 2;    } while (x !== x1);    return x;}
console.log(sqrt (2)); // 1.414console.log(sqrt (9)); // 3console.log(sqrt (25)); // 5

Any way to obtain square root of a number without using math.h and sqrt()?

This method uses successive approximations. It doesn't take many iterations. Because the value of root can dither, I check the convergence to a small error.

//#define MINDIFF 2.2250738585072014e-308   // smallest positive double
#define MINDIFF 2.25e-308 // use for convergence check

double sqroot(double square)
{
double root=square/3, last, diff=1;
if (square <= 0) return 0;
do {
last = root;
root = (root + square / root) / 2;
diff = root - last;
} while (diff > MINDIFF || diff < -MINDIFF);
return root;
}

Or, you could do it more simply by iterating a fixed number of times

double sqroot(double square)
{
double root=square/3;
int i;
if (square <= 0) return 0;
for (i=0; i<32; i++)
root = (root + square / root) / 2;
return root;
}

How to find out if a number is a perfect square without using sqrt function or ** in Python?

You have to change

while num/base!=base:

to

while num/base>base:

and it will work.



Related Topics



Leave a reply



Submit