Square Root without math.h

Hi guys. I am having trouble coding a square root in c++. My lecturer gave me an math assignment about vectors and i am not allowed to use any other libraries except for the standard standard input/output libraries so i cant use the "sqrt()" . :( In my assignment i need to use the square root function to calculate some stuff inside.

I am able to do a power square as n^2 means n*n.
But i dont know how to do a square root. Since square root of n also means n^1/2, does it mean i need to multiply it with something too?

I'm really at a loss here. Would appreciate any help :)
Thank You in advanced
Last edited on
You can do that with Heron's method:
http://en.wikipedia.org/wiki/Heron's_method
I have read through the wiki but I still dont get it. Would it be possible if you could explain more of the babylonian method please?
What's the problem? The article lists the three steps you have to perform.
It even has an example.
i do not understand why i would need to do x0 = 6 * 10^2
That's explained under "Rough estimation".
125348 has six digits, so D=6.
6 is even. Then 6*10^n is used the start value, which is a rough approximation of the real square root.
n is defined by D=2*n+2, so n=(D-2)/2 = 2
The simpliest (but certainly not fastest) method is to mulitply increasing values with itself in a loop until the result is the value in question
Another is to start with a value, multiply it with itself and see if it's larger or smaller than the radicand and to adjust your value accordingly using binary search (the method you use in the number guessing game).
i do not understand why i would need to do x0 = 6 * 10^2


I feel for you, math equations confuse me as well.
@Athar
so lets say i want to square root the number 298. Therefore D = 3 right?
so my code should be like this?

1
2
3
4
5
6
7
8
9
10
double squareRoot (double number) {
	double temp;
	x0 = 3 * (10*10); // x0 = 3 * (10^2)

	for (int i = 1; i < 3; i++) {
		temp = 0.5(x0 + (number/x0));
	}

	return temp;
};


@coder777
im sorry but i do not understand what you meant. do you mind elaborating it a bit more please? thank you :)
Close enough. For 298, x0 is 2*10ยน=20, not 300.
Your loop only runs twice, but you should repeat the step at least D times.

You also can't omit the multiplication operator in C++.
Thank You Athar. Much appreciated. And ya i had a typo error didnt put the *. But thanks again though for the help. Really helpful thanks.

And thanks to all the other people who have helped. Cant thank you enough! :D

Have a nice day guys! :D
wat coder 777 says is to do thiis:(which would work only if it is a perfect square
suppose the number is n;
1. We start i from 1 to n-1, and try comparing i^2 with n...if no match is found then,goto step 2 else abort.
2.find numbers i and i+1 such that i^2<n<(i+1)^2
3.Then divide the interval i,i+1 into 10 equal parts, and repeat steps 1&2.
4.The point at which u stop is ur desired level of accuracy, and state the answer as the mean of the two values b/w which the square root lies.......................

the code is alongside
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<conio.h>
#include<stdio.h>
main()
{float n,i,j,start,end,size,acc_level,c=0,count=0;
clrscr();
printf("Enter the number");
scanf("%f",&n);

printf("%f",epo(10,1));
start=1;end=n;size=1;
for(i=start;i<=end;i+=size)
{
if(n==(i*i))
{
printf("Square root of the entered number is %f",i);
goto end;
}
}
printf("Enter the accuracy level");
scanf("%f",&acc_level);
for(i=start;i<=end;i+=size)
	{
		if(i*i<n&&(i+size)*(i+size)>=n)
		{
		start=i,end=i+size;
		size=size/10;printf("%f",i);count++;
		}
	if(count==acc_level)
	break;
	}



printf("The square root to the desired level is %f",(start+end)*0.5);
end:
getch();
}
however be sure to put the accuracy level in the range [1,8],giving considerably accurate values

Last edited on
Topic archived. No new replies allowed.