home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #23 / NN_1992_23.iso / spool / sci / math / 13313 < prev    next >
Encoding:
Text File  |  1992-10-16  |  1.5 KB  |  54 lines

  1. Path: sparky!uunet!ogicse!uwm.edu!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!nucsrl!kaufman
  2. From: kaufman@eecs.nwu.edu (Michael L. Kaufman)
  3. Newsgroups: sci.math
  4. Subject: Here's a program to compute pi quickly
  5. Message-ID: <1992Oct16.153942.28398@eecs.nwu.edu>
  6. Date: 16 Oct 92 15:39:42 GMT
  7. Article-I.D.: eecs.1992Oct16.153942.28398
  8. Sender: kaufman@eecs.nwu.edu (Michael L. Kaufman)
  9. Organization: EECS Department, Northwestern University
  10. Lines: 42
  11.  
  12. Since I asked the question in the first place, I thought I would post an
  13. answer. I found this in David Wells's book _The Penguin Dictionary of
  14. Curious and Intersting Numbers_. (The book by the way is quite interesting,
  15. although I have found a few mistakes in it.) The method, goes like this:
  16.  
  17. #include <math.h>
  18. main()
  19. {
  20.     float x,y,a,b,c;
  21.     int ch = 1;
  22.  
  23.     a = x = 1.0;
  24.     c = 0.25;
  25.     b = 1 / sqrt(2);
  26.  
  27.  
  28.     while(ch != 'q')
  29.     {
  30.         y = a;
  31.         a = (a + b) / 2.0;
  32.         b = sqrt(b * y);
  33.         c = c - x * (a - y) * (a - y);
  34.         x = 2.0 * x;
  35.     
  36.         printf("%10g\n", (a + b) * (a + b) / (4 * c));
  37.         ch = getchar();
  38.     }
  39. }
  40.  
  41.  
  42.  
  43. Each pass through the loop doubles the number of digits you have.
  44.  
  45. Anyhow, thanks to the people who helped me, and I hope this helps someone else.
  46.  
  47. Michael
  48.  
  49. -- 
  50. Michael Kaufman | I've seen things you people wouldn't believe. Attack ships on
  51.  kaufman        | fire off the shoulder of Orion. I watched C-beams glitter in
  52.   @eecs.nwu.edu | the dark near the Tannhauser gate. All those moments will be
  53.                 | lost in time - like tears in rain. Time to die.     Roy Batty 
  54.