home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / programm / 3551 < prev    next >
Encoding:
Internet Message Format  |  1993-01-22  |  4.6 KB

  1. Path: sparky!uunet!mcsun!uknet!comlab.ox.ac.uk!imc
  2. From: imc@comlab.ox.ac.uk (Ian Collier)
  3. Newsgroups: comp.programming
  4. Subject: Re: how to calculate pi + how about PRIME numbers ?
  5. Message-ID: <2953.imc@uk.ac.ox.prg>
  6. Date: 22 Jan 93 14:23:17 GMT
  7. References: <peterd.727578786@tscc2> <LOWRY.93Jan20104500@rotor.watson.ibm.com> <4659@sicsun.epfl.ch> <andrea.727558073@pX1.stfx.ca> <c8h3xpm@rpi.edu>
  8. Organization: Oxford University Computing Laboratory
  9. Lines: 105
  10. X-Local-Date: Friday, 22nd January 1993 at 2:22pm GMT
  11. Originator: imc@msc6.comlab
  12.  
  13. OK, time for my 2p :-)
  14.  
  15. In article <peterd.727578786@tscc2>, peterd@tscc2.macarthur.uws.edu.au wrote:
  16. >Ok, I asked for an algorithm for PI (and got several responses - thank you)
  17. >(Why am I asking for this ? I just want to give my poor suffering pc
  18. >something to do in the background of OS/2, seeing as it's never switched
  19. >off intentionally I should be able to just leave it running !)
  20.  
  21. OK, if you are using OS/2 then you should be able to use REXX (there, I said
  22. it again) so that you don't need to write yourself an arbitrary precision
  23. arithmetic library.  I sent out several examples to a couple of people by
  24. mail last November, and I still have them.
  25.  
  26. >How about an efficent algorithm for PRIME numbers ?
  27.  
  28. The usual way to generate a list of primes is with the sieve of Eratosthenes:
  29.  
  30.  1. Write a list of all natural numbers except 0 and 1 in ascending order
  31.  2. Remove the first element from the list and call it p
  32.  3. Output p
  33.  4. Cross off the list all multiples of p
  34.  5. go to 2.
  35.  
  36. Obviously you will have to truncate the list you make in (1) and limit the
  37. range of the prime numbers (unless you use a functional language with lazy
  38. evaluation).
  39.  
  40. If you are more interested in obtaining a few large prime numbers than in
  41. making a list, then there are more efficient methods than the above.
  42.  
  43. And now back to pi...
  44.  
  45. Message-ID: <4659@sicsun.epfl.ch>
  46. From: guglielmetti@elia.epfl.ch (Philippe Guglielmetti)
  47.  
  48. >For a fast algorithm (using reals), I think you can use almost any serie.
  49. >for example
  50. >arcsin x = x + (1*x^3)/(2*3) + (1*3*x^5)/(2*4*5) + (1*3*5*x^7)/(2*4*6*7)
  51. >If you want a precision algorithm, then it becomes really interesting
  52.  
  53. The above is fairly fast among simple summations for pi, but there are
  54. iterative methods which can beat any summation.  As for your second point,
  55. REXX contains its own precision arithmetic, making life much easier.
  56.  
  57. Message-ID: <andrea.727558073@pX1.stfx.ca>
  58. From: andrea@pX1.stfx.ca (John Andrea)
  59.  
  60. >There is an iterative algorithm for Pi given in 
  61. >"Ramaujan & Pi"
  62. >Scientific American, Feb. 1989
  63. >The accuracy quadrouples at each iteration.
  64.  
  65. However, I find that the 4th root is so much slower to calculate than the
  66. square root that a similar quadratic method can turn out faster than this
  67. quartic.  A compromise, using cube roots and having cubic convergence, was
  68. published in the October 1992 issue of "Notices of the AMS".
  69.  
  70. Message-ID: <LOWRY.93Jan20104500@rotor.watson.ibm.com>
  71. From: lowry@watson.ibm.com (Andy Lowry)
  72.  
  73. >One of the coolest formulas I've seen in Gregory's formula:
  74. >  pi/4 = 4*atan(1/5)-atan(1/239)
  75. >It's one of those formulas that makes you want to know how in the
  76. >world anybody ever stumbled across it.
  77.  
  78. Well... you know that tan(x+y) = (tan x + tan y)/(1 - tan x.tan y)  and
  79. therefore that  atan p = atan x + atan[(p-x)/(1+px)] for any x.
  80.  
  81. If you guess at x=1/5 and use the above formula several times to make the
  82. residue as small as possible, you get:
  83.  
  84. pi/4 = atan 1 = atan 1/5 + atan 2/3
  85.               = atan 1/5 + atan 1/5 + atan 7/17
  86.           = atan 1/5 + atan 1/5 + atan 1/5 + atan 9/46
  87.           = atan 1/5 + atan 1/5 + atan 1/5 + atan 1/5 - atan 1/239
  88.  
  89. If you try this with several values of x, you will probably find that 1/5
  90. comes out the best.
  91.  
  92. Message-ID: <c8h3xpm@rpi.edu>
  93. From: rogerj@aix.rpi.edu (Diversion (Jeff Rogers))
  94.  
  95. >Why not just 
  96. >   pi/4=atan(1)? 
  97. >If your answer is in radians, this works too, but
  98. >I think it requires prior knowledge of pi. 
  99. >Maybe this is the answer (to my question above), but I don't see why a
  100. >taylor expansion would work for some values and not others.
  101.  
  102. If you don't know, it's perhaps best not to comment...
  103.  
  104. The taylor series for atan(x) is
  105.   x - x3/3 + x5/5 - x7/7 + ...
  106. (where x3 means x cubed, etc).  The result is in radians.  Now insert x=1
  107. into that formula and see how long it takes you to calculate pi to 50
  108. figures...
  109.  
  110. This taylor series converges for any x such that |x|<=1.  However the
  111. convergence is faster for small values of x - hence 1/5 and 1/239 are
  112. suitable values to use.  The series can be improved by applying certain
  113. transformations to its argument, but it is never particularly brilliant on
  114. an input of 1.
  115.  
  116. Ian Collier
  117. Ian.Collier@prg.ox.ac.uk | imc@ecs.ox.ac.uk
  118.