• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help!!!

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's best to start with pseudo code then plug actual code into that after you have something that sounds right

I kind of mangled the code to hide where the loop is
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here is the program. It is an interesting formula indeed!
[deleted]
[ January 15, 2004: Message edited by: Jim Yingst ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoa! This is a homework problem, right Stacey? We don't want to be posting complete solutions here. Sorry Mohan.
 
reply
    Bookmark Topic Watch Topic
  • New Topic