Using Heron's method? Question

Edit: Oh, wait that would require addition and division functions as well as display functions. Well there's guess and check but you would still need some display functions there, too. I'm stumped, how did you make it without any functions? Razz </sarcasm>
dont know. ill post the code tomorrow morning. i had no help in doing this.
You probably used guess and check then. Heron's method is a much more intelligent, accurate, and quick way of obtaining it. The theory is as follows:

1. You take a random guess of the square root of a number and store that to A
2. (A + N/A)/2 will be closer and thus store this to A
3. Repeat this for a certain amount of iterations (you don't need many as it converges fast)
4. A becomes the square root of N

Here's how I make square roots (without using the square root key or similar cheats)


Code:

:Prompt N
:Rand->A
:While .00000001<abs(A-B  ;I think that is enough accuracy
:A->B
:.5(A+N/A->A
:End
:A


Wow, its almost as fast as the OS and incredibly accurate. Come on, that's pretty 1337 Very Happy

Another interesting point about Heron's formula is you can easily prove it. Let's say X is our square root.

(Sqr(X)+X/Sqr(X))/2 = Sqr(X) ;original
(X/Sqr(X) + X/Sqr(X))/2 = Sqr(X);lcm
(2X/Sqr(X))/2 = Sqr(X) ;combine like terms
2X/(2Sqr(X)) = Sqr(X) ;dividing is the same thing as multiply by the inverse
X/Sqr(X) = Sqr(X) ;Cancel the two's
XSqr(X)/X = Sqr(X) ;Rationalize that denominator
Sqr(X) = Sqr(X) ;Cancel the X's
0 = 0 ;Heh, I just like to do this to show that two formulas that equal each other always break down to this
Woot Smile
dang, very 1337 indeed
Thanks, I had to learn this method for the AIME where I couldn't use a calculator, yet was still expected to take square roots of numbers greater than 100.
Chipmaster wrote:

Code:

:Prompt N
:Rand->A
:While .00000001<abs(A-B  ;I think that is enough accuracy
:A->B
:.5(A+N/A->A
:End
:A


Quote:

:Prompt N
:Rand->A
:While E-8<abs(A-B
:A->B
:.5(A+N/A->A
:End
:A
mine was different than either of those. I guess I wont post it however since Im sick today and cant access my files at school.
37 bytes is pretty good, though. Smile
I thouhgt Heron's formula was the name of a formula used for finding the area of triangles....
.5sqrt(.5A^2+.5B^2+.5C^2 ...something like that.
yeah...so are their 2 Heron's formulas?
They are both from the same formula, apparently:
http://mathworld.wolfram.com/HeronsFormula.html
cool. I didn't know that
Nor I. We both learned something today. Smile
Heron's Formula =/= Heron's Method. Two different things entirely. You're right on Heron's formula. Its used for area of trianges. Heron's Method is different. See this.

KermMartian wrote:
37 bytes is pretty good, though. Smile
Thanks of course the best way would be to

Code:

:Prompt N
:Sqr(N
w00t w00t...

heres my code:


Code:
import java.util.*;


public class SqrtFinder{

   private static final double TOFIND = 544452;

   double value;
   public SqrtFinder(byte b){value = b;}
   public SqrtFinder(short b){value = b;}
   public SqrtFinder(int b){value = b;}
   public SqrtFinder(long b){value = b;}
   public SqrtFinder(float b){value = b;}
   public SqrtFinder(double b){value = b;}


   public double findSqrt(){
      double cur = value;
      double toOp = cur;
      if(value < 0) return Double.NaN;
      for(int i = 0; i < 128; i++){
         toOp /= 2;
         if(cur*cur < value) cur += toOp;
         if(cur*cur > value) cur -= toOp;
         if(cur*cur == value){return cur;}
      }
      return cur;
   }

   public static void main(String[] args){
      Scanner in = new Scanner(System.in);
      for(int x = 0; x < 10; x++){
         System.out.println("Enter a number:");
         double i = in.nextDouble();
         SqrtFinder fnd = new SqrtFinder(i);
         System.out.println("Our sqrt = " + fnd.findSqrt());
         System.out.println("Official Java sqrt = " + Math.sqrt(i));
      }
   }
}


I also use a slightly more complex (bad pub...) version for the new php script I got set up for Kerm to use in the say box.
Elfprince: where's this code you speak of?
Edit: nvm.
maybe... Smile
Keadiae, i have a few words of advice if you are going to post in this forum.

Do NOT necropost (post in a long dead topic), unless you want to be a target for Kllrnoj, 0x5

P.S. What was the

Quote:
maybe...


responding too? Use quotes?
another one, one word posts = no no

If you do necropost, at least make it a usefull comment.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
» Goto page Previous  1, 2, 3, 4, 5, 6  Next
» View previous topic :: View next topic  
Page 3 of 6
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement