473,624 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Square root of a number.

Hi!

Could anyone tell me how to find the square root of a number without
using the sqrt function. I did it by using Newton's Formula. How can it
be done by using the Binomial Theorem/Taylor Series? Is there any other
way of doing it rather than using series?

Thank you,

Priyam

Mar 1 '06 #1
32 4978
"pr************ @gmail.com" <pr************ @gmail.com> writes:
Could anyone tell me how to find the square root of a number without
using the sqrt function.


Why? Is there something wrong with the sqrt function?

In any case, I don't think your question is particularly about C.
You might try comp.programmin g or one of the math.* groups.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 1 '06 #2
<pr************ @gmail.com> wrote:
Could anyone tell me how to find the square root of a number without
using the sqrt function. I did it by using Newton's Formula. How can it
be done by using the Binomial Theorem/Taylor Series? Is there any other
way of doing it rather than using series?


You can do it the way that used to be taught in elementary school.
Mar 1 '06 #3
In article <11************ *********@j33g2 000cwa.googlegr oups.com>,
"pr************ @gmail.com" <pr************ @gmail.com> wrote:
Hi!

Could anyone tell me how to find the square root of a number without
using the sqrt function. I did it by using Newton's Formula. How can it
be done by using the Binomial Theorem/Taylor Series? Is there any other
way of doing it rather than using series?


The usual method is to calculate sqrt (1 / x) first using Newton
iteration, because it can be done in a form that doesn't need any slow
divisions; then multiply the result by x,

On a computer without floating point unit, a reasonable approach is to
find a square root similar to performing a division: sqrt (x) = x / sqrt
(x), only modifying the remainder and the divisor after each digit of
the result is found.
Mar 1 '06 #4
pr************@ gmail.com wrote:
Could anyone tell me how to find the square root of a number without
using the sqrt function. I did it by using Newton's Formula. How can it
be done by using the Binomial Theorem/Taylor Series? Is there any other
way of doing it rather than using series?


Everything you ever wanted to know about square roots but were afraid
to ask:

http://www.pobox.com/~qed/sqroot.html

I never thought of using the Binomial Theorem. I guess you would
normalize the exponent so that you could try to expand:

(1 + x) ^ 0.5 = 1 + Choose(0.5,1) * x + Choose(0.5,2) * x^2 + ...

were Choose(r,i) = r*(r-1)*(r-2)* ... * (r-i+1) / i!

This works directly for 1 <= r < 2. If we have 2 <= r < 4 then you can
compute the square root of the reciprocal and reciprocate it again at
the end.

But in general Taylor series like expansions are going to be slower
than the other calculating methods. Even the digit by digit methods
use fewer resources per digit.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Mar 1 '06 #5
<pr************ @gmail.com> wrote in message
news:11******** *************@j 33g2000cwa.goog legroups.com...
Hi!

Could anyone tell me how to find the square root of a number without
using the sqrt function. I did it by using Newton's Formula. How can it
be done by using the Binomial Theorem/Taylor Series? Is there any other
way of doing it rather than using series?

You are passed a positive real number.
You know that the square root cannot be less than zero, and cannot be
greater than the number itself.
Take the number halfway between (half the number you are passed) and
multiply it by itself. If it is equal to the number, you have your answer.
If it is too low, you have a new minimum, if it is too high, you have a new
maximum.
Repeat with these limits, until you either get the answer or hit the
floating point accuracy of the machine - this will happen quite quickly even
for huge arguments.

This isn't the most sophisticated algorithm, but it will work.

--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $6.90 paper, available www.lulu.com
Mar 1 '06 #6
Malcolm said:
You are passed a positive real number.
You know that the square root cannot be less than zero, and cannot be
greater than the number itself.


double root = malcolmsqrt(0.2 5);

Oops.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 1 '06 #7
Richard Heathfield wrote:

Malcolm said:
You are passed a positive real number.
You know that the square root cannot be
less than zero, and cannot be
greater than the number itself.


double root = malcolmsqrt(0.2 5);


The square root of N
Is in between N and one
Except for zero

--
pete
Mar 1 '06 #8
pete said:
Richard Heathfield wrote:

Malcolm said:
> You are passed a positive real number.
> You know that the square root cannot be
> less than zero, and cannot be
> greater than the number itself.


double root = malcolmsqrt(0.2 5);


The square root of N
Is in between N and one
Except for zero


If N and one are included in the range, then it works for zero too. And if
they're not, it doesn't work for one.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 1 '06 #9
pete <pf*****@mindsp ring.com> writes:
The square root of N
Is in between N and one
Except for zero


A math haiku. Bravo!
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
Mar 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8684
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied together and the square root of the product is returned and displayed for the user. The function should return a double. Hint: If you multiply an integer by 1.0 the result will be a double. For example:
2
3183
by: Protoman | last post by:
Can you help me? For 4, my square root funct gives 4 instead of 2; here's the code: #include <iostream> #include <cstdlib> using namespace std; template<class T> T Abs(T Nbr) {
2
3136
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get a pseudorandom number generator. So, rather than calculate the square root to try to get a particular precision answer, you would calculate it to x number of bits. This particular function takes a radicand and the number of iterations as...
4
8465
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a simple task but my mind suddenly got stuck. I am trying to implement a square root function as a practice. I am able to code for the perfect square
2
4912
by: ryso | last post by:
Hi. i need a code(witten in C) for extracting the square root from a large number(100+ digits). Please help :D.Thanks
10
13626
by: socondc22 | last post by:
my program is trying to use the babylonian algorithm in order to find the square root... i have a number for the number to have the square root taken of and also a number to run the loop... Whenever i go to print out the answer its rounding instead of giving me the answer i need... any help?? double num1, num2, the_root; cout<< "Enter number to have square root taken of it\n"; cin>> num1; cout<< "Enter number for how...
4
8084
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code involving lot of matrices. At one point I need to calculate the square root of a matrix e.g. A which contains non-zero off-diagonal elements. I searched for a lot of info on net but no algorithm worked. My best bet for finding square root was to find...
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8681
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.