Calculating square root without sqrt

Posted 10 June 2009 - 05:29 PM

Can somebody help me with this. I need to calculate of a very big number with 1000 digits. I can't use sqrt because it wont fit in long long int. The input should be like this: First the number of digits, then the digits and output first the number of digits then the digits.
Input:
3
6
2
5
Output:
2
2
5
I know about finding the square root by hand procedure, but it's too hard for me to do it in a program. I used this code below but it's too slow and it't not for huge numbers.

Re: Calculating square root without sqrt

Re: Calculating square root without sqrt

Posted 10 June 2009 - 07:52 PM

Quote

I need to calculate of a very big number with 1000 digits.

Hardware cannot store such a long number. Either you implement a software solution yourself, or look for a large number library. There's a few out there. Look under the names arbitrary precision library or large number library.

Don't be confused from this. All we did is turn the problem into a long multiplication equation. But the new obstacle is like you said, there's no room to store so many digits!

The concept to know here is BigInt. BigInt is usually mentioned when adding HUGE numbers together. Multiplying is the same thing, almost. All you need is a vector and some imagination.

Make a vector for your answer.

#include <vector>
vector<int> answer;

/*
2.2
x3.1
------
2 2
6 6
------
6.8 2
*/
double temp = 6.82;
answer.push_back( (int)temp ); //Cast it so it only stores the 6! Use better way to cast.
//Find a way to just keep the leftover .8 and store it in another variable
/*Now...the next √10 is taken care of:
6( plus .8 from that temp )
x3.1
--------
21.08
*/

The six you are now multplying with at the top is from the vector itself. The .8 is from the variable keeping track of leftover decimals. You keep them seperate to keep the vector with only ints.

When you end up with something like 21, which is 2 digits, carry the 2 over and add it to the next equation you will multiply--and store the 1 in the vector like we did to the 6.

Summary:

1) Break 'em down to many small roots of √10's and whatever is left( scientific notation form ), in this case, the √4.892

2) Multiply then store the first digit on the left of the . into your vector

3) Strip its decimals away and add it to the vector and times it by your next √10

4) Keep repeating until you run out of √10's to multiply to the growing vector. In this case, until your sum is near enough to 69.9428( accurate enough for your liking*see above Note about precision*)

5) Your vector will contain your answer when you're done so you'd have to cout it using a for loop to loop through its index. The answer will probably be backwards so do something to flip it around.

I understand very well that my explanation is nearly impossible to follow but this isn't an easy thing to digest. I've known many people who were given the task to design a BigInt class and never get it done, and that was just simple addition. Hopefully you at least know how to break your problem down now using factors of √10.

Re: Calculating square root without sqrt

Posted 12 June 2009 - 04:36 AM

Newtons method for solving equations can be used to find square roots easily and quickly,
It state's x1 = x0 - f(x0)/f'(x0), where x0 is its initial estimate and x1 is its new estimate which will be closer to actual value.
Starting with n as starting no. and x as the answer
√n = x
So x^2 = n
f(x) = x^2 - n = 0
f'(x) = 2x

So in newtons method x1 = x0 - (x0 ^2 -n)/2x0
And in c++ that looks like this

#include <iostream>
using namespace std;
double sqroot(double n);
double sqroot(double n)
{
double estimate;
double newEstimate;
newEstimate = n-1;
estimate = n;//estimate can be made closer to expected value but the number to square will also work just needs to run a few more times
for(int i =0;estimate != newEstimate && i !=20;i++){// i is only to stop cycling due to calculation errors in using doubles
estimate = newEstimate;
newEstimate = estimate- (estimate*estimate - n)/(2*estimate);
}
return newEstimate;
}
int main(){
double temp;
cout<<"Please enter no. to sqaure";
cin>>temp;
temp = sqroot(temp);
cout<<temp;
return 0;
}