posted 14 years ago

Hi,

It's me again. I'm having a time with a program that I'm suppose to write. I'm suppose to write a program calculating the square root of a number without using the "math class" method. My text gives me a formula to use:

nextGuess = (lastGuess + (num / lastGuess)) /2

with the initial guess being the starting value of lastGuess. Also if the differences between next and last guess is very small then nextguess can be the approx sqrt.

I don't understand the math behind this formula to even try and work it into code. Am I setting values for nextGuess and lastGuess and going from there? If so, what values would I start with.

I'm sooo lost!!!

Can anyone help me?

Stacey

It's me again. I'm having a time with a program that I'm suppose to write. I'm suppose to write a program calculating the square root of a number without using the "math class" method. My text gives me a formula to use:

nextGuess = (lastGuess + (num / lastGuess)) /2

with the initial guess being the starting value of lastGuess. Also if the differences between next and last guess is very small then nextguess can be the approx sqrt.

I don't understand the math behind this formula to even try and work it into code. Am I setting values for nextGuess and lastGuess and going from there? If so, what values would I start with.

I'm sooo lost!!!

Can anyone help me?

Stacey

posted 14 years ago

i'm not sure how you are supposed to choose your starting guesses, but you could probably assume 1 as a first guess and 0 as a last guess, initially.

you can now kind of work through it with a real number... say you want to find the square root of 9.

lG = 0

nG = 1

diff = 1 - Not Small!!!

lg now becomes 1

nG = (lg + ( num / lg )) / 2... or

nG = (1 + ( 9/1 )) / 2 = 5.

diff is 1 - 5 or 4 - not small!!!

lg now becomes 5

nG = (5 + 9/5) / 2 = 3.4

diff is 1.6 - not small!!!

lg = 3.4

nG = (3.4 + 9/3.4) / 2 = 3.02...

diff is about .3... we're getting close...

ultimatly, YOU have to decide what is "close enough". In the "Real World", this is done all the time - it depends on the application. measuring a first down for a game of backyard football may only be within a few feet, but at an NFL game, you better be well withing an inch...

if you want to understand some of the math, you can look at it like this...

each time, you divide your guess into the original number. if your guess IS EXACTLY THE SQUARE ROOT, when you divide it into your starting number, you get THE SQUARE ROOT. so, the average of these two will be the same number - your guess doesn't change.

But, the farther off your guess is, the greater the difference between the quotient and your last guess. if you guess is too big, the quotient will be too small, so you'll pull your next guess down. and vice versa.

does this help at all???

you can now kind of work through it with a real number... say you want to find the square root of 9.

lG = 0

nG = 1

diff = 1 - Not Small!!!

lg now becomes 1

nG = (lg + ( num / lg )) / 2... or

nG = (1 + ( 9/1 )) / 2 = 5.

diff is 1 - 5 or 4 - not small!!!

lg now becomes 5

nG = (5 + 9/5) / 2 = 3.4

diff is 1.6 - not small!!!

lg = 3.4

nG = (3.4 + 9/3.4) / 2 = 3.02...

diff is about .3... we're getting close...

ultimatly, YOU have to decide what is "close enough". In the "Real World", this is done all the time - it depends on the application. measuring a first down for a game of backyard football may only be within a few feet, but at an NFL game, you better be well withing an inch...

if you want to understand some of the math, you can look at it like this...

each time, you divide your guess into the original number. if your guess IS EXACTLY THE SQUARE ROOT, when you divide it into your starting number, you get THE SQUARE ROOT. so, the average of these two will be the same number - your guess doesn't change.

But, the farther off your guess is, the greater the difference between the quotient and your last guess. if you guess is too big, the quotient will be too small, so you'll pull your next guess down. and vice versa.

does this help at all???

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |