Prime bug
- Subject: Prime bug
- From: wheeler at super.org
- Date: Tue, 19 Sep 89 23:56:17 -0500
- Apparently-to: mathgroup-out at yoda.ncsa.uiuc.edu
Here is a code fragment for computing the number of primes <= x for
moderate values of x.
PrimePi[x_?NumberQ] :=
Block[{n, m, avg, flrx = Floor[x]},
m = LogIntegral[N[flrx]];
n = Ceiling[m - LogIntegral[Sqrt[N[flrx]]]];
m = Floor[m];
While[ m != n+1,
avg = Quotient[n+m,2];
Print[n," ", Prime[n]];
If[ flrx < Prime[avg], m = avg, n = avg]
];
n
]
The thing to look at is the Print statement in the While loop.
Watch this:
Mathematica (sun3.68881) 1.2 (August 3, 1989) [With pre-loaded data]
by S. Wolfram, D. Grayson, R. Maeder, H. Cejtin,
S. Omohundro, D. Ballman and J. Keiper
with I. Rivin and D. Withoff
Copyright 1988,1989 Wolfram Research Inc.
In[1]:= <<PrimePi.m
0.0666667 Second
In[2]:= p = Prime[105096565]
0.283333 Second
Out[2]= 2147462123
In[3]:= PrimePi[p]
105094402 2147415443
105094402 2147415443
105095608 2147417855 <-- Clearly not a prime!!
105096211 2147419061
105096513 2147419665
105096664 2147419967
105096739 2147420117
105096777 2147420193
105096796 2147420231
105096805 2147420249
105096810 2147420259
105096812 2147420263
105096813 2147420265
1.3 Second
Out[3]= 105096814 <-- Not the right answer.
In[4]:= Prime[105094402]
0.366667 Second
Out[4]= 2147368919 <-- This is not what is given in the list!
Why is there a difference in the Prime function within the
program and the Prime function on the top level?
Ferrell Wheeler
Supercomputing Research Center
wheeler at super.org
(301) 805-7360