home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / PROGRAMM.PAK / PRIMEPI.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  850 b   |  35 lines

  1.  
  2. (*********************************************************************
  3.  
  4.         Adapted from
  5.         Roman E. Maeder: Programming in Mathematica,
  6.         Second Edition, Addison-Wesley, 1991.
  7.  
  8.  *********************************************************************)
  9.  
  10.  
  11. (* the function is called "MyPrimePi" to avoid naming conflicts
  12.    with the built-in function PrimePi *)
  13.  
  14. MyPrimePi::usage = "MyPrimePi[x] returns the number of primes <= x."
  15.  
  16. Attributes[MyPrimePi] = {Listable}
  17.  
  18. Begin["`Private`"]
  19.  
  20. MyPrimePi[x_] := 0 /; x < 2
  21.  
  22. MyPrimePi[x_] :=
  23.     Module[{li, n0, n1, m, nx = N[x]},
  24.         li = LogIntegral[nx];
  25.         n0 = Floor[li - LogIntegral[Sqrt[nx]]];
  26.         n1 = Ceiling[li];
  27.         While[ n1 - n0 > 1,
  28.             m = Floor[(n0+n1)/2];                 (* midpoint *)
  29.             If[ Prime[m] <= nx, n0 = m, n1 = m ]
  30.         ];
  31.         n0
  32.     ] /; x >= 2
  33.  
  34. End[]
  35.