Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello everyone,
i need a simple algorithm for finding out square root a 4 digit number...
I know how to find out it on PAPER but designing algorithm is not being successful...plz help me out..
Posted
Comments
Sergey Alexandrovich Kryukov 28-Jul-13 1:55am    
Homework?
—SA
bbirajdar 28-Jul-13 5:13am    
My suggestion to all the people who post code to homework type questions-

Its the codeproject's unwrittem policy not to spoon feed or provide code to homework type of questions. In those cases proper guidance or references are expected on the basis of which the person who posts the question can move ahead a step, study and implement it on his own. The motive behind this is to help him learning and not just make him copy paste the code and execute it without understanding.

I always remember the chinese proverb whenever I see these types of questions -"Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime.”

No offence against anybody. But I have always seen the most senior members on CP never post code to homework type of questions and I try to follow them. So I never vote these solutions even if they are correct (my own policy).
Sergey Alexandrovich Kryukov 28-Jul-13 12:04pm    
If you posted it as an answer, I believe it would be the best answer so far.
—SA
H.Brydon 28-Jul-13 22:48pm    
I believe it is "Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime. Show him how to use Google and he will never bother you again.”
Sergey Alexandrovich Kryukov 3-Jun-15 21:15pm    
Those words about feeding a fish are golden.
And by the way, I hope you will like my own saying as well:
Feeding Apple or Why Don't We Answer Some Questions.
—SA

I know there is a method for calculating the square root manually, but that one ins not particularly useful for calculating it on a computer. Computers can multiply and divide numbers with many digits and the execution time is nearly the same, no matter the factor are a single digit or contain many decimal places.

One of the easiest square root algorithms for a computer is the so-called Babylonian method (in fact an application of the Newton algorithm for solving an equation):

C++
x = 0.5 * (x + s/x);

s being the number that you want to take the square root of. See http://en.wikipedia.org/wiki/Square_root_algorithm[^].

For the starting value you might choose x=s/4. Four iterations of the above formula should give you a precision of about 6 significant digits.
 
Share this answer
 
v2
Comments
Joezer BH 28-Jul-13 5:09am    
5ed!

this method is being taught in Universities as an example of simplicity in algorithms which could otherwise have been complicated, while this is not an exact answer, you would easily produce a "close to" answer with the appropriate precision, if you take 20 iterations for instance that would probably suit even NASA calculations
nv3 28-Jul-13 5:56am    
Thank you.
Such algorithm is already implemented in the CPUs. That said, it looks like your problem is not a real problem, but the one posed for educational purpose. It means that, most likely, this is some kind of home assignment, and this one is a pretty interesting one.

Will it help you if someone does your home assignment for you? I don't think so. Not only you need yet another credit, but you really need education. By not doing your assignment by yourself, you are loosing yest another chance to get some more knowledge and experience. And loosing this chance is not good for your. So, use your chances which your school gives you.

—SA
 
Share this answer
 
Comments
nv3 28-Jul-13 4:45am    
Not all CPUs have a build-in instruction for square-rooting. So I would not be all that harsh with the guy. I just wonder, why he hasn't taken a look into Wikipedia.
Sergey Alexandrovich Kryukov 28-Jul-13 12:25pm    
Thank you for your note. Yes, it shows that my speculations were not quite accurate, but I don't think the fix of them could shake my logic. Well, most systems where such calculations are needed, standard libraries, etc.

Maybe you are right about some harshness, you never know exactly how to deal with people in some situations, but I personally think it's just right. Note that I never characterize the person in any negative way. I would not have any right to say something like that: he just asked a question, it should not be discouraged. I'm only giving OP the advice, what to do with this situation.

—SA
nv3 28-Jul-13 13:27pm    
No problem, Sergey. You are in general a very good tutor.
Sergey Alexandrovich Kryukov 28-Jul-13 14:36pm    
Agree, no problems. Thank you for your nice words...
—SA
bbirajdar 28-Jul-13 5:03am    
+5 for the guidance...
Do something like this:
C#
double square_root(double low, double high)
{
    double mid, result = 0.0;

    mid = (high + low) / 2;

    //Base Case
    if((mid*mid) == high) {
        result = mid;
    } else if((high - low) < 0.001) {
        //Stops when the high and low numbers are within 0.001 of one another.
        result = mid;
    } else if((mid*mid) > high) {
        result = square_root(low, mid);
    } else {
        result = square_root(mid, high);
    }
    return result;
}

Or,
See..
http://edisontus.blogspot.com/2013/04/square-root-algorithm-for-c.html[^]
 
Share this answer
 
Comments
Stefan_Lang 29-Jul-13 10:39am    
So, ... what is the square root of 0.25 again?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900