Karim Belabas on Mon, 17 Sep 2012 22:21:11 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: forprime


* Bill Allombert [2012-09-17 18:50]:
> On Sun, Sep 16, 2012 at 09:12:14AM +0200, Karim Belabas wrote:
> > > On Sat, Sep 15, 2012 at 3:36 AM,  <michel.marcus@free.fr> wrote:
> > > > forprime loops over prime numbers.
> > > >
> > > > is there a  function that would loop over composite numbers ?
> > * Charles Greathouse [2012-09-15 09:41]:
> > > I typically write
> > > 
> > > p=3; forprime(q=5, lim, for(n=p+1, q-1, /* your code here */); p=q)
> > 
> > It's not easy to do this properly in GP and the result is not that readable
> > [ N.B. the above loops through composites only up to precprime(lim) ]
> > 
> > I just committed a function forcomposite() to 'master', following the (new)
> > forprime() model:
[...]
> What is the usecase for such function ? I never needed it myself.
> 
> At worse you can do 
> for(a=1,1000,if(!isprime(a),print(a)))

Rather slower than the original code snippet, but more correct.

(20:07) gp > lim=10^8;
(20:07) gp > s=0;forcomposite(q=3,lim,s++); s
time = 12,846 ms.
%2 = 94238544

(20:07) gp > s=0; p=3; forprime(q=5, lim, for(n=p+1, q-1, s++); p=q)
time = 13,801 ms.  \\ result not printed we know it's a little wrong

(20:07) gp > s=0;for(q=3,lim,if (!isprime(q),s++)); s
time = 54,684 ms.
%1 = 94238544

> I feel concerned because I will have to write dedicated code for GP2C to
> handle it...

How much dedicated code ? I intended to add a few more loop constructs to GP: 

  - forstepprime()    [ loop over primes in an arithmetic progression ]
  - forprimepowers()  [ loop over prime powers ]

How costly would these be ? (The first one is already in master, only not
exported to GP yet.)

Those are (much) more useful than forcomposite(), although I thought it didn't
hurt to have the latter. Since it's non-trivial to write properly...

Cheers,

    K.B.
-- 
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite Bordeaux 1          Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation    http://www.math.u-bordeaux1.fr/~belabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux1.fr/  [PARI/GP]
`